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