clang  5.0.0
CGDecl.cpp
Go to the documentation of this file.
1 //===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This contains code to emit Decl nodes as LLVM code.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CGBlocks.h"
15 #include "CGCXXABI.h"
16 #include "CGCleanup.h"
17 #include "CGDebugInfo.h"
18 #include "CGOpenCLRuntime.h"
19 #include "CGOpenMPRuntime.h"
20 #include "CodeGenFunction.h"
21 #include "CodeGenModule.h"
22 #include "TargetInfo.h"
23 #include "clang/AST/ASTContext.h"
24 #include "clang/AST/CharUnits.h"
25 #include "clang/AST/Decl.h"
26 #include "clang/AST/DeclObjC.h"
27 #include "clang/AST/DeclOpenMP.h"
29 #include "clang/Basic/TargetInfo.h"
32 #include "llvm/IR/DataLayout.h"
33 #include "llvm/IR/GlobalVariable.h"
34 #include "llvm/IR/Intrinsics.h"
35 #include "llvm/IR/Type.h"
36 
37 using namespace clang;
38 using namespace CodeGen;
39 
41  switch (D.getKind()) {
42  case Decl::BuiltinTemplate:
43  case Decl::TranslationUnit:
44  case Decl::ExternCContext:
45  case Decl::Namespace:
46  case Decl::UnresolvedUsingTypename:
47  case Decl::ClassTemplateSpecialization:
48  case Decl::ClassTemplatePartialSpecialization:
49  case Decl::VarTemplateSpecialization:
50  case Decl::VarTemplatePartialSpecialization:
51  case Decl::TemplateTypeParm:
52  case Decl::UnresolvedUsingValue:
53  case Decl::NonTypeTemplateParm:
54  case Decl::CXXDeductionGuide:
55  case Decl::CXXMethod:
56  case Decl::CXXConstructor:
57  case Decl::CXXDestructor:
58  case Decl::CXXConversion:
59  case Decl::Field:
60  case Decl::MSProperty:
61  case Decl::IndirectField:
62  case Decl::ObjCIvar:
63  case Decl::ObjCAtDefsField:
64  case Decl::ParmVar:
65  case Decl::ImplicitParam:
66  case Decl::ClassTemplate:
67  case Decl::VarTemplate:
68  case Decl::FunctionTemplate:
69  case Decl::TypeAliasTemplate:
70  case Decl::TemplateTemplateParm:
71  case Decl::ObjCMethod:
72  case Decl::ObjCCategory:
73  case Decl::ObjCProtocol:
74  case Decl::ObjCInterface:
75  case Decl::ObjCCategoryImpl:
76  case Decl::ObjCImplementation:
77  case Decl::ObjCProperty:
78  case Decl::ObjCCompatibleAlias:
79  case Decl::PragmaComment:
80  case Decl::PragmaDetectMismatch:
81  case Decl::AccessSpec:
82  case Decl::LinkageSpec:
83  case Decl::Export:
84  case Decl::ObjCPropertyImpl:
85  case Decl::FileScopeAsm:
86  case Decl::Friend:
87  case Decl::FriendTemplate:
88  case Decl::Block:
89  case Decl::Captured:
90  case Decl::ClassScopeFunctionSpecialization:
91  case Decl::UsingShadow:
92  case Decl::ConstructorUsingShadow:
93  case Decl::ObjCTypeParam:
94  case Decl::Binding:
95  llvm_unreachable("Declaration should not be in declstmts!");
96  case Decl::Function: // void X();
97  case Decl::Record: // struct/union/class X;
98  case Decl::Enum: // enum X;
99  case Decl::EnumConstant: // enum ? { X = ? }
100  case Decl::CXXRecord: // struct/union/class X; [C++]
101  case Decl::StaticAssert: // static_assert(X, ""); [C++0x]
102  case Decl::Label: // __label__ x;
103  case Decl::Import:
104  case Decl::OMPThreadPrivate:
105  case Decl::OMPCapturedExpr:
106  case Decl::Empty:
107  // None of these decls require codegen support.
108  return;
109 
110  case Decl::NamespaceAlias:
111  if (CGDebugInfo *DI = getDebugInfo())
112  DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(D));
113  return;
114  case Decl::Using: // using X; [C++]
115  if (CGDebugInfo *DI = getDebugInfo())
116  DI->EmitUsingDecl(cast<UsingDecl>(D));
117  return;
118  case Decl::UsingPack:
119  for (auto *Using : cast<UsingPackDecl>(D).expansions())
120  EmitDecl(*Using);
121  return;
122  case Decl::UsingDirective: // using namespace X; [C++]
123  if (CGDebugInfo *DI = getDebugInfo())
124  DI->EmitUsingDirective(cast<UsingDirectiveDecl>(D));
125  return;
126  case Decl::Var:
127  case Decl::Decomposition: {
128  const VarDecl &VD = cast<VarDecl>(D);
129  assert(VD.isLocalVarDecl() &&
130  "Should not see file-scope variables inside a function!");
131  EmitVarDecl(VD);
132  if (auto *DD = dyn_cast<DecompositionDecl>(&VD))
133  for (auto *B : DD->bindings())
134  if (auto *HD = B->getHoldingVar())
135  EmitVarDecl(*HD);
136  return;
137  }
138 
139  case Decl::OMPDeclareReduction:
140  return CGM.EmitOMPDeclareReduction(cast<OMPDeclareReductionDecl>(&D), this);
141 
142  case Decl::Typedef: // typedef int X;
143  case Decl::TypeAlias: { // using X = int; [C++0x]
144  const TypedefNameDecl &TD = cast<TypedefNameDecl>(D);
145  QualType Ty = TD.getUnderlyingType();
146 
147  if (Ty->isVariablyModifiedType())
149  }
150  }
151 }
152 
153 /// EmitVarDecl - This method handles emission of any variable declaration
154 /// inside a function, including static vars etc.
156  if (D.hasExternalStorage())
157  // Don't emit it now, allow it to be emitted lazily on its first use.
158  return;
159 
160  // Some function-scope variable does not have static storage but still
161  // needs to be emitted like a static variable, e.g. a function-scope
162  // variable in constant address space in OpenCL.
163  if (D.getStorageDuration() != SD_Automatic) {
164  llvm::GlobalValue::LinkageTypes Linkage =
165  CGM.getLLVMLinkageVarDefinition(&D, /*isConstant=*/false);
166 
167  // FIXME: We need to force the emission/use of a guard variable for
168  // some variables even if we can constant-evaluate them because
169  // we can't guarantee every translation unit will constant-evaluate them.
170 
171  return EmitStaticVarDecl(D, Linkage);
172  }
173 
176 
177  assert(D.hasLocalStorage());
178  return EmitAutoVarDecl(D);
179 }
180 
181 static std::string getStaticDeclName(CodeGenModule &CGM, const VarDecl &D) {
182  if (CGM.getLangOpts().CPlusPlus)
183  return CGM.getMangledName(&D).str();
184 
185  // If this isn't C++, we don't need a mangled name, just a pretty one.
186  assert(!D.isExternallyVisible() && "name shouldn't matter");
187  std::string ContextName;
188  const DeclContext *DC = D.getDeclContext();
189  if (auto *CD = dyn_cast<CapturedDecl>(DC))
190  DC = cast<DeclContext>(CD->getNonClosureContext());
191  if (const auto *FD = dyn_cast<FunctionDecl>(DC))
192  ContextName = CGM.getMangledName(FD);
193  else if (const auto *BD = dyn_cast<BlockDecl>(DC))
194  ContextName = CGM.getBlockMangledName(GlobalDecl(), BD);
195  else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(DC))
196  ContextName = OMD->getSelector().getAsString();
197  else
198  llvm_unreachable("Unknown context for static var decl");
199 
200  ContextName += "." + D.getNameAsString();
201  return ContextName;
202 }
203 
205  const VarDecl &D, llvm::GlobalValue::LinkageTypes Linkage) {
206  // In general, we don't always emit static var decls once before we reference
207  // them. It is possible to reference them before emitting the function that
208  // contains them, and it is possible to emit the containing function multiple
209  // times.
210  if (llvm::Constant *ExistingGV = StaticLocalDeclMap[&D])
211  return ExistingGV;
212 
213  QualType Ty = D.getType();
214  assert(Ty->isConstantSizeType() && "VLAs can't be static");
215 
216  // Use the label if the variable is renamed with the asm-label extension.
217  std::string Name;
218  if (D.hasAttr<AsmLabelAttr>())
219  Name = getMangledName(&D);
220  else
221  Name = getStaticDeclName(*this, D);
222 
224  unsigned AS = GetGlobalVarAddressSpace(&D);
225  unsigned TargetAS = getContext().getTargetAddressSpace(AS);
226 
227  // Local address space cannot have an initializer.
228  llvm::Constant *Init = nullptr;
230  Init = EmitNullConstant(Ty);
231  else
232  Init = llvm::UndefValue::get(LTy);
233 
234  llvm::GlobalVariable *GV = new llvm::GlobalVariable(
235  getModule(), LTy, Ty.isConstant(getContext()), Linkage, Init, Name,
236  nullptr, llvm::GlobalVariable::NotThreadLocal, TargetAS);
237  GV->setAlignment(getContext().getDeclAlign(&D).getQuantity());
238  setGlobalVisibility(GV, &D);
239 
240  if (supportsCOMDAT() && GV->isWeakForLinker())
241  GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
242 
243  if (D.getTLSKind())
244  setTLSMode(GV, D);
245 
246  if (D.isExternallyVisible()) {
247  if (D.hasAttr<DLLImportAttr>())
248  GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass);
249  else if (D.hasAttr<DLLExportAttr>())
250  GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass);
251  }
252 
253  // Make sure the result is of the correct type.
254  unsigned ExpectedAS = Ty.getAddressSpace();
255  llvm::Constant *Addr = GV;
256  if (AS != ExpectedAS) {
258  *this, GV, AS, ExpectedAS,
259  LTy->getPointerTo(getContext().getTargetAddressSpace(ExpectedAS)));
260  }
261 
262  setStaticLocalDeclAddress(&D, Addr);
263 
264  // Ensure that the static local gets initialized by making sure the parent
265  // function gets emitted eventually.
266  const Decl *DC = cast<Decl>(D.getDeclContext());
267 
268  // We can't name blocks or captured statements directly, so try to emit their
269  // parents.
270  if (isa<BlockDecl>(DC) || isa<CapturedDecl>(DC)) {
271  DC = DC->getNonClosureContext();
272  // FIXME: Ensure that global blocks get emitted.
273  if (!DC)
274  return Addr;
275  }
276 
277  GlobalDecl GD;
278  if (const auto *CD = dyn_cast<CXXConstructorDecl>(DC))
279  GD = GlobalDecl(CD, Ctor_Base);
280  else if (const auto *DD = dyn_cast<CXXDestructorDecl>(DC))
281  GD = GlobalDecl(DD, Dtor_Base);
282  else if (const auto *FD = dyn_cast<FunctionDecl>(DC))
283  GD = GlobalDecl(FD);
284  else {
285  // Don't do anything for Obj-C method decls or global closures. We should
286  // never defer them.
287  assert(isa<ObjCMethodDecl>(DC) && "unexpected parent code decl");
288  }
289  if (GD.getDecl())
290  (void)GetAddrOfGlobal(GD);
291 
292  return Addr;
293 }
294 
295 /// hasNontrivialDestruction - Determine whether a type's destruction is
296 /// non-trivial. If so, and the variable uses static initialization, we must
297 /// register its destructor to run on exit.
300  return RD && !RD->hasTrivialDestructor();
301 }
302 
303 /// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the
304 /// global variable that has already been created for it. If the initializer
305 /// has a different type than GV does, this may free GV and return a different
306 /// one. Otherwise it just returns GV.
307 llvm::GlobalVariable *
309  llvm::GlobalVariable *GV) {
310  llvm::Constant *Init = CGM.EmitConstantInit(D, this);
311 
312  // If constant emission failed, then this should be a C++ static
313  // initializer.
314  if (!Init) {
315  if (!getLangOpts().CPlusPlus)
316  CGM.ErrorUnsupported(D.getInit(), "constant l-value expression");
317  else if (HaveInsertPoint()) {
318  // Since we have a static initializer, this global variable can't
319  // be constant.
320  GV->setConstant(false);
321 
322  EmitCXXGuardedInit(D, GV, /*PerformInit*/true);
323  }
324  return GV;
325  }
326 
327  // The initializer may differ in type from the global. Rewrite
328  // the global to match the initializer. (We have to do this
329  // because some types, like unions, can't be completely represented
330  // in the LLVM type system.)
331  if (GV->getType()->getElementType() != Init->getType()) {
332  llvm::GlobalVariable *OldGV = GV;
333 
334  GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
335  OldGV->isConstant(),
336  OldGV->getLinkage(), Init, "",
337  /*InsertBefore*/ OldGV,
338  OldGV->getThreadLocalMode(),
340  GV->setVisibility(OldGV->getVisibility());
341  GV->setComdat(OldGV->getComdat());
342 
343  // Steal the name of the old global
344  GV->takeName(OldGV);
345 
346  // Replace all uses of the old global with the new global
347  llvm::Constant *NewPtrForOldDecl =
348  llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
349  OldGV->replaceAllUsesWith(NewPtrForOldDecl);
350 
351  // Erase the old global, since it is no longer used.
352  OldGV->eraseFromParent();
353  }
354 
355  GV->setConstant(CGM.isTypeConstant(D.getType(), true));
356  GV->setInitializer(Init);
357 
359  // We have a constant initializer, but a nontrivial destructor. We still
360  // need to perform a guarded "initialization" in order to register the
361  // destructor.
362  EmitCXXGuardedInit(D, GV, /*PerformInit*/false);
363  }
364 
365  return GV;
366 }
367 
369  llvm::GlobalValue::LinkageTypes Linkage) {
370  // Check to see if we already have a global variable for this
371  // declaration. This can happen when double-emitting function
372  // bodies, e.g. with complete and base constructors.
373  llvm::Constant *addr = CGM.getOrCreateStaticVarDecl(D, Linkage);
374  CharUnits alignment = getContext().getDeclAlign(&D);
375 
376  // Store into LocalDeclMap before generating initializer to handle
377  // circular references.
378  setAddrOfLocalVar(&D, Address(addr, alignment));
379 
380  // We can't have a VLA here, but we can have a pointer to a VLA,
381  // even though that doesn't really make any sense.
382  // Make sure to evaluate VLA bounds now so that we have them for later.
383  if (D.getType()->isVariablyModifiedType())
385 
386  // Save the type in case adding the initializer forces a type change.
387  llvm::Type *expectedType = addr->getType();
388 
389  llvm::GlobalVariable *var =
390  cast<llvm::GlobalVariable>(addr->stripPointerCasts());
391 
392  // CUDA's local and local static __shared__ variables should not
393  // have any non-empty initializers. This is ensured by Sema.
394  // Whatever initializer such variable may have when it gets here is
395  // a no-op and should not be emitted.
396  bool isCudaSharedVar = getLangOpts().CUDA && getLangOpts().CUDAIsDevice &&
397  D.hasAttr<CUDASharedAttr>();
398  // If this value has an initializer, emit it.
399  if (D.getInit() && !isCudaSharedVar)
400  var = AddInitializerToStaticVarDecl(D, var);
401 
402  var->setAlignment(alignment.getQuantity());
403 
404  if (D.hasAttr<AnnotateAttr>())
405  CGM.AddGlobalAnnotations(&D, var);
406 
407  if (auto *SA = D.getAttr<PragmaClangBSSSectionAttr>())
408  var->addAttribute("bss-section", SA->getName());
409  if (auto *SA = D.getAttr<PragmaClangDataSectionAttr>())
410  var->addAttribute("data-section", SA->getName());
411  if (auto *SA = D.getAttr<PragmaClangRodataSectionAttr>())
412  var->addAttribute("rodata-section", SA->getName());
413 
414  if (const SectionAttr *SA = D.getAttr<SectionAttr>())
415  var->setSection(SA->getName());
416 
417  if (D.hasAttr<UsedAttr>())
418  CGM.addUsedGlobal(var);
419 
420  // We may have to cast the constant because of the initializer
421  // mismatch above.
422  //
423  // FIXME: It is really dangerous to store this in the map; if anyone
424  // RAUW's the GV uses of this constant will be invalid.
425  llvm::Constant *castedAddr =
426  llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(var, expectedType);
427  if (var != castedAddr)
428  LocalDeclMap.find(&D)->second = Address(castedAddr, alignment);
429  CGM.setStaticLocalDeclAddress(&D, castedAddr);
430 
432 
433  // Emit global variable debug descriptor for static vars.
434  CGDebugInfo *DI = getDebugInfo();
435  if (DI &&
437  DI->setLocation(D.getLocation());
438  DI->EmitGlobalVariable(var, &D);
439  }
440 }
441 
442 namespace {
443  struct DestroyObject final : EHScopeStack::Cleanup {
444  DestroyObject(Address addr, QualType type,
445  CodeGenFunction::Destroyer *destroyer,
446  bool useEHCleanupForArray)
447  : addr(addr), type(type), destroyer(destroyer),
448  useEHCleanupForArray(useEHCleanupForArray) {}
449 
450  Address addr;
451  QualType type;
452  CodeGenFunction::Destroyer *destroyer;
453  bool useEHCleanupForArray;
454 
455  void Emit(CodeGenFunction &CGF, Flags flags) override {
456  // Don't use an EH cleanup recursively from an EH cleanup.
457  bool useEHCleanupForArray =
458  flags.isForNormalCleanup() && this->useEHCleanupForArray;
459 
460  CGF.emitDestroy(addr, type, destroyer, useEHCleanupForArray);
461  }
462  };
463 
464  struct DestroyNRVOVariable final : EHScopeStack::Cleanup {
465  DestroyNRVOVariable(Address addr,
466  const CXXDestructorDecl *Dtor,
467  llvm::Value *NRVOFlag)
468  : Dtor(Dtor), NRVOFlag(NRVOFlag), Loc(addr) {}
469 
470  const CXXDestructorDecl *Dtor;
471  llvm::Value *NRVOFlag;
472  Address Loc;
473 
474  void Emit(CodeGenFunction &CGF, Flags flags) override {
475  // Along the exceptions path we always execute the dtor.
476  bool NRVO = flags.isForNormalCleanup() && NRVOFlag;
477 
478  llvm::BasicBlock *SkipDtorBB = nullptr;
479  if (NRVO) {
480  // If we exited via NRVO, we skip the destructor call.
481  llvm::BasicBlock *RunDtorBB = CGF.createBasicBlock("nrvo.unused");
482  SkipDtorBB = CGF.createBasicBlock("nrvo.skipdtor");
483  llvm::Value *DidNRVO =
484  CGF.Builder.CreateFlagLoad(NRVOFlag, "nrvo.val");
485  CGF.Builder.CreateCondBr(DidNRVO, SkipDtorBB, RunDtorBB);
486  CGF.EmitBlock(RunDtorBB);
487  }
488 
490  /*ForVirtualBase=*/false,
491  /*Delegating=*/false,
492  Loc);
493 
494  if (NRVO) CGF.EmitBlock(SkipDtorBB);
495  }
496  };
497 
498  struct CallStackRestore final : EHScopeStack::Cleanup {
499  Address Stack;
500  CallStackRestore(Address Stack) : Stack(Stack) {}
501  void Emit(CodeGenFunction &CGF, Flags flags) override {
503  llvm::Value *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stackrestore);
504  CGF.Builder.CreateCall(F, V);
505  }
506  };
507 
508  struct ExtendGCLifetime final : EHScopeStack::Cleanup {
509  const VarDecl &Var;
510  ExtendGCLifetime(const VarDecl *var) : Var(*var) {}
511 
512  void Emit(CodeGenFunction &CGF, Flags flags) override {
513  // Compute the address of the local variable, in case it's a
514  // byref or something.
515  DeclRefExpr DRE(const_cast<VarDecl*>(&Var), false,
516  Var.getType(), VK_LValue, SourceLocation());
517  llvm::Value *value = CGF.EmitLoadOfScalar(CGF.EmitDeclRefLValue(&DRE),
518  SourceLocation());
519  CGF.EmitExtendGCLifetime(value);
520  }
521  };
522 
523  struct CallCleanupFunction final : EHScopeStack::Cleanup {
524  llvm::Constant *CleanupFn;
525  const CGFunctionInfo &FnInfo;
526  const VarDecl &Var;
527 
528  CallCleanupFunction(llvm::Constant *CleanupFn, const CGFunctionInfo *Info,
529  const VarDecl *Var)
530  : CleanupFn(CleanupFn), FnInfo(*Info), Var(*Var) {}
531 
532  void Emit(CodeGenFunction &CGF, Flags flags) override {
533  DeclRefExpr DRE(const_cast<VarDecl*>(&Var), false,
534  Var.getType(), VK_LValue, SourceLocation());
535  // Compute the address of the local variable, in case it's a byref
536  // or something.
537  llvm::Value *Addr = CGF.EmitDeclRefLValue(&DRE).getPointer();
538 
539  // In some cases, the type of the function argument will be different from
540  // the type of the pointer. An example of this is
541  // void f(void* arg);
542  // __attribute__((cleanup(f))) void *g;
543  //
544  // To fix this we insert a bitcast here.
545  QualType ArgTy = FnInfo.arg_begin()->type;
546  llvm::Value *Arg =
547  CGF.Builder.CreateBitCast(Addr, CGF.ConvertType(ArgTy));
548 
549  CallArgList Args;
550  Args.add(RValue::get(Arg),
551  CGF.getContext().getPointerType(Var.getType()));
552  auto Callee = CGCallee::forDirect(CleanupFn);
553  CGF.EmitCall(FnInfo, Callee, ReturnValueSlot(), Args);
554  }
555  };
556 } // end anonymous namespace
557 
558 /// EmitAutoVarWithLifetime - Does the setup required for an automatic
559 /// variable with lifetime.
560 static void EmitAutoVarWithLifetime(CodeGenFunction &CGF, const VarDecl &var,
561  Address addr,
562  Qualifiers::ObjCLifetime lifetime) {
563  switch (lifetime) {
565  llvm_unreachable("present but none");
566 
568  // nothing to do
569  break;
570 
571  case Qualifiers::OCL_Strong: {
572  CodeGenFunction::Destroyer *destroyer =
573  (var.hasAttr<ObjCPreciseLifetimeAttr>()
576 
577  CleanupKind cleanupKind = CGF.getARCCleanupKind();
578  CGF.pushDestroy(cleanupKind, addr, var.getType(), destroyer,
579  cleanupKind & EHCleanup);
580  break;
581  }
583  // nothing to do
584  break;
585 
587  // __weak objects always get EH cleanups; otherwise, exceptions
588  // could cause really nasty crashes instead of mere leaks.
589  CGF.pushDestroy(NormalAndEHCleanup, addr, var.getType(),
591  /*useEHCleanup*/ true);
592  break;
593  }
594 }
595 
596 static bool isAccessedBy(const VarDecl &var, const Stmt *s) {
597  if (const Expr *e = dyn_cast<Expr>(s)) {
598  // Skip the most common kinds of expressions that make
599  // hierarchy-walking expensive.
600  s = e = e->IgnoreParenCasts();
601 
602  if (const DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e))
603  return (ref->getDecl() == &var);
604  if (const BlockExpr *be = dyn_cast<BlockExpr>(e)) {
605  const BlockDecl *block = be->getBlockDecl();
606  for (const auto &I : block->captures()) {
607  if (I.getVariable() == &var)
608  return true;
609  }
610  }
611  }
612 
613  for (const Stmt *SubStmt : s->children())
614  // SubStmt might be null; as in missing decl or conditional of an if-stmt.
615  if (SubStmt && isAccessedBy(var, SubStmt))
616  return true;
617 
618  return false;
619 }
620 
621 static bool isAccessedBy(const ValueDecl *decl, const Expr *e) {
622  if (!decl) return false;
623  if (!isa<VarDecl>(decl)) return false;
624  const VarDecl *var = cast<VarDecl>(decl);
625  return isAccessedBy(*var, e);
626 }
627 
629  const LValue &destLV, const Expr *init) {
630  bool needsCast = false;
631 
632  while (auto castExpr = dyn_cast<CastExpr>(init->IgnoreParens())) {
633  switch (castExpr->getCastKind()) {
634  // Look through casts that don't require representation changes.
635  case CK_NoOp:
636  case CK_BitCast:
637  case CK_BlockPointerToObjCPointerCast:
638  needsCast = true;
639  break;
640 
641  // If we find an l-value to r-value cast from a __weak variable,
642  // emit this operation as a copy or move.
643  case CK_LValueToRValue: {
644  const Expr *srcExpr = castExpr->getSubExpr();
645  if (srcExpr->getType().getObjCLifetime() != Qualifiers::OCL_Weak)
646  return false;
647 
648  // Emit the source l-value.
649  LValue srcLV = CGF.EmitLValue(srcExpr);
650 
651  // Handle a formal type change to avoid asserting.
652  auto srcAddr = srcLV.getAddress();
653  if (needsCast) {
654  srcAddr = CGF.Builder.CreateElementBitCast(srcAddr,
655  destLV.getAddress().getElementType());
656  }
657 
658  // If it was an l-value, use objc_copyWeak.
659  if (srcExpr->getValueKind() == VK_LValue) {
660  CGF.EmitARCCopyWeak(destLV.getAddress(), srcAddr);
661  } else {
662  assert(srcExpr->getValueKind() == VK_XValue);
663  CGF.EmitARCMoveWeak(destLV.getAddress(), srcAddr);
664  }
665  return true;
666  }
667 
668  // Stop at anything else.
669  default:
670  return false;
671  }
672 
673  init = castExpr->getSubExpr();
674  }
675  return false;
676 }
677 
679  LValue &lvalue,
680  const VarDecl *var) {
681  lvalue.setAddress(CGF.emitBlockByrefAddress(lvalue.getAddress(), var));
682 }
683 
685  SourceLocation Loc) {
686  if (!SanOpts.has(SanitizerKind::NullabilityAssign))
687  return;
688 
689  auto Nullability = LHS.getType()->getNullability(getContext());
691  return;
692 
693  // Check if the right hand side of the assignment is nonnull, if the left
694  // hand side must be nonnull.
695  SanitizerScope SanScope(this);
696  llvm::Value *IsNotNull = Builder.CreateIsNotNull(RHS);
697  llvm::Constant *StaticData[] = {
699  llvm::ConstantInt::get(Int8Ty, 0), // The LogAlignment info is unused.
700  llvm::ConstantInt::get(Int8Ty, TCK_NonnullAssign)};
701  EmitCheck({{IsNotNull, SanitizerKind::NullabilityAssign}},
702  SanitizerHandler::TypeMismatch, StaticData, RHS);
703 }
704 
705 void CodeGenFunction::EmitScalarInit(const Expr *init, const ValueDecl *D,
706  LValue lvalue, bool capturedByInit) {
707  Qualifiers::ObjCLifetime lifetime = lvalue.getObjCLifetime();
708  if (!lifetime) {
709  llvm::Value *value = EmitScalarExpr(init);
710  if (capturedByInit)
711  drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
712  EmitNullabilityCheck(lvalue, value, init->getExprLoc());
713  EmitStoreThroughLValue(RValue::get(value), lvalue, true);
714  return;
715  }
716 
717  if (const CXXDefaultInitExpr *DIE = dyn_cast<CXXDefaultInitExpr>(init))
718  init = DIE->getExpr();
719 
720  // If we're emitting a value with lifetime, we have to do the
721  // initialization *before* we leave the cleanup scopes.
722  if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(init)) {
723  enterFullExpression(ewc);
724  init = ewc->getSubExpr();
725  }
727 
728  // We have to maintain the illusion that the variable is
729  // zero-initialized. If the variable might be accessed in its
730  // initializer, zero-initialize before running the initializer, then
731  // actually perform the initialization with an assign.
732  bool accessedByInit = false;
733  if (lifetime != Qualifiers::OCL_ExplicitNone)
734  accessedByInit = (capturedByInit || isAccessedBy(D, init));
735  if (accessedByInit) {
736  LValue tempLV = lvalue;
737  // Drill down to the __block object if necessary.
738  if (capturedByInit) {
739  // We can use a simple GEP for this because it can't have been
740  // moved yet.
742  cast<VarDecl>(D),
743  /*follow*/ false));
744  }
745 
746  auto ty = cast<llvm::PointerType>(tempLV.getAddress().getElementType());
747  llvm::Value *zero = CGM.getNullPointer(ty, tempLV.getType());
748 
749  // If __weak, we want to use a barrier under certain conditions.
750  if (lifetime == Qualifiers::OCL_Weak)
751  EmitARCInitWeak(tempLV.getAddress(), zero);
752 
753  // Otherwise just do a simple store.
754  else
755  EmitStoreOfScalar(zero, tempLV, /* isInitialization */ true);
756  }
757 
758  // Emit the initializer.
759  llvm::Value *value = nullptr;
760 
761  switch (lifetime) {
763  llvm_unreachable("present but none");
764 
766  value = EmitARCUnsafeUnretainedScalarExpr(init);
767  break;
768 
769  case Qualifiers::OCL_Strong: {
770  value = EmitARCRetainScalarExpr(init);
771  break;
772  }
773 
774  case Qualifiers::OCL_Weak: {
775  // If it's not accessed by the initializer, try to emit the
776  // initialization with a copy or move.
777  if (!accessedByInit && tryEmitARCCopyWeakInit(*this, lvalue, init)) {
778  return;
779  }
780 
781  // No way to optimize a producing initializer into this. It's not
782  // worth optimizing for, because the value will immediately
783  // disappear in the common case.
784  value = EmitScalarExpr(init);
785 
786  if (capturedByInit) drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
787  if (accessedByInit)
788  EmitARCStoreWeak(lvalue.getAddress(), value, /*ignored*/ true);
789  else
790  EmitARCInitWeak(lvalue.getAddress(), value);
791  return;
792  }
793 
796  break;
797  }
798 
799  if (capturedByInit) drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
800 
801  EmitNullabilityCheck(lvalue, value, init->getExprLoc());
802 
803  // If the variable might have been accessed by its initializer, we
804  // might have to initialize with a barrier. We have to do this for
805  // both __weak and __strong, but __weak got filtered out above.
806  if (accessedByInit && lifetime == Qualifiers::OCL_Strong) {
807  llvm::Value *oldValue = EmitLoadOfScalar(lvalue, init->getExprLoc());
808  EmitStoreOfScalar(value, lvalue, /* isInitialization */ true);
810  return;
811  }
812 
813  EmitStoreOfScalar(value, lvalue, /* isInitialization */ true);
814 }
815 
816 /// canEmitInitWithFewStoresAfterMemset - Decide whether we can emit the
817 /// non-zero parts of the specified initializer with equal or fewer than
818 /// NumStores scalar stores.
819 static bool canEmitInitWithFewStoresAfterMemset(llvm::Constant *Init,
820  unsigned &NumStores) {
821  // Zero and Undef never requires any extra stores.
822  if (isa<llvm::ConstantAggregateZero>(Init) ||
823  isa<llvm::ConstantPointerNull>(Init) ||
824  isa<llvm::UndefValue>(Init))
825  return true;
826  if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) ||
827  isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) ||
828  isa<llvm::ConstantExpr>(Init))
829  return Init->isNullValue() || NumStores--;
830 
831  // See if we can emit each element.
832  if (isa<llvm::ConstantArray>(Init) || isa<llvm::ConstantStruct>(Init)) {
833  for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) {
834  llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i));
835  if (!canEmitInitWithFewStoresAfterMemset(Elt, NumStores))
836  return false;
837  }
838  return true;
839  }
840 
841  if (llvm::ConstantDataSequential *CDS =
842  dyn_cast<llvm::ConstantDataSequential>(Init)) {
843  for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
844  llvm::Constant *Elt = CDS->getElementAsConstant(i);
845  if (!canEmitInitWithFewStoresAfterMemset(Elt, NumStores))
846  return false;
847  }
848  return true;
849  }
850 
851  // Anything else is hard and scary.
852  return false;
853 }
854 
855 /// emitStoresForInitAfterMemset - For inits that
856 /// canEmitInitWithFewStoresAfterMemset returned true for, emit the scalar
857 /// stores that would be required.
858 static void emitStoresForInitAfterMemset(llvm::Constant *Init, llvm::Value *Loc,
859  bool isVolatile, CGBuilderTy &Builder) {
860  assert(!Init->isNullValue() && !isa<llvm::UndefValue>(Init) &&
861  "called emitStoresForInitAfterMemset for zero or undef value.");
862 
863  if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) ||
864  isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) ||
865  isa<llvm::ConstantExpr>(Init)) {
866  Builder.CreateDefaultAlignedStore(Init, Loc, isVolatile);
867  return;
868  }
869 
870  if (llvm::ConstantDataSequential *CDS =
871  dyn_cast<llvm::ConstantDataSequential>(Init)) {
872  for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
873  llvm::Constant *Elt = CDS->getElementAsConstant(i);
874 
875  // If necessary, get a pointer to the element and emit it.
876  if (!Elt->isNullValue() && !isa<llvm::UndefValue>(Elt))
878  Elt, Builder.CreateConstGEP2_32(Init->getType(), Loc, 0, i),
879  isVolatile, Builder);
880  }
881  return;
882  }
883 
884  assert((isa<llvm::ConstantStruct>(Init) || isa<llvm::ConstantArray>(Init)) &&
885  "Unknown value type!");
886 
887  for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) {
888  llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i));
889 
890  // If necessary, get a pointer to the element and emit it.
891  if (!Elt->isNullValue() && !isa<llvm::UndefValue>(Elt))
893  Elt, Builder.CreateConstGEP2_32(Init->getType(), Loc, 0, i),
894  isVolatile, Builder);
895  }
896 }
897 
898 /// shouldUseMemSetPlusStoresToInitialize - Decide whether we should use memset
899 /// plus some stores to initialize a local variable instead of using a memcpy
900 /// from a constant global. It is beneficial to use memset if the global is all
901 /// zeros, or mostly zeros and large.
902 static bool shouldUseMemSetPlusStoresToInitialize(llvm::Constant *Init,
903  uint64_t GlobalSize) {
904  // If a global is all zeros, always use a memset.
905  if (isa<llvm::ConstantAggregateZero>(Init)) return true;
906 
907  // If a non-zero global is <= 32 bytes, always use a memcpy. If it is large,
908  // do it if it will require 6 or fewer scalar stores.
909  // TODO: Should budget depends on the size? Avoiding a large global warrants
910  // plopping in more stores.
911  unsigned StoreBudget = 6;
912  uint64_t SizeLimit = 32;
913 
914  return GlobalSize > SizeLimit &&
915  canEmitInitWithFewStoresAfterMemset(Init, StoreBudget);
916 }
917 
918 /// EmitAutoVarDecl - Emit code and set up an entry in LocalDeclMap for a
919 /// variable declaration with auto, register, or no storage class specifier.
920 /// These turn into simple stack objects, or GlobalValues depending on target.
922  AutoVarEmission emission = EmitAutoVarAlloca(D);
923  EmitAutoVarInit(emission);
924  EmitAutoVarCleanups(emission);
925 }
926 
927 /// Emit a lifetime.begin marker if some criteria are satisfied.
928 /// \return a pointer to the temporary size Value if a marker was emitted, null
929 /// otherwise
931  llvm::Value *Addr) {
932  if (!ShouldEmitLifetimeMarkers)
933  return nullptr;
934 
935  llvm::Value *SizeV = llvm::ConstantInt::get(Int64Ty, Size);
936  Addr = Builder.CreateBitCast(Addr, AllocaInt8PtrTy);
937  llvm::CallInst *C =
938  Builder.CreateCall(CGM.getLLVMLifetimeStartFn(), {SizeV, Addr});
939  C->setDoesNotThrow();
940  return SizeV;
941 }
942 
944  Addr = Builder.CreateBitCast(Addr, AllocaInt8PtrTy);
945  llvm::CallInst *C =
946  Builder.CreateCall(CGM.getLLVMLifetimeEndFn(), {Size, Addr});
947  C->setDoesNotThrow();
948 }
949 
950 /// EmitAutoVarAlloca - Emit the alloca and debug information for a
951 /// local variable. Does not emit initialization or destruction.
954  QualType Ty = D.getType();
955  assert(Ty.getAddressSpace() == LangAS::Default);
956 
957  AutoVarEmission emission(D);
958 
959  bool isByRef = D.hasAttr<BlocksAttr>();
960  emission.IsByRef = isByRef;
961 
962  CharUnits alignment = getContext().getDeclAlign(&D);
963 
964  // If the type is variably-modified, emit all the VLA sizes for it.
965  if (Ty->isVariablyModifiedType())
967 
968  Address address = Address::invalid();
969  if (Ty->isConstantSizeType()) {
970  bool NRVO = getLangOpts().ElideConstructors &&
971  D.isNRVOVariable();
972 
973  // If this value is an array or struct with a statically determinable
974  // constant initializer, there are optimizations we can do.
975  //
976  // TODO: We should constant-evaluate the initializer of any variable,
977  // as long as it is initialized by a constant expression. Currently,
978  // isConstantInitializer produces wrong answers for structs with
979  // reference or bitfield members, and a few other cases, and checking
980  // for POD-ness protects us from some of these.
981  if (D.getInit() && (Ty->isArrayType() || Ty->isRecordType()) &&
982  (D.isConstexpr() ||
983  ((Ty.isPODType(getContext()) ||
985  D.getInit()->isConstantInitializer(getContext(), false)))) {
986 
987  // If the variable's a const type, and it's neither an NRVO
988  // candidate nor a __block variable and has no mutable members,
989  // emit it as a global instead.
990  // Exception is if a variable is located in non-constant address space
991  // in OpenCL.
992  if ((!getLangOpts().OpenCL ||
994  (CGM.getCodeGenOpts().MergeAllConstants && !NRVO && !isByRef &&
995  CGM.isTypeConstant(Ty, true))) {
997 
998  // Signal this condition to later callbacks.
999  emission.Addr = Address::invalid();
1000  assert(emission.wasEmittedAsGlobal());
1001  return emission;
1002  }
1003 
1004  // Otherwise, tell the initialization code that we're in this case.
1005  emission.IsConstantAggregate = true;
1006  }
1007 
1008  // A normal fixed sized variable becomes an alloca in the entry block,
1009  // unless it's an NRVO variable.
1010 
1011  if (NRVO) {
1012  // The named return value optimization: allocate this variable in the
1013  // return slot, so that we can elide the copy when returning this
1014  // variable (C++0x [class.copy]p34).
1015  address = ReturnValue;
1016 
1017  if (const RecordType *RecordTy = Ty->getAs<RecordType>()) {
1018  if (!cast<CXXRecordDecl>(RecordTy->getDecl())->hasTrivialDestructor()) {
1019  // Create a flag that is used to indicate when the NRVO was applied
1020  // to this variable. Set it to zero to indicate that NRVO was not
1021  // applied.
1022  llvm::Value *Zero = Builder.getFalse();
1023  Address NRVOFlag =
1024  CreateTempAlloca(Zero->getType(), CharUnits::One(), "nrvo");
1026  Builder.CreateStore(Zero, NRVOFlag);
1027 
1028  // Record the NRVO flag for this variable.
1029  NRVOFlags[&D] = NRVOFlag.getPointer();
1030  emission.NRVOFlag = NRVOFlag.getPointer();
1031  }
1032  }
1033  } else {
1034  CharUnits allocaAlignment;
1035  llvm::Type *allocaTy;
1036  if (isByRef) {
1037  auto &byrefInfo = getBlockByrefInfo(&D);
1038  allocaTy = byrefInfo.Type;
1039  allocaAlignment = byrefInfo.ByrefAlignment;
1040  } else {
1041  allocaTy = ConvertTypeForMem(Ty);
1042  allocaAlignment = alignment;
1043  }
1044 
1045  // Create the alloca. Note that we set the name separately from
1046  // building the instruction so that it's there even in no-asserts
1047  // builds.
1048  address = CreateTempAlloca(allocaTy, allocaAlignment, D.getName());
1049 
1050  // Don't emit lifetime markers for MSVC catch parameters. The lifetime of
1051  // the catch parameter starts in the catchpad instruction, and we can't
1052  // insert code in those basic blocks.
1053  bool IsMSCatchParam =
1055 
1056  // Emit a lifetime intrinsic if meaningful. There's no point in doing this
1057  // if we don't have a valid insertion point (?).
1058  if (HaveInsertPoint() && !IsMSCatchParam) {
1059  // If there's a jump into the lifetime of this variable, its lifetime
1060  // gets broken up into several regions in IR, which requires more work
1061  // to handle correctly. For now, just omit the intrinsics; this is a
1062  // rare case, and it's better to just be conservatively correct.
1063  // PR28267.
1064  //
1065  // We have to do this in all language modes if there's a jump past the
1066  // declaration. We also have to do it in C if there's a jump to an
1067  // earlier point in the current block because non-VLA lifetimes begin as
1068  // soon as the containing block is entered, not when its variables
1069  // actually come into scope; suppressing the lifetime annotations
1070  // completely in this case is unnecessarily pessimistic, but again, this
1071  // is rare.
1072  if (!Bypasses.IsBypassed(&D) &&
1073  !(!getLangOpts().CPlusPlus && hasLabelBeenSeenInCurrentScope())) {
1074  uint64_t size = CGM.getDataLayout().getTypeAllocSize(allocaTy);
1075  emission.SizeForLifetimeMarkers =
1076  EmitLifetimeStart(size, address.getPointer());
1077  }
1078  } else {
1079  assert(!emission.useLifetimeMarkers());
1080  }
1081  }
1082  } else {
1084 
1085  if (!DidCallStackSave) {
1086  // Save the stack.
1087  Address Stack =
1088  CreateTempAlloca(Int8PtrTy, getPointerAlign(), "saved_stack");
1089 
1090  llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave);
1091  llvm::Value *V = Builder.CreateCall(F);
1092  Builder.CreateStore(V, Stack);
1093 
1094  DidCallStackSave = true;
1095 
1096  // Push a cleanup block and restore the stack there.
1097  // FIXME: in general circumstances, this should be an EH cleanup.
1099  }
1100 
1101  llvm::Value *elementCount;
1102  QualType elementType;
1103  std::tie(elementCount, elementType) = getVLASize(Ty);
1104 
1105  llvm::Type *llvmTy = ConvertTypeForMem(elementType);
1106 
1107  // Allocate memory for the array.
1108  address = CreateTempAlloca(llvmTy, alignment, "vla", elementCount);
1109  }
1110 
1111  setAddrOfLocalVar(&D, address);
1112  emission.Addr = address;
1113 
1114  // Emit debug info for local var declaration.
1115  if (HaveInsertPoint())
1116  if (CGDebugInfo *DI = getDebugInfo()) {
1117  if (CGM.getCodeGenOpts().getDebugInfo() >=
1119  DI->setLocation(D.getLocation());
1120  DI->EmitDeclareOfAutoVariable(&D, address.getPointer(), Builder);
1121  }
1122  }
1123 
1124  if (D.hasAttr<AnnotateAttr>())
1125  EmitVarAnnotations(&D, address.getPointer());
1126 
1127  // Make sure we call @llvm.lifetime.end.
1128  if (emission.useLifetimeMarkers())
1130  emission.getAllocatedAddress(),
1131  emission.getSizeForLifetimeMarkers());
1132 
1133  return emission;
1134 }
1135 
1136 /// Determines whether the given __block variable is potentially
1137 /// captured by the given expression.
1138 static bool isCapturedBy(const VarDecl &var, const Expr *e) {
1139  // Skip the most common kinds of expressions that make
1140  // hierarchy-walking expensive.
1141  e = e->IgnoreParenCasts();
1142 
1143  if (const BlockExpr *be = dyn_cast<BlockExpr>(e)) {
1144  const BlockDecl *block = be->getBlockDecl();
1145  for (const auto &I : block->captures()) {
1146  if (I.getVariable() == &var)
1147  return true;
1148  }
1149 
1150  // No need to walk into the subexpressions.
1151  return false;
1152  }
1153 
1154  if (const StmtExpr *SE = dyn_cast<StmtExpr>(e)) {
1155  const CompoundStmt *CS = SE->getSubStmt();
1156  for (const auto *BI : CS->body())
1157  if (const auto *E = dyn_cast<Expr>(BI)) {
1158  if (isCapturedBy(var, E))
1159  return true;
1160  }
1161  else if (const auto *DS = dyn_cast<DeclStmt>(BI)) {
1162  // special case declarations
1163  for (const auto *I : DS->decls()) {
1164  if (const auto *VD = dyn_cast<VarDecl>((I))) {
1165  const Expr *Init = VD->getInit();
1166  if (Init && isCapturedBy(var, Init))
1167  return true;
1168  }
1169  }
1170  }
1171  else
1172  // FIXME. Make safe assumption assuming arbitrary statements cause capturing.
1173  // Later, provide code to poke into statements for capture analysis.
1174  return true;
1175  return false;
1176  }
1177 
1178  for (const Stmt *SubStmt : e->children())
1179  if (isCapturedBy(var, cast<Expr>(SubStmt)))
1180  return true;
1181 
1182  return false;
1183 }
1184 
1185 /// \brief Determine whether the given initializer is trivial in the sense
1186 /// that it requires no code to be generated.
1188  if (!Init)
1189  return true;
1190 
1191  if (const CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init))
1192  if (CXXConstructorDecl *Constructor = Construct->getConstructor())
1193  if (Constructor->isTrivial() &&
1194  Constructor->isDefaultConstructor() &&
1195  !Construct->requiresZeroInitialization())
1196  return true;
1197 
1198  return false;
1199 }
1200 
1202  assert(emission.Variable && "emission was not valid!");
1203 
1204  // If this was emitted as a global constant, we're done.
1205  if (emission.wasEmittedAsGlobal()) return;
1206 
1207  const VarDecl &D = *emission.Variable;
1209  QualType type = D.getType();
1210 
1211  // If this local has an initializer, emit it now.
1212  const Expr *Init = D.getInit();
1213 
1214  // If we are at an unreachable point, we don't need to emit the initializer
1215  // unless it contains a label.
1216  if (!HaveInsertPoint()) {
1217  if (!Init || !ContainsLabel(Init)) return;
1219  }
1220 
1221  // Initialize the structure of a __block variable.
1222  if (emission.IsByRef)
1223  emitByrefStructureInit(emission);
1224 
1225  if (isTrivialInitializer(Init))
1226  return;
1227 
1228  // Check whether this is a byref variable that's potentially
1229  // captured and moved by its own initializer. If so, we'll need to
1230  // emit the initializer first, then copy into the variable.
1231  bool capturedByInit = emission.IsByRef && isCapturedBy(D, Init);
1232 
1233  Address Loc =
1234  capturedByInit ? emission.Addr : emission.getObjectAddress(*this);
1235 
1236  llvm::Constant *constant = nullptr;
1237  if (emission.IsConstantAggregate || D.isConstexpr()) {
1238  assert(!capturedByInit && "constant init contains a capturing block?");
1239  constant = CGM.EmitConstantInit(D, this);
1240  }
1241 
1242  if (!constant) {
1243  LValue lv = MakeAddrLValue(Loc, type);
1244  lv.setNonGC(true);
1245  return EmitExprAsInit(Init, &D, lv, capturedByInit);
1246  }
1247 
1248  if (!emission.IsConstantAggregate) {
1249  // For simple scalar/complex initialization, store the value directly.
1250  LValue lv = MakeAddrLValue(Loc, type);
1251  lv.setNonGC(true);
1252  return EmitStoreThroughLValue(RValue::get(constant), lv, true);
1253  }
1254 
1255  // If this is a simple aggregate initialization, we can optimize it
1256  // in various ways.
1257  bool isVolatile = type.isVolatileQualified();
1258 
1259  llvm::Value *SizeVal =
1260  llvm::ConstantInt::get(IntPtrTy,
1261  getContext().getTypeSizeInChars(type).getQuantity());
1262 
1263  llvm::Type *BP = Int8PtrTy;
1264  if (Loc.getType() != BP)
1265  Loc = Builder.CreateBitCast(Loc, BP);
1266 
1267  // If the initializer is all or mostly zeros, codegen with memset then do
1268  // a few stores afterward.
1270  CGM.getDataLayout().getTypeAllocSize(constant->getType()))) {
1271  Builder.CreateMemSet(Loc, llvm::ConstantInt::get(Int8Ty, 0), SizeVal,
1272  isVolatile);
1273  // Zero and undef don't require a stores.
1274  if (!constant->isNullValue() && !isa<llvm::UndefValue>(constant)) {
1275  Loc = Builder.CreateBitCast(Loc, constant->getType()->getPointerTo());
1276  emitStoresForInitAfterMemset(constant, Loc.getPointer(),
1277  isVolatile, Builder);
1278  }
1279  } else {
1280  // Otherwise, create a temporary global with the initializer then
1281  // memcpy from the global to the alloca.
1282  std::string Name = getStaticDeclName(CGM, D);
1283  unsigned AS = 0;
1284  if (getLangOpts().OpenCL) {
1286  BP = llvm::PointerType::getInt8PtrTy(getLLVMContext(), AS);
1287  }
1288  llvm::GlobalVariable *GV =
1289  new llvm::GlobalVariable(CGM.getModule(), constant->getType(), true,
1290  llvm::GlobalValue::PrivateLinkage,
1291  constant, Name, nullptr,
1292  llvm::GlobalValue::NotThreadLocal, AS);
1293  GV->setAlignment(Loc.getAlignment().getQuantity());
1294  GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
1295 
1296  Address SrcPtr = Address(GV, Loc.getAlignment());
1297  if (SrcPtr.getType() != BP)
1298  SrcPtr = Builder.CreateBitCast(SrcPtr, BP);
1299 
1300  Builder.CreateMemCpy(Loc, SrcPtr, SizeVal, isVolatile);
1301  }
1302 }
1303 
1304 /// Emit an expression as an initializer for a variable at the given
1305 /// location. The expression is not necessarily the normal
1306 /// initializer for the variable, and the address is not necessarily
1307 /// its normal location.
1308 ///
1309 /// \param init the initializing expression
1310 /// \param var the variable to act as if we're initializing
1311 /// \param loc the address to initialize; its type is a pointer
1312 /// to the LLVM mapping of the variable's type
1313 /// \param alignment the alignment of the address
1314 /// \param capturedByInit true if the variable is a __block variable
1315 /// whose address is potentially changed by the initializer
1317  LValue lvalue, bool capturedByInit) {
1318  QualType type = D->getType();
1319 
1320  if (type->isReferenceType()) {
1321  RValue rvalue = EmitReferenceBindingToExpr(init);
1322  if (capturedByInit)
1323  drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
1324  EmitStoreThroughLValue(rvalue, lvalue, true);
1325  return;
1326  }
1327  switch (getEvaluationKind(type)) {
1328  case TEK_Scalar:
1329  EmitScalarInit(init, D, lvalue, capturedByInit);
1330  return;
1331  case TEK_Complex: {
1332  ComplexPairTy complex = EmitComplexExpr(init);
1333  if (capturedByInit)
1334  drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
1335  EmitStoreOfComplex(complex, lvalue, /*init*/ true);
1336  return;
1337  }
1338  case TEK_Aggregate:
1339  if (type->isAtomicType()) {
1340  EmitAtomicInit(const_cast<Expr*>(init), lvalue);
1341  } else {
1342  // TODO: how can we delay here if D is captured by its initializer?
1343  EmitAggExpr(init, AggValueSlot::forLValue(lvalue,
1347  }
1348  return;
1349  }
1350  llvm_unreachable("bad evaluation kind");
1351 }
1352 
1353 /// Enter a destroy cleanup for the given local variable.
1355  const CodeGenFunction::AutoVarEmission &emission,
1356  QualType::DestructionKind dtorKind) {
1357  assert(dtorKind != QualType::DK_none);
1358 
1359  // Note that for __block variables, we want to destroy the
1360  // original stack object, not the possibly forwarded object.
1361  Address addr = emission.getObjectAddress(*this);
1362 
1363  const VarDecl *var = emission.Variable;
1364  QualType type = var->getType();
1365 
1366  CleanupKind cleanupKind = NormalAndEHCleanup;
1367  CodeGenFunction::Destroyer *destroyer = nullptr;
1368 
1369  switch (dtorKind) {
1370  case QualType::DK_none:
1371  llvm_unreachable("no cleanup for trivially-destructible variable");
1372 
1374  // If there's an NRVO flag on the emission, we need a different
1375  // cleanup.
1376  if (emission.NRVOFlag) {
1377  assert(!type->isArrayType());
1379  EHStack.pushCleanup<DestroyNRVOVariable>(cleanupKind, addr,
1380  dtor, emission.NRVOFlag);
1381  return;
1382  }
1383  break;
1384 
1386  // Suppress cleanups for pseudo-strong variables.
1387  if (var->isARCPseudoStrong()) return;
1388 
1389  // Otherwise, consider whether to use an EH cleanup or not.
1390  cleanupKind = getARCCleanupKind();
1391 
1392  // Use the imprecise destroyer by default.
1393  if (!var->hasAttr<ObjCPreciseLifetimeAttr>())
1395  break;
1396 
1398  break;
1399  }
1400 
1401  // If we haven't chosen a more specific destroyer, use the default.
1402  if (!destroyer) destroyer = getDestroyer(dtorKind);
1403 
1404  // Use an EH cleanup in array destructors iff the destructor itself
1405  // is being pushed as an EH cleanup.
1406  bool useEHCleanup = (cleanupKind & EHCleanup);
1407  EHStack.pushCleanup<DestroyObject>(cleanupKind, addr, type, destroyer,
1408  useEHCleanup);
1409 }
1410 
1412  assert(emission.Variable && "emission was not valid!");
1413 
1414  // If this was emitted as a global constant, we're done.
1415  if (emission.wasEmittedAsGlobal()) return;
1416 
1417  // If we don't have an insertion point, we're done. Sema prevents
1418  // us from jumping into any of these scopes anyway.
1419  if (!HaveInsertPoint()) return;
1420 
1421  const VarDecl &D = *emission.Variable;
1422 
1423  // Check the type for a cleanup.
1424  if (QualType::DestructionKind dtorKind = D.getType().isDestructedType())
1425  emitAutoVarTypeCleanup(emission, dtorKind);
1426 
1427  // In GC mode, honor objc_precise_lifetime.
1428  if (getLangOpts().getGC() != LangOptions::NonGC &&
1429  D.hasAttr<ObjCPreciseLifetimeAttr>()) {
1430  EHStack.pushCleanup<ExtendGCLifetime>(NormalCleanup, &D);
1431  }
1432 
1433  // Handle the cleanup attribute.
1434  if (const CleanupAttr *CA = D.getAttr<CleanupAttr>()) {
1435  const FunctionDecl *FD = CA->getFunctionDecl();
1436 
1437  llvm::Constant *F = CGM.GetAddrOfFunction(FD);
1438  assert(F && "Could not find function!");
1439 
1441  EHStack.pushCleanup<CallCleanupFunction>(NormalAndEHCleanup, F, &Info, &D);
1442  }
1443 
1444  // If this is a block variable, call _Block_object_destroy
1445  // (on the unforwarded address).
1446  if (emission.IsByRef)
1447  enterByrefCleanup(emission);
1448 }
1449 
1452  switch (kind) {
1453  case QualType::DK_none: llvm_unreachable("no destroyer for trivial dtor");
1455  return destroyCXXObject;
1457  return destroyARCStrongPrecise;
1459  return destroyARCWeak;
1460  }
1461  llvm_unreachable("Unknown DestructionKind");
1462 }
1463 
1464 /// pushEHDestroy - Push the standard destructor for the given type as
1465 /// an EH-only cleanup.
1467  Address addr, QualType type) {
1468  assert(dtorKind && "cannot push destructor for trivial type");
1469  assert(needsEHCleanup(dtorKind));
1470 
1471  pushDestroy(EHCleanup, addr, type, getDestroyer(dtorKind), true);
1472 }
1473 
1474 /// pushDestroy - Push the standard destructor for the given type as
1475 /// at least a normal cleanup.
1477  Address addr, QualType type) {
1478  assert(dtorKind && "cannot push destructor for trivial type");
1479 
1480  CleanupKind cleanupKind = getCleanupKind(dtorKind);
1481  pushDestroy(cleanupKind, addr, type, getDestroyer(dtorKind),
1482  cleanupKind & EHCleanup);
1483 }
1484 
1486  QualType type, Destroyer *destroyer,
1487  bool useEHCleanupForArray) {
1488  pushFullExprCleanup<DestroyObject>(cleanupKind, addr, type,
1489  destroyer, useEHCleanupForArray);
1490 }
1491 
1493  EHStack.pushCleanup<CallStackRestore>(Kind, SPMem);
1494 }
1495 
1497  CleanupKind cleanupKind, Address addr, QualType type,
1498  Destroyer *destroyer, bool useEHCleanupForArray) {
1499  assert(!isInConditionalBranch() &&
1500  "performing lifetime extension from within conditional");
1501 
1502  // Push an EH-only cleanup for the object now.
1503  // FIXME: When popping normal cleanups, we need to keep this EH cleanup
1504  // around in case a temporary's destructor throws an exception.
1505  if (cleanupKind & EHCleanup)
1506  EHStack.pushCleanup<DestroyObject>(
1507  static_cast<CleanupKind>(cleanupKind & ~NormalCleanup), addr, type,
1508  destroyer, useEHCleanupForArray);
1509 
1510  // Remember that we need to push a full cleanup for the object at the
1511  // end of the full-expression.
1512  pushCleanupAfterFullExpr<DestroyObject>(
1513  cleanupKind, addr, type, destroyer, useEHCleanupForArray);
1514 }
1515 
1516 /// emitDestroy - Immediately perform the destruction of the given
1517 /// object.
1518 ///
1519 /// \param addr - the address of the object; a type*
1520 /// \param type - the type of the object; if an array type, all
1521 /// objects are destroyed in reverse order
1522 /// \param destroyer - the function to call to destroy individual
1523 /// elements
1524 /// \param useEHCleanupForArray - whether an EH cleanup should be
1525 /// used when destroying array elements, in case one of the
1526 /// destructions throws an exception
1528  Destroyer *destroyer,
1529  bool useEHCleanupForArray) {
1530  const ArrayType *arrayType = getContext().getAsArrayType(type);
1531  if (!arrayType)
1532  return destroyer(*this, addr, type);
1533 
1534  llvm::Value *length = emitArrayLength(arrayType, type, addr);
1535 
1536  CharUnits elementAlign =
1537  addr.getAlignment()
1538  .alignmentOfArrayElement(getContext().getTypeSizeInChars(type));
1539 
1540  // Normally we have to check whether the array is zero-length.
1541  bool checkZeroLength = true;
1542 
1543  // But if the array length is constant, we can suppress that.
1544  if (llvm::ConstantInt *constLength = dyn_cast<llvm::ConstantInt>(length)) {
1545  // ...and if it's constant zero, we can just skip the entire thing.
1546  if (constLength->isZero()) return;
1547  checkZeroLength = false;
1548  }
1549 
1550  llvm::Value *begin = addr.getPointer();
1551  llvm::Value *end = Builder.CreateInBoundsGEP(begin, length);
1552  emitArrayDestroy(begin, end, type, elementAlign, destroyer,
1553  checkZeroLength, useEHCleanupForArray);
1554 }
1555 
1556 /// emitArrayDestroy - Destroys all the elements of the given array,
1557 /// beginning from last to first. The array cannot be zero-length.
1558 ///
1559 /// \param begin - a type* denoting the first element of the array
1560 /// \param end - a type* denoting one past the end of the array
1561 /// \param elementType - the element type of the array
1562 /// \param destroyer - the function to call to destroy elements
1563 /// \param useEHCleanup - whether to push an EH cleanup to destroy
1564 /// the remaining elements in case the destruction of a single
1565 /// element throws
1567  llvm::Value *end,
1568  QualType elementType,
1569  CharUnits elementAlign,
1570  Destroyer *destroyer,
1571  bool checkZeroLength,
1572  bool useEHCleanup) {
1573  assert(!elementType->isArrayType());
1574 
1575  // The basic structure here is a do-while loop, because we don't
1576  // need to check for the zero-element case.
1577  llvm::BasicBlock *bodyBB = createBasicBlock("arraydestroy.body");
1578  llvm::BasicBlock *doneBB = createBasicBlock("arraydestroy.done");
1579 
1580  if (checkZeroLength) {
1581  llvm::Value *isEmpty = Builder.CreateICmpEQ(begin, end,
1582  "arraydestroy.isempty");
1583  Builder.CreateCondBr(isEmpty, doneBB, bodyBB);
1584  }
1585 
1586  // Enter the loop body, making that address the current address.
1587  llvm::BasicBlock *entryBB = Builder.GetInsertBlock();
1588  EmitBlock(bodyBB);
1589  llvm::PHINode *elementPast =
1590  Builder.CreatePHI(begin->getType(), 2, "arraydestroy.elementPast");
1591  elementPast->addIncoming(end, entryBB);
1592 
1593  // Shift the address back by one element.
1594  llvm::Value *negativeOne = llvm::ConstantInt::get(SizeTy, -1, true);
1595  llvm::Value *element = Builder.CreateInBoundsGEP(elementPast, negativeOne,
1596  "arraydestroy.element");
1597 
1598  if (useEHCleanup)
1599  pushRegularPartialArrayCleanup(begin, element, elementType, elementAlign,
1600  destroyer);
1601 
1602  // Perform the actual destruction there.
1603  destroyer(*this, Address(element, elementAlign), elementType);
1604 
1605  if (useEHCleanup)
1606  PopCleanupBlock();
1607 
1608  // Check whether we've reached the end.
1609  llvm::Value *done = Builder.CreateICmpEQ(element, begin, "arraydestroy.done");
1610  Builder.CreateCondBr(done, doneBB, bodyBB);
1611  elementPast->addIncoming(element, Builder.GetInsertBlock());
1612 
1613  // Done.
1614  EmitBlock(doneBB);
1615 }
1616 
1617 /// Perform partial array destruction as if in an EH cleanup. Unlike
1618 /// emitArrayDestroy, the element type here may still be an array type.
1620  llvm::Value *begin, llvm::Value *end,
1621  QualType type, CharUnits elementAlign,
1622  CodeGenFunction::Destroyer *destroyer) {
1623  // If the element type is itself an array, drill down.
1624  unsigned arrayDepth = 0;
1625  while (const ArrayType *arrayType = CGF.getContext().getAsArrayType(type)) {
1626  // VLAs don't require a GEP index to walk into.
1627  if (!isa<VariableArrayType>(arrayType))
1628  arrayDepth++;
1629  type = arrayType->getElementType();
1630  }
1631 
1632  if (arrayDepth) {
1633  llvm::Value *zero = llvm::ConstantInt::get(CGF.SizeTy, 0);
1634 
1635  SmallVector<llvm::Value*,4> gepIndices(arrayDepth+1, zero);
1636  begin = CGF.Builder.CreateInBoundsGEP(begin, gepIndices, "pad.arraybegin");
1637  end = CGF.Builder.CreateInBoundsGEP(end, gepIndices, "pad.arrayend");
1638  }
1639 
1640  // Destroy the array. We don't ever need an EH cleanup because we
1641  // assume that we're in an EH cleanup ourselves, so a throwing
1642  // destructor causes an immediate terminate.
1643  CGF.emitArrayDestroy(begin, end, type, elementAlign, destroyer,
1644  /*checkZeroLength*/ true, /*useEHCleanup*/ false);
1645 }
1646 
1647 namespace {
1648  /// RegularPartialArrayDestroy - a cleanup which performs a partial
1649  /// array destroy where the end pointer is regularly determined and
1650  /// does not need to be loaded from a local.
1651  class RegularPartialArrayDestroy final : public EHScopeStack::Cleanup {
1652  llvm::Value *ArrayBegin;
1653  llvm::Value *ArrayEnd;
1654  QualType ElementType;
1655  CodeGenFunction::Destroyer *Destroyer;
1656  CharUnits ElementAlign;
1657  public:
1658  RegularPartialArrayDestroy(llvm::Value *arrayBegin, llvm::Value *arrayEnd,
1659  QualType elementType, CharUnits elementAlign,
1660  CodeGenFunction::Destroyer *destroyer)
1661  : ArrayBegin(arrayBegin), ArrayEnd(arrayEnd),
1662  ElementType(elementType), Destroyer(destroyer),
1663  ElementAlign(elementAlign) {}
1664 
1665  void Emit(CodeGenFunction &CGF, Flags flags) override {
1666  emitPartialArrayDestroy(CGF, ArrayBegin, ArrayEnd,
1667  ElementType, ElementAlign, Destroyer);
1668  }
1669  };
1670 
1671  /// IrregularPartialArrayDestroy - a cleanup which performs a
1672  /// partial array destroy where the end pointer is irregularly
1673  /// determined and must be loaded from a local.
1674  class IrregularPartialArrayDestroy final : public EHScopeStack::Cleanup {
1675  llvm::Value *ArrayBegin;
1676  Address ArrayEndPointer;
1677  QualType ElementType;
1678  CodeGenFunction::Destroyer *Destroyer;
1679  CharUnits ElementAlign;
1680  public:
1681  IrregularPartialArrayDestroy(llvm::Value *arrayBegin,
1682  Address arrayEndPointer,
1683  QualType elementType,
1684  CharUnits elementAlign,
1685  CodeGenFunction::Destroyer *destroyer)
1686  : ArrayBegin(arrayBegin), ArrayEndPointer(arrayEndPointer),
1687  ElementType(elementType), Destroyer(destroyer),
1688  ElementAlign(elementAlign) {}
1689 
1690  void Emit(CodeGenFunction &CGF, Flags flags) override {
1691  llvm::Value *arrayEnd = CGF.Builder.CreateLoad(ArrayEndPointer);
1692  emitPartialArrayDestroy(CGF, ArrayBegin, arrayEnd,
1693  ElementType, ElementAlign, Destroyer);
1694  }
1695  };
1696 } // end anonymous namespace
1697 
1698 /// pushIrregularPartialArrayCleanup - Push an EH cleanup to destroy
1699 /// already-constructed elements of the given array. The cleanup
1700 /// may be popped with DeactivateCleanupBlock or PopCleanupBlock.
1701 ///
1702 /// \param elementType - the immediate element type of the array;
1703 /// possibly still an array type
1705  Address arrayEndPointer,
1706  QualType elementType,
1707  CharUnits elementAlign,
1708  Destroyer *destroyer) {
1709  pushFullExprCleanup<IrregularPartialArrayDestroy>(EHCleanup,
1710  arrayBegin, arrayEndPointer,
1711  elementType, elementAlign,
1712  destroyer);
1713 }
1714 
1715 /// pushRegularPartialArrayCleanup - Push an EH cleanup to destroy
1716 /// already-constructed elements of the given array. The cleanup
1717 /// may be popped with DeactivateCleanupBlock or PopCleanupBlock.
1718 ///
1719 /// \param elementType - the immediate element type of the array;
1720 /// possibly still an array type
1722  llvm::Value *arrayEnd,
1723  QualType elementType,
1724  CharUnits elementAlign,
1725  Destroyer *destroyer) {
1726  pushFullExprCleanup<RegularPartialArrayDestroy>(EHCleanup,
1727  arrayBegin, arrayEnd,
1728  elementType, elementAlign,
1729  destroyer);
1730 }
1731 
1732 /// Lazily declare the @llvm.lifetime.start intrinsic.
1734  if (LifetimeStartFn)
1735  return LifetimeStartFn;
1736  LifetimeStartFn = llvm::Intrinsic::getDeclaration(&getModule(),
1737  llvm::Intrinsic::lifetime_start, AllocaInt8PtrTy);
1738  return LifetimeStartFn;
1739 }
1740 
1741 /// Lazily declare the @llvm.lifetime.end intrinsic.
1743  if (LifetimeEndFn)
1744  return LifetimeEndFn;
1745  LifetimeEndFn = llvm::Intrinsic::getDeclaration(&getModule(),
1746  llvm::Intrinsic::lifetime_end, AllocaInt8PtrTy);
1747  return LifetimeEndFn;
1748 }
1749 
1750 namespace {
1751  /// A cleanup to perform a release of an object at the end of a
1752  /// function. This is used to balance out the incoming +1 of a
1753  /// ns_consumed argument when we can't reasonably do that just by
1754  /// not doing the initial retain for a __block argument.
1755  struct ConsumeARCParameter final : EHScopeStack::Cleanup {
1756  ConsumeARCParameter(llvm::Value *param,
1757  ARCPreciseLifetime_t precise)
1758  : Param(param), Precise(precise) {}
1759 
1760  llvm::Value *Param;
1761  ARCPreciseLifetime_t Precise;
1762 
1763  void Emit(CodeGenFunction &CGF, Flags flags) override {
1764  CGF.EmitARCRelease(Param, Precise);
1765  }
1766  };
1767 } // end anonymous namespace
1768 
1769 /// Emit an alloca (or GlobalValue depending on target)
1770 /// for the specified parameter and set up LocalDeclMap.
1772  unsigned ArgNo) {
1773  // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl?
1774  assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) &&
1775  "Invalid argument to EmitParmDecl");
1776 
1777  Arg.getAnyValue()->setName(D.getName());
1778 
1779  QualType Ty = D.getType();
1780 
1781  // Use better IR generation for certain implicit parameters.
1782  if (auto IPD = dyn_cast<ImplicitParamDecl>(&D)) {
1783  // The only implicit argument a block has is its literal.
1784  // We assume this is always passed directly.
1785  if (BlockInfo) {
1786  setBlockContextParameter(IPD, ArgNo, Arg.getDirectValue());
1787  return;
1788  }
1789 
1790  // Apply any prologue 'this' adjustments required by the ABI. Be careful to
1791  // handle the case where 'this' is passed indirectly as part of an inalloca
1792  // struct.
1793  if (const CXXMethodDecl *MD =
1794  dyn_cast_or_null<CXXMethodDecl>(CurCodeDecl)) {
1795  if (MD->isVirtual() && IPD == CXXABIThisDecl) {
1796  llvm::Value *This = Arg.isIndirect()
1798  : Arg.getDirectValue();
1800  *this, CurGD, This);
1801  if (Arg.isIndirect())
1802  Builder.CreateStore(This, Arg.getIndirectAddress());
1803  else
1804  Arg = ParamValue::forDirect(This);
1805  }
1806  }
1807  }
1808 
1809  Address DeclPtr = Address::invalid();
1810  bool DoStore = false;
1811  bool IsScalar = hasScalarEvaluationKind(Ty);
1812  // If we already have a pointer to the argument, reuse the input pointer.
1813  if (Arg.isIndirect()) {
1814  DeclPtr = Arg.getIndirectAddress();
1815  // If we have a prettier pointer type at this point, bitcast to that.
1816  unsigned AS = DeclPtr.getType()->getAddressSpace();
1817  llvm::Type *IRTy = ConvertTypeForMem(Ty)->getPointerTo(AS);
1818  if (DeclPtr.getType() != IRTy)
1819  DeclPtr = Builder.CreateBitCast(DeclPtr, IRTy, D.getName());
1820 
1821  // Push a destructor cleanup for this parameter if the ABI requires it.
1822  // Don't push a cleanup in a thunk for a method that will also emit a
1823  // cleanup.
1824  if (!IsScalar && !CurFuncIsThunk &&
1826  const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
1827  if (RD && RD->hasNonTrivialDestructor())
1829  }
1830  } else {
1831  // Otherwise, create a temporary to hold the value.
1832  DeclPtr = CreateMemTemp(Ty, getContext().getDeclAlign(&D),
1833  D.getName() + ".addr");
1834  DoStore = true;
1835  }
1836 
1837  llvm::Value *ArgVal = (DoStore ? Arg.getDirectValue() : nullptr);
1838 
1839  LValue lv = MakeAddrLValue(DeclPtr, Ty);
1840  if (IsScalar) {
1841  Qualifiers qs = Ty.getQualifiers();
1842  if (Qualifiers::ObjCLifetime lt = qs.getObjCLifetime()) {
1843  // We honor __attribute__((ns_consumed)) for types with lifetime.
1844  // For __strong, it's handled by just skipping the initial retain;
1845  // otherwise we have to balance out the initial +1 with an extra
1846  // cleanup to do the release at the end of the function.
1847  bool isConsumed = D.hasAttr<NSConsumedAttr>();
1848 
1849  // 'self' is always formally __strong, but if this is not an
1850  // init method then we don't want to retain it.
1851  if (D.isARCPseudoStrong()) {
1852  const ObjCMethodDecl *method = cast<ObjCMethodDecl>(CurCodeDecl);
1853  assert(&D == method->getSelfDecl());
1854  assert(lt == Qualifiers::OCL_Strong);
1855  assert(qs.hasConst());
1856  assert(method->getMethodFamily() != OMF_init);
1857  (void) method;
1859  }
1860 
1861  // Load objects passed indirectly.
1862  if (Arg.isIndirect() && !ArgVal)
1863  ArgVal = Builder.CreateLoad(DeclPtr);
1864 
1865  if (lt == Qualifiers::OCL_Strong) {
1866  if (!isConsumed) {
1867  if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
1868  // use objc_storeStrong(&dest, value) for retaining the
1869  // object. But first, store a null into 'dest' because
1870  // objc_storeStrong attempts to release its old value.
1871  llvm::Value *Null = CGM.EmitNullConstant(D.getType());
1872  EmitStoreOfScalar(Null, lv, /* isInitialization */ true);
1873  EmitARCStoreStrongCall(lv.getAddress(), ArgVal, true);
1874  DoStore = false;
1875  }
1876  else
1877  // Don't use objc_retainBlock for block pointers, because we
1878  // don't want to Block_copy something just because we got it
1879  // as a parameter.
1880  ArgVal = EmitARCRetainNonBlock(ArgVal);
1881  }
1882  } else {
1883  // Push the cleanup for a consumed parameter.
1884  if (isConsumed) {
1885  ARCPreciseLifetime_t precise = (D.hasAttr<ObjCPreciseLifetimeAttr>()
1887  EHStack.pushCleanup<ConsumeARCParameter>(getARCCleanupKind(), ArgVal,
1888  precise);
1889  }
1890 
1891  if (lt == Qualifiers::OCL_Weak) {
1892  EmitARCInitWeak(DeclPtr, ArgVal);
1893  DoStore = false; // The weak init is a store, no need to do two.
1894  }
1895  }
1896 
1897  // Enter the cleanup scope.
1898  EmitAutoVarWithLifetime(*this, D, DeclPtr, lt);
1899  }
1900  }
1901 
1902  // Store the initial value into the alloca.
1903  if (DoStore)
1904  EmitStoreOfScalar(ArgVal, lv, /* isInitialization */ true);
1905 
1906  setAddrOfLocalVar(&D, DeclPtr);
1907 
1908  // Emit debug info for param declaration.
1909  if (CGDebugInfo *DI = getDebugInfo()) {
1910  if (CGM.getCodeGenOpts().getDebugInfo() >=
1912  DI->EmitDeclareOfArgVariable(&D, DeclPtr.getPointer(), ArgNo, Builder);
1913  }
1914  }
1915 
1916  if (D.hasAttr<AnnotateAttr>())
1917  EmitVarAnnotations(&D, DeclPtr.getPointer());
1918 
1919  // We can only check return value nullability if all arguments to the
1920  // function satisfy their nullability preconditions. This makes it necessary
1921  // to emit null checks for args in the function body itself.
1922  if (requiresReturnValueNullabilityCheck()) {
1923  auto Nullability = Ty->getNullability(getContext());
1925  SanitizerScope SanScope(this);
1926  RetValNullabilityPrecondition =
1927  Builder.CreateAnd(RetValNullabilityPrecondition,
1928  Builder.CreateIsNotNull(Arg.getAnyValue()));
1929  }
1930  }
1931 }
1932 
1934  CodeGenFunction *CGF) {
1935  if (!LangOpts.OpenMP || (!LangOpts.EmitAllDecls && !D->isUsed()))
1936  return;
1937  getOpenMPRuntime().emitUserDefinedReduction(CGF, D);
1938 }
unsigned getAddressSpace() const
Return the address space of this type.
Definition: Type.h:5605
CGOpenCLRuntime & getOpenCLRuntime()
Return a reference to the configured OpenCL runtime.
ReturnValueSlot - Contains the address where the return value of a function can be stored...
Definition: CGCall.h:281
Defines the clang::ASTContext interface.
virtual llvm::Value * adjustThisParameterInVirtualFunctionPrologue(CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This)
Perform ABI-specific "this" parameter adjustment in a virtual function prologue.
Definition: CGCXXABI.h:363
llvm::StoreInst * CreateDefaultAlignedStore(llvm::Value *Val, llvm::Value *Addr, bool IsVolatile=false)
Definition: CGBuilder.h:122
FunctionDecl - An instance of this class is created to represent a function declaration or definition...
Definition: Decl.h:1618
StringRef getName() const
getName - Get the name of identifier for this declaration as a StringRef.
Definition: Decl.h:237
void EmitStaticVarDecl(const VarDecl &D, llvm::GlobalValue::LinkageTypes Linkage)
Definition: CGDecl.cpp:368
llvm::Value * EmitARCRetainAutoreleaseScalarExpr(const Expr *expr)
Definition: CGObjC.cpp:2985
Destroyer * getDestroyer(QualType::DestructionKind destructionKind)
Definition: CGDecl.cpp:1451
A (possibly-)qualified type.
Definition: Type.h:616
ArrayRef< Capture > captures() const
Definition: Decl.h:3682
bool isPODType(const ASTContext &Context) const
Determine whether this is a Plain Old Data (POD) type (C++ 3.9p10).
Definition: Type.cpp:2005
void EmitExtendGCLifetime(llvm::Value *object)
EmitExtendGCLifetime - Given a pointer to an Objective-C object, make sure it survives garbage collec...
Definition: CGObjC.cpp:3195
llvm::Value * getPointer() const
Definition: CGValue.h:342
llvm::Type * ConvertTypeForMem(QualType T)
void EmitVarDecl(const VarDecl &D)
EmitVarDecl - Emit a local variable declaration.
Definition: CGDecl.cpp:155
CharUnits getDeclAlign(const Decl *D, bool ForAlignof=false) const
Return a conservative estimate of the alignment of the specified decl D.
bool hasTrivialDestructor() const
Determine whether this class has a trivial destructor (C++ [class.dtor]p3)
Definition: DeclCXX.h:1350
DestructionKind isDestructedType() const
Returns a nonzero value if objects of this type require non-trivial work to clean up after...
Definition: Type.h:1054
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:2513
llvm::Module & getModule() const
static AggValueSlot forLValue(const LValue &LV, IsDestructed_t isDestructed, NeedsGCBarriers_t needsGC, IsAliased_t isAliased, IsZeroed_t isZeroed=IsNotZeroed)
Definition: CGValue.h:539
Stmt - This represents one statement.
Definition: Stmt.h:60
bool isInConditionalBranch() const
isInConditionalBranch - Return true if we're currently emitting one branch or the other of a conditio...
const TargetInfo & getTarget() const
unsigned GetGlobalVarAddressSpace(const VarDecl *D)
Return the AST address space of the underlying global variable for D, as determined by its declaratio...
Defines the SourceManager interface.
static bool canEmitInitWithFewStoresAfterMemset(llvm::Constant *Init, unsigned &NumStores)
canEmitInitWithFewStoresAfterMemset - Decide whether we can emit the non-zero parts of the specified ...
Definition: CGDecl.cpp:819
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition: CharUnits.h:179
bool isRecordType() const
Definition: Type.h:5769
void emitAutoVarTypeCleanup(const AutoVarEmission &emission, QualType::DestructionKind dtorKind)
Enter a destroy cleanup for the given local variable.
Definition: CGDecl.cpp:1354
QualType getUnderlyingType() const
Definition: Decl.h:2727
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:81
Address getAddress() const
Definition: CGValue.h:346
const Decl * CurCodeDecl
CurCodeDecl - This is the inner-most code context, which includes blocks.
bool hasNonTrivialDestructor() const
Determine whether this class has a non-trivial destructor (C++ [class.dtor]p3)
Definition: DeclCXX.h:1356
void EmitAutoVarDecl(const VarDecl &D)
EmitAutoVarDecl - Emit an auto variable declaration.
Definition: CGDecl.cpp:921
const llvm::DataLayout & getDataLayout() const
static Destroyer destroyARCStrongPrecise
void pushLifetimeExtendedDestroy(CleanupKind kind, Address addr, QualType type, Destroyer *destroyer, bool useEHCleanupForArray)
Definition: CGDecl.cpp:1496
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:1749
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition: Type.h:2497
const Expr * getInit() const
Definition: Decl.h:1146
Represents a call to a C++ constructor.
Definition: ExprCXX.h:1177
void EmitARCCopyWeak(Address dst, Address src)
void @objc_copyWeak(i8** dest, i8** src) Disregards the current value in dest.
Definition: CGObjC.cpp:2324
const LangOptions & getLangOpts() const
llvm::Value * EmitARCRetainNonBlock(llvm::Value *value)
Retain the given object, with normal retain semantics.
Definition: CGObjC.cpp:1983
llvm::IntegerType * Int8Ty
i8, i16, i32, and i64
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2329
VarDecl - An instance of this class is created to represent a variable declaration or definition...
Definition: Decl.h:758
llvm::Type * getElementType() const
Return the type of the values stored in this address.
Definition: Address.h:52
RAII object to set/unset CodeGenFunction::IsSanitizerScope.
bool areArgsDestroyedLeftToRightInCallee() const
Are arguments to a call destroyed left to right in the callee? This is a fundamental language change...
Definition: TargetCXXABI.h:216
ObjCLifetime getObjCLifetime() const
Definition: Type.h:309
This class gathers all debug information during compilation and is responsible for emitting to llvm g...
Definition: CGDebugInfo.h:53
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:113
void EmitVariablyModifiedType(QualType Ty)
EmitVLASize - Capture all the sizes for the VLA expressions in the given variably-modified type and s...
llvm::Type * ConvertTypeForMem(QualType T)
ConvertTypeForMem - Convert type T into a llvm::Type.
Represents an expression – generally a full-expression – that introduces cleanups to be run at the en...
Definition: ExprCXX.h:2920
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have...
Definition: Linkage.h:25
The collection of all-type qualifiers we support.
Definition: Type.h:118
void emitDestroy(Address addr, QualType type, Destroyer *destroyer, bool useEHCleanupForArray)
emitDestroy - Immediately perform the destruction of the given object.
Definition: CGDecl.cpp:1527
void emitByrefStructureInit(const AutoVarEmission &emission)
Initialize the structural components of a __block variable, i.e.
Definition: CGBlocks.cpp:2277
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 call a handler function in a sanitizer runtime with the provided argum...
Definition: CGExpr.cpp:2719
bool hasAttr() const
Definition: DeclBase.h:521
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
bool isVariablyModifiedType() const
Whether this type is a variably-modified type (C99 6.7.5).
Definition: Type.h:1813
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:1316
RValue EmitReferenceBindingToExpr(const Expr *E)
Emits a reference binding to the passed in expression.
Definition: CGExpr.cpp:531
bool isReferenceType() const
Definition: Type.h:5721
const internal::VariadicAllOfMatcher< Decl > decl
Matches declarations.
Definition: ASTMatchers.h:281
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:1466
Denotes a cleanup that should run when a scope is exited using exceptional control flow (a throw stat...
Definition: EHScopeStack.h:81
void reportGlobalToASan(llvm::GlobalVariable *GV, const VarDecl &D, bool IsDynInit=false)
CleanupKind getCleanupKind(QualType::DestructionKind kind)
const Decl * getDecl() const
Definition: GlobalDecl.h:62
bool hasExternalStorage() const
Returns true if a variable has extern or private_extern storage.
Definition: Decl.h:996
void setNonGC(bool Value)
Definition: CGValue.h:290
T * getAttr() const
Definition: DeclBase.h:518
llvm::Constant * getLLVMLifetimeStartFn()
Lazily declare the .lifetime.start intrinsic.
Definition: CGDecl.cpp:1733
llvm::Value * EmitARCStoreStrongCall(Address addr, llvm::Value *value, bool resultIgnored)
Store into a strong object.
Definition: CGObjC.cpp:2136
llvm::Value * EmitLoadOfScalar(Address Addr, bool Volatile, QualType Ty, SourceLocation Loc, LValueBaseInfo BaseInfo=LValueBaseInfo(AlignmentSource::Type), llvm::MDNode *TBAAInfo=nullptr, QualType TBAABaseTy=QualType(), uint64_t TBAAOffset=0, bool isNontemporal=false)
EmitLoadOfScalar - Load a scalar value from an address, taking care to appropriately convert from the...
Definition: CGExpr.cpp:1437
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:1721
static bool hasScalarEvaluationKind(QualType T)
static void drillIntoBlockVariable(CodeGenFunction &CGF, LValue &lvalue, const VarDecl *var)
Definition: CGDecl.cpp:678
Base object ctor.
Definition: ABI.h:27
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:150
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:38
static bool shouldUseMemSetPlusStoresToInitialize(llvm::Constant *Init, uint64_t GlobalSize)
shouldUseMemSetPlusStoresToInitialize - Decide whether we should use memset plus some stores to initi...
Definition: CGDecl.cpp:902
Qualifiers::ObjCLifetime getObjCLifetime() const
Definition: CGValue.h:279
ObjCMethodFamily getMethodFamily() const
Determines the family of this method.
Definition: DeclObjC.cpp:931
void EmitOMPDeclareReduction(const OMPDeclareReductionDecl *D, CodeGenFunction *CGF=nullptr)
Emit a code for declare reduction construct.
Definition: CGDecl.cpp:1933
An x-value expression is a reference to an object with independent storage but which can be "moved"...
Definition: Specifiers.h:115
void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl)
Emit information about a global variable.
void setStaticLocalDeclAddress(const VarDecl *D, llvm::Constant *C)
child_range children()
Definition: Stmt.cpp:208
const ArrayType * getAsArrayType(QualType T) const
Type Query functions.
bool needsEHCleanup(QualType::DestructionKind kind)
Determines whether an EH cleanup is required to destroy a type with the given destruction kind...
llvm::Value * EmitARCUnsafeUnretainedScalarExpr(const Expr *expr)
EmitARCUnsafeUnretainedScalarExpr - Semantically equivalent to immediately releasing the resut of Emi...
Definition: CGObjC.cpp:3096
bool IsBypassed(const VarDecl *D) const
Returns true if the variable declaration was by bypassed by any goto or switch statement.
llvm::CallInst * CreateMemCpy(Address Dest, Address Src, llvm::Value *Size, bool IsVolatile=false)
Definition: CGBuilder.h:252
std::string getNameAsString() const
getNameAsString - Get a human-readable name for the declaration, even if it is one of the special kin...
Definition: Decl.h:252
Expr * IgnoreParenCasts() LLVM_READONLY
IgnoreParenCasts - Ignore parentheses and casts.
Definition: Expr.cpp:2399
Values of this type can never be null.
Scope - A scope is a transient data structure that is used while parsing the program.
Definition: Scope.h:39
llvm::BasicBlock * createBasicBlock(const Twine &name="", llvm::Function *parent=nullptr, llvm::BasicBlock *before=nullptr)
createBasicBlock - Create an LLVM basic block.
llvm::Value * EmitARCRetainScalarExpr(const Expr *expr)
EmitARCRetainScalarExpr - Semantically equivalent to EmitARCRetainObject(e->getType(), EmitScalarExpr(e)), but making a best-effort attempt to peephole expressions that naturally produce retained objects.
Definition: CGObjC.cpp:2969
Denotes a cleanup that should run when a scope is exited using normal control flow (falling off the e...
Definition: EHScopeStack.h:85
void EmitAtomicInit(Expr *E, LValue lvalue)
Definition: CGAtomic.cpp:1816
bool hasConst() const
Definition: Type.h:237
GlobalDecl CurGD
CurGD - The GlobalDecl for the current function being compiled.
static bool ContainsLabel(const Stmt *S, bool IgnoreCaseStmts=false)
ContainsLabel - Return true if the statement contains a label in it.
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:90
void setGlobalVisibility(llvm::GlobalValue *GV, const NamedDecl *D) const
Set the visibility for the given LLVM GlobalValue.
detail::InMemoryDirectory::const_iterator I
QualType getType() const
Definition: Decl.h:589
This object can be modified without requiring retains or releases.
Definition: Type.h:139
bool isTypeConstant(QualType QTy, bool ExcludeCtorDtor)
isTypeConstant - Determine whether an object of this type can be emitted as a constant.
static CharUnits One()
One - Construct a CharUnits quantity of one.
Definition: CharUnits.h:58
CompoundStmt - This represents a group of statements like { stmt stmt }.
Definition: Stmt.h:575
std::pair< llvm::Value *, llvm::Value * > ComplexPairTy
AutoVarEmission EmitAutoVarAlloca(const VarDecl &var)
EmitAutoVarAlloca - Emit the alloca and debug information for a local variable.
Definition: CGDecl.cpp:953
const CodeGen::CGBlockInfo * BlockInfo
const TargetCodeGenInfo & getTargetCodeGenInfo()
Qualifiers::ObjCLifetime getObjCLifetime() const
Returns lifetime attribute of this type.
Definition: Type.h:1028
RValue - This trivial value class is used to represent the result of an expression that is evaluated...
Definition: CGValue.h:38
void setAddress(Address address)
Definition: CGValue.h:347
CleanupKind getARCCleanupKind()
Retrieves the default cleanup kind for an ARC cleanup.
StringRef getBlockMangledName(GlobalDecl GD, const BlockDecl *BD)
std::vector< bool > & Stack
llvm::Constant * getNullPointer(llvm::PointerType *T, QualType QT)
Get target specific null pointer.
llvm::Value * EmitARCStoreWeak(Address addr, llvm::Value *value, bool ignored)
i8* @objc_storeWeak(i8** addr, i8* value) Returns value.
Definition: CGObjC.cpp:2268
void EmitCXXGuardedInit(const VarDecl &D, llvm::GlobalVariable *DeclPtr, bool PerformInit)
Emit code in this function to perform a guarded variable initialization.
Definition: CGDeclCXX.cpp:248
static TypeEvaluationKind getEvaluationKind(QualType T)
hasAggregateLLVMType - Return true if the specified AST type will map into an aggregate LLVM type or ...
llvm::Value * getPointer() const
Definition: Address.h:38
bool isMicrosoft() const
Is this ABI an MSVC-compatible ABI?
Definition: TargetCXXABI.h:154
BlockDecl - This represents a block literal declaration, which is like an unnamed FunctionDecl...
Definition: Decl.h:3557
ValueDecl - Represent the declaration of a variable (in which case it is an lvalue) a function (in wh...
Definition: Decl.h:580
Expr - This represents one expression.
Definition: Expr.h:105
void EmitARCMoveWeak(Address dst, Address src)
void @objc_moveWeak(i8** dest, i8** src) Disregards the current value in dest.
Definition: CGObjC.cpp:2315
Emit only debug info necessary for generating line number tables (-gline-tables-only).
void EmitAutoVarInit(const AutoVarEmission &emission)
Definition: CGDecl.cpp:1201
static Address invalid()
Definition: Address.h:35
std::string Label
CGCXXABI & getCXXABI() const
void AddGlobalAnnotations(const ValueDecl *D, llvm::GlobalValue *GV)
Add global annotations that are set on D, for the global GV.
Enters a new scope for capturing cleanups, all of which will be executed once the scope is exited...
static CGCallee forDirect(llvm::Constant *functionPtr, const CGCalleeInfo &abstractInfo=CGCalleeInfo())
Definition: CGCall.h:125
bool isAtomicType() const
Definition: Type.h:5794
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...
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition: Expr.h:4820
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2551
Kind getKind() const
Definition: DeclBase.h:410
DeclContext * getDeclContext()
Definition: DeclBase.h:416
ASTContext & getContext() const
ImplicitParamDecl * getSelfDecl() const
Definition: DeclObjC.h:408
void add(RValue rvalue, QualType type, bool needscopy=false)
Definition: CGCall.h:207
void EmitStoreOfScalar(llvm::Value *Value, Address Addr, bool Volatile, QualType Ty, LValueBaseInfo BaseInfo=LValueBaseInfo(AlignmentSource::Type), llvm::MDNode *TBAAInfo=nullptr, bool isInit=false, QualType TBAABaseTy=QualType(), uint64_t TBAAOffset=0, bool isNontemporal=false)
EmitStoreOfScalar - Store a scalar value to an address, taking care to appropriately convert from the...
Definition: CGExpr.cpp:1527
llvm::LLVMContext & getLLVMContext()
Base object dtor.
Definition: ABI.h:37
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:684
llvm::GlobalValue::LinkageTypes getLLVMLinkageVarDefinition(const VarDecl *VD, bool IsConstant)
Returns LLVM linkage for a declarator.
Checking the value assigned to a _Nonnull pointer. Must not be null.
bool isExceptionVariable() const
Determine whether this variable is the exception variable in a C++ catch statememt or an Objective-C ...
Definition: Decl.h:1232
llvm::PointerType * AllocaInt8PtrTy
bool isExternallyVisible() const
Definition: Decl.h:338
llvm::CallInst * CreateMemSet(Address Dest, llvm::Value *Value, llvm::Value *Size, bool IsVolatile=false)
Definition: CGBuilder.h:274
llvm::Function * getIntrinsic(unsigned IID, ArrayRef< llvm::Type * > Tys=None)
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:1566
static bool hasNontrivialDestruction(QualType T)
hasNontrivialDestruction - Determine whether a type's destruction is non-trivial. ...
Definition: CGDecl.cpp:298
float __ovld __cnfn length(float p)
Return the length of vector p, i.e., sqrt(p.x2 + p.y 2 + ...)
static bool isCapturedBy(const VarDecl &var, const Expr *e)
Determines whether the given __block variable is potentially captured by the given expression...
Definition: CGDecl.cpp:1138
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:29
The l-value was considered opaque, so the alignment was determined from a type.
void setBlockContextParameter(const ImplicitParamDecl *D, unsigned argNum, llvm::Value *ptr)
Definition: CGBlocks.cpp:1182
void enterByrefCleanup(const AutoVarEmission &emission)
Enter a cleanup to destroy a __block variable.
Definition: CGBlocks.cpp:2410
llvm::Constant * getOrCreateStaticVarDecl(const VarDecl &D, llvm::GlobalValue::LinkageTypes Linkage)
Definition: CGDecl.cpp:204
There is no lifetime qualification on this type.
Definition: Type.h:135
bool HaveInsertPoint() const
HaveInsertPoint - True if an insertion point is defined.
Address CreateBitCast(Address Addr, llvm::Type *Ty, const llvm::Twine &Name="")
Definition: CGBuilder.h:142
Assigning into this object requires the old value to be released and the new value to be retained...
Definition: Type.h:146
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:308
Kind
ASTContext & getContext() const
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:1476
Encodes a location in the source.
void EnsureInsertPoint()
EnsureInsertPoint - Ensure that an insertion point is defined so that emitted IR has a place to go...
body_range body()
Definition: Stmt.h:605
void EmitARCRelease(llvm::Value *value, ARCPreciseLifetime_t precise)
Release the given object.
Definition: CGObjC.cpp:2090
LValue EmitDeclRefLValue(const DeclRefExpr *E)
Definition: CGExpr.cpp:2251
This represents '#pragma omp declare reduction ...' directive.
Definition: DeclOpenMP.h:102
bool isConstant(const ASTContext &Ctx) const
Definition: Type.h:753
static std::string getStaticDeclName(CodeGenModule &CGM, const VarDecl &D)
Definition: CGDecl.cpp:181
bool isConstantSizeType() const
Return true if this is not a variable sized type, according to the rules of C99 6.7.5p3.
Definition: Type.cpp:1920
bool isLocalVarDecl() const
isLocalVarDecl - Returns true for local variable declarations other than parameters.
Definition: Decl.h:1034
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:1619
const BlockByrefInfo & getBlockByrefInfo(const VarDecl *var)
BuildByrefInfo - This routine changes a __block variable declared as T x into:
Definition: CGBlocks.cpp:2189
llvm::Value * EmitLifetimeStart(uint64_t Size, llvm::Value *Addr)
Emit a lifetime.begin marker if some criteria are satisfied.
Definition: CGDecl.cpp:930
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:1903
static bool isAccessedBy(const VarDecl &var, const Stmt *s)
Definition: CGDecl.cpp:596
SanitizerSet SanOpts
Sanitizers enabled for this function.
This file defines OpenMP nodes for declarative directives.
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:1187
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:197
const CodeGenOptions & getCodeGenOpts() const
const Type * getBaseElementTypeUnsafe() const
Get the base element type of this type, potentially discarding type qualifiers.
Definition: Type.h:6000
An aligned address.
Definition: Address.h:25
const LangOptions & getLangOpts() const
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:560
Complete object dtor.
Definition: ABI.h:36
void EmitParmDecl(const VarDecl &D, ParamValue Arg, unsigned ArgNo)
EmitParmDecl - Emit a ParmVarDecl or an ImplicitParamDecl.
Definition: CGDecl.cpp:1771
Assigning into this object requires a lifetime extension.
Definition: Type.h:152
StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}).
Definition: Expr.h:3464
static ParamValue forDirect(llvm::Value *value)
llvm::Constant * getLLVMLifetimeEndFn()
Lazily declare the .lifetime.end intrinsic.
Definition: CGDecl.cpp:1742
void enterFullExpression(const ExprWithCleanups *E)
void EmitDecl(const Decl &D)
EmitDecl - Emit a declaration.
Definition: CGDecl.cpp:40
static Destroyer destroyARCStrongImprecise
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition: Expr.cpp:216
llvm::Constant * GetAddrOfGlobal(GlobalDecl GD, ForDefinition_t IsForDefinition=NotForDefinition)
llvm::Value * EmitScalarExpr(const Expr *E, bool IgnoreResultAssign=false)
EmitScalarExpr - Emit the computation of the specified expression of LLVM scalar type, returning the result.
void addUsedGlobal(llvm::GlobalValue *GV)
Add a global to a list to be added to the llvm.used metadata.
Base class for declarations which introduce a typedef-name.
Definition: Decl.h:2682
QualType getType() const
Definition: Expr.h:127
void ErrorUnsupported(const Stmt *S, const char *Type)
Print out an error that codegen doesn't support the specified stmt yet.
CGFunctionInfo - Class to encapsulate the information about a function definition.
CharUnits getAlignment() const
Return the alignment of this pointer.
Definition: Address.h:67
This class organizes the cross-function state that is used while generating LLVM code.
CGOpenMPRuntime & getOpenMPRuntime()
Return a reference to the configured OpenMP runtime.
static ApplyDebugLocation CreateDefaultArtificial(CodeGenFunction &CGF, SourceLocation TemporaryLocation)
Apply TemporaryLocation if it is valid.
Definition: CGDebugInfo.h:655
Address getObjectAddress(CodeGenFunction &CGF) const
Returns the address of the object within this declaration.
void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type, bool ForVirtualBase, bool Delegating, Address This)
Definition: CGClass.cpp:2302
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1215
StringRef Name
Definition: USRFinder.cpp:123
CXXDestructorDecl * getDestructor() const
Returns the destructor decl for this class.
Definition: DeclCXX.cpp:1437
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
Definition: ASTMatchers.h:2126
const CGFunctionInfo & arrangeFunctionDeclaration(const FunctionDecl *FD)
Free functions are functions that are compatible with an ordinary C function pointer type...
Definition: CGCall.cpp:419
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...
llvm::LoadInst * CreateLoad(Address Addr, const llvm::Twine &Name="")
Definition: CGBuilder.h:70
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
bool isUsed(bool CheckUsedAttr=true) const
Whether any (re-)declaration of the entity was used, meaning that a definition is required...
Definition: DeclBase.cpp:367
Decl * getNonClosureContext()
Find the innermost non-closure ancestor of this declaration, walking up through blocks, lambdas, etc.
Definition: DeclBase.cpp:940
llvm::Constant * EmitNullConstant(QualType T)
Return the result of value-initializing the given type, i.e.
detail::InMemoryDirectory::const_iterator E
llvm::StoreInst * CreateStore(llvm::Value *Val, Address Addr, bool IsVolatile=false)
Definition: CGBuilder.h:108
static bool tryEmitARCCopyWeakInit(CodeGenFunction &CGF, const LValue &destLV, const Expr *init)
Definition: CGDecl.cpp:628
void EmitAggExpr(const Expr *E, AggValueSlot AS)
EmitAggExpr - Emit the computation of the specified expression of aggregate type. ...
Definition: CGExprAgg.cpp:1539
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:2687
void EmitAutoVarCleanups(const AutoVarEmission &emission)
Definition: CGDecl.cpp:1411
llvm::PointerType * getType() const
Return the type of the pointer value.
Definition: Address.h:44
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:3784
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:6042
void EmitStoreOfComplex(ComplexPairTy V, LValue dest, bool isInit)
EmitStoreOfComplex - Store a complex number into the specified l-value.
llvm::LoadInst * CreateFlagLoad(llvm::Value *Addr, const llvm::Twine &Name="")
Emit a load from an i1 flag variable.
Definition: CGBuilder.h:129
bool has(SanitizerMask K) const
Check if a certain (single) sanitizer is enabled.
Definition: Sanitizers.h:50
void EmitScalarInit(const Expr *init, const ValueDecl *D, LValue lvalue, bool capturedByInit)
Definition: CGDecl.cpp:705
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.
bool isNRVOVariable() const
Determine whether this local variable can be used with the named return value optimization (NRVO)...
Definition: Decl.h:1250
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1548
StringRef getMangledName(GlobalDecl GD)
Internal linkage, which indicates that the entity can be referred to from within the translation unit...
Definition: Linkage.h:33
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
EmitBlock - Emit the given block.
Definition: CGStmt.cpp:436
void EmitARCInitWeak(Address addr, llvm::Value *value)
i8* @objc_initWeak(i8** addr, i8* value) Returns value.
Definition: CGObjC.cpp:2280
ARCPreciseLifetime_t
Does an ARC strong l-value have precise lifetime?
Definition: CGValue.h:119
ComplexPairTy EmitComplexExpr(const Expr *E, bool IgnoreReal=false, bool IgnoreImag=false)
EmitComplexExpr - Emit the computation of the specified expression of complex type, returning the result.
llvm::Constant * EmitConstantInit(const VarDecl &D, CodeGenFunction *CGF=nullptr)
Try to emit the initializer for the given declaration as a constant; returns 0 if the expression cann...
A use of a default initializer in a constructor or in aggregate initialization.
Definition: ExprCXX.h:1052
Reading or writing from this object requires a barrier call.
Definition: Type.h:149
const internal::VariadicDynCastAllOfMatcher< Stmt, CastExpr > castExpr
Matches any cast nodes of Clang's AST.
Definition: ASTMatchers.h:2063
virtual llvm::Value * performAddrSpaceCast(CodeGen::CodeGenFunction &CGF, llvm::Value *V, unsigned SrcAddr, unsigned DestAddr, llvm::Type *DestTy, bool IsNonNull=false) const
Perform address space cast of an expression of pointer type.
Definition: TargetInfo.cpp:432
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...
static void emitStoresForInitAfterMemset(llvm::Constant *Init, llvm::Value *Loc, bool isVolatile, CGBuilderTy &Builder)
emitStoresForInitAfterMemset - For inits that canEmitInitWithFewStoresAfterMemset returned true for...
Definition: CGDecl.cpp:858
bool isARCPseudoStrong() const
Determine whether this variable is an ARC pseudo-__strong variable.
Definition: Decl.h:1272
Represents a C++ struct/union/class.
Definition: DeclCXX.h:267
BoundNodesTreeBuilder *const Builder
TargetCXXABI getCXXABI() const
Get the C++ ABI currently in use.
Definition: TargetInfo.h:861
bool isObjCObjectPointerType() const
Definition: Type.h:5784
llvm::Type * ConvertType(QualType T)
LValue MakeAddrLValue(Address Addr, QualType T, LValueBaseInfo BaseInfo=LValueBaseInfo(AlignmentSource::Type))
LValue EmitLValue(const Expr *E)
EmitLValue - Emit code to compute a designator that specifies the location of the expression...
Definition: CGExpr.cpp:1082
Address ReturnValue
ReturnValue - The temporary alloca to hold the return value.
void pushStackRestore(CleanupKind kind, Address SPMem)
Definition: CGDecl.cpp:1492
bool isConstexpr() const
Whether this variable is (C++11) constexpr.
Definition: Decl.h:1299
unsigned kind
All of the diagnostics that can be emitted by the frontend.
Definition: DiagnosticIDs.h:44
bool isArrayType() const
Definition: Type.h:5751
std::pair< llvm::Value *, QualType > getVLASize(const VariableArrayType *vla)
getVLASize - Returns an LLVM value that corresponds to the size, in non-variably-sized elements...
Defines the clang::TargetInfo interface.
QualType getType() const
Definition: CGValue.h:277
void setLocation(SourceLocation Loc)
Update the current source location.
unsigned getTargetAddressSpace(QualType T) const
Definition: ASTContext.h:2321
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:953
ExprValueKind getValueKind() const
getValueKind - The value kind that this expression produces.
Definition: Expr.h:402
static RValue get(llvm::Value *V)
Definition: CGValue.h:85
void EmitLifetimeEnd(llvm::Value *Size, llvm::Value *Addr)
Definition: CGDecl.cpp:943
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:2591
An l-value expression is a reference to an object with independent storage.
Definition: Specifiers.h:110
SourceLocation getLocation() const
Definition: DeclBase.h:407
LValue - This represents an lvalue references.
Definition: CGValue.h:171
Information for lazily generating a cleanup.
Definition: EHScopeStack.h:147
void EmitVarAnnotations(const VarDecl *D, llvm::Value *V)
Emit local annotations for the local variable V, declared by D.
Automatic storage duration (most local variables).
Definition: Specifiers.h:274
SanitizerMetadata * getSanitizerMetadata()
bool CurFuncIsThunk
In C++, whether we are code generating a thunk.
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...
StorageDuration getStorageDuration() const
Get the storage duration of this variable, per C++ [basic.stc].
Definition: Decl.h:1008
Optional< NullabilityKind > getNullability(const ASTContext &context) const
Determine the nullability of the given type.
Definition: Type.cpp:3526
CallArgList - Type for representing both the value and type of arguments in a call.
Definition: CGCall.h:182
Address CreateMemTemp(QualType T, const Twine &Name="tmp", bool CastToDefaultAddrSpace=true)
CreateMemTemp - Create a temporary memory object of the given type, with appropriate alignment...
Definition: CGExpr.cpp:123
void PopCleanupBlock(bool FallThroughIsBranchThrough=false)
PopCleanupBlock - Will pop the cleanup entry on the stack and process all branch fixups.
Definition: CGCleanup.cpp:640
QualType getBaseElementType(const ArrayType *VAT) const
Return the innermost element type of an array type.
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:2152
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:1704
RValue EmitCall(const CGFunctionInfo &CallInfo, const CGCallee &Callee, ReturnValueSlot ReturnValue, const CallArgList &Args, llvm::Instruction **callOrInvoke=nullptr)
EmitCall - Generate a call of the given function, expecting the given result type, and using the given argument list which specifies both the LLVM arguments and the types they were derived from.
Definition: CGCall.cpp:3695
bool hasLocalStorage() const
hasLocalStorage - Returns true if a variable with function scope is a non-static local variable...
Definition: Decl.h:963
Expr * IgnoreParens() LLVM_READONLY
IgnoreParens - Ignore parentheses.
Definition: Expr.cpp:2368
bool hasLabelBeenSeenInCurrentScope() const
Return true if a label was seen in the current scope.
Qualifiers getQualifiers() const
Retrieve the set of qualifiers applied to this type.
Definition: Type.h:5516
void Destroyer(CodeGenFunction &CGF, Address addr, QualType ty)
static CharUnits getDeclAlign(Expr *E, CharUnits TypeAlign, ASTContext &Context)
A helper function to get the alignment of a Decl referred to by DeclRefExpr or MemberExpr.