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