clang  10.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).getAsAlign());
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 /// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the
309 /// global variable that has already been created for it. If the initializer
310 /// has a different type than GV does, this may free GV and return a different
311 /// one. Otherwise it just returns GV.
312 llvm::GlobalVariable *
314  llvm::GlobalVariable *GV) {
315  ConstantEmitter emitter(*this);
316  llvm::Constant *Init = emitter.tryEmitForInitializer(D);
317 
318  // If constant emission failed, then this should be a C++ static
319  // initializer.
320  if (!Init) {
321  if (!getLangOpts().CPlusPlus)
322  CGM.ErrorUnsupported(D.getInit(), "constant l-value expression");
323  else if (HaveInsertPoint()) {
324  // Since we have a static initializer, this global variable can't
325  // be constant.
326  GV->setConstant(false);
327 
328  EmitCXXGuardedInit(D, GV, /*PerformInit*/true);
329  }
330  return GV;
331  }
332 
333  // The initializer may differ in type from the global. Rewrite
334  // the global to match the initializer. (We have to do this
335  // because some types, like unions, can't be completely represented
336  // in the LLVM type system.)
337  if (GV->getType()->getElementType() != Init->getType()) {
338  llvm::GlobalVariable *OldGV = GV;
339 
340  GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
341  OldGV->isConstant(),
342  OldGV->getLinkage(), Init, "",
343  /*InsertBefore*/ OldGV,
344  OldGV->getThreadLocalMode(),
346  GV->setVisibility(OldGV->getVisibility());
347  GV->setDSOLocal(OldGV->isDSOLocal());
348  GV->setComdat(OldGV->getComdat());
349 
350  // Steal the name of the old global
351  GV->takeName(OldGV);
352 
353  // Replace all uses of the old global with the new global
354  llvm::Constant *NewPtrForOldDecl =
355  llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
356  OldGV->replaceAllUsesWith(NewPtrForOldDecl);
357 
358  // Erase the old global, since it is no longer used.
359  OldGV->eraseFromParent();
360  }
361 
362  GV->setConstant(CGM.isTypeConstant(D.getType(), true));
363  GV->setInitializer(Init);
364 
365  emitter.finalize(GV);
366 
368  // We have a constant initializer, but a nontrivial destructor. We still
369  // need to perform a guarded "initialization" in order to register the
370  // destructor.
371  EmitCXXGuardedInit(D, GV, /*PerformInit*/false);
372  }
373 
374  return GV;
375 }
376 
378  llvm::GlobalValue::LinkageTypes Linkage) {
379  // Check to see if we already have a global variable for this
380  // declaration. This can happen when double-emitting function
381  // bodies, e.g. with complete and base constructors.
382  llvm::Constant *addr = CGM.getOrCreateStaticVarDecl(D, Linkage);
383  CharUnits alignment = getContext().getDeclAlign(&D);
384 
385  // Store into LocalDeclMap before generating initializer to handle
386  // circular references.
387  setAddrOfLocalVar(&D, Address(addr, alignment));
388 
389  // We can't have a VLA here, but we can have a pointer to a VLA,
390  // even though that doesn't really make any sense.
391  // Make sure to evaluate VLA bounds now so that we have them for later.
392  if (D.getType()->isVariablyModifiedType())
394 
395  // Save the type in case adding the initializer forces a type change.
396  llvm::Type *expectedType = addr->getType();
397 
398  llvm::GlobalVariable *var =
399  cast<llvm::GlobalVariable>(addr->stripPointerCasts());
400 
401  // CUDA's local and local static __shared__ variables should not
402  // have any non-empty initializers. This is ensured by Sema.
403  // Whatever initializer such variable may have when it gets here is
404  // a no-op and should not be emitted.
405  bool isCudaSharedVar = getLangOpts().CUDA && getLangOpts().CUDAIsDevice &&
406  D.hasAttr<CUDASharedAttr>();
407  // If this value has an initializer, emit it.
408  if (D.getInit() && !isCudaSharedVar)
409  var = AddInitializerToStaticVarDecl(D, var);
410 
411  var->setAlignment(alignment.getAsAlign());
412 
413  if (D.hasAttr<AnnotateAttr>())
414  CGM.AddGlobalAnnotations(&D, var);
415 
416  if (auto *SA = D.getAttr<PragmaClangBSSSectionAttr>())
417  var->addAttribute("bss-section", SA->getName());
418  if (auto *SA = D.getAttr<PragmaClangDataSectionAttr>())
419  var->addAttribute("data-section", SA->getName());
420  if (auto *SA = D.getAttr<PragmaClangRodataSectionAttr>())
421  var->addAttribute("rodata-section", SA->getName());
422  if (auto *SA = D.getAttr<PragmaClangRelroSectionAttr>())
423  var->addAttribute("relro-section", SA->getName());
424 
425  if (const SectionAttr *SA = D.getAttr<SectionAttr>())
426  var->setSection(SA->getName());
427 
428  if (D.hasAttr<UsedAttr>())
429  CGM.addUsedGlobal(var);
430 
431  // We may have to cast the constant because of the initializer
432  // mismatch above.
433  //
434  // FIXME: It is really dangerous to store this in the map; if anyone
435  // RAUW's the GV uses of this constant will be invalid.
436  llvm::Constant *castedAddr =
437  llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(var, expectedType);
438  if (var != castedAddr)
439  LocalDeclMap.find(&D)->second = Address(castedAddr, alignment);
440  CGM.setStaticLocalDeclAddress(&D, castedAddr);
441 
443 
444  // Emit global variable debug descriptor for static vars.
445  CGDebugInfo *DI = getDebugInfo();
446  if (DI &&
448  DI->setLocation(D.getLocation());
449  DI->EmitGlobalVariable(var, &D);
450  }
451 }
452 
453 namespace {
454  struct DestroyObject final : EHScopeStack::Cleanup {
455  DestroyObject(Address addr, QualType type,
456  CodeGenFunction::Destroyer *destroyer,
457  bool useEHCleanupForArray)
458  : addr(addr), type(type), destroyer(destroyer),
459  useEHCleanupForArray(useEHCleanupForArray) {}
460 
461  Address addr;
462  QualType type;
463  CodeGenFunction::Destroyer *destroyer;
464  bool useEHCleanupForArray;
465 
466  void Emit(CodeGenFunction &CGF, Flags flags) override {
467  // Don't use an EH cleanup recursively from an EH cleanup.
468  bool useEHCleanupForArray =
469  flags.isForNormalCleanup() && this->useEHCleanupForArray;
470 
471  CGF.emitDestroy(addr, type, destroyer, useEHCleanupForArray);
472  }
473  };
474 
475  template <class Derived>
476  struct DestroyNRVOVariable : EHScopeStack::Cleanup {
477  DestroyNRVOVariable(Address addr, QualType type, llvm::Value *NRVOFlag)
478  : NRVOFlag(NRVOFlag), Loc(addr), Ty(type) {}
479 
480  llvm::Value *NRVOFlag;
481  Address Loc;
482  QualType Ty;
483 
484  void Emit(CodeGenFunction &CGF, Flags flags) override {
485  // Along the exceptions path we always execute the dtor.
486  bool NRVO = flags.isForNormalCleanup() && NRVOFlag;
487 
488  llvm::BasicBlock *SkipDtorBB = nullptr;
489  if (NRVO) {
490  // If we exited via NRVO, we skip the destructor call.
491  llvm::BasicBlock *RunDtorBB = CGF.createBasicBlock("nrvo.unused");
492  SkipDtorBB = CGF.createBasicBlock("nrvo.skipdtor");
493  llvm::Value *DidNRVO =
494  CGF.Builder.CreateFlagLoad(NRVOFlag, "nrvo.val");
495  CGF.Builder.CreateCondBr(DidNRVO, SkipDtorBB, RunDtorBB);
496  CGF.EmitBlock(RunDtorBB);
497  }
498 
499  static_cast<Derived *>(this)->emitDestructorCall(CGF);
500 
501  if (NRVO) CGF.EmitBlock(SkipDtorBB);
502  }
503 
504  virtual ~DestroyNRVOVariable() = default;
505  };
506 
507  struct DestroyNRVOVariableCXX final
508  : DestroyNRVOVariable<DestroyNRVOVariableCXX> {
509  DestroyNRVOVariableCXX(Address addr, QualType type,
510  const CXXDestructorDecl *Dtor, llvm::Value *NRVOFlag)
511  : DestroyNRVOVariable<DestroyNRVOVariableCXX>(addr, type, NRVOFlag),
512  Dtor(Dtor) {}
513 
514  const CXXDestructorDecl *Dtor;
515 
516  void emitDestructorCall(CodeGenFunction &CGF) {
518  /*ForVirtualBase=*/false,
519  /*Delegating=*/false, Loc, Ty);
520  }
521  };
522 
523  struct DestroyNRVOVariableC final
524  : DestroyNRVOVariable<DestroyNRVOVariableC> {
525  DestroyNRVOVariableC(Address addr, llvm::Value *NRVOFlag, QualType Ty)
526  : DestroyNRVOVariable<DestroyNRVOVariableC>(addr, Ty, NRVOFlag) {}
527 
528  void emitDestructorCall(CodeGenFunction &CGF) {
529  CGF.destroyNonTrivialCStruct(CGF, Loc, Ty);
530  }
531  };
532 
533  struct CallStackRestore final : EHScopeStack::Cleanup {
534  Address Stack;
535  CallStackRestore(Address Stack) : Stack(Stack) {}
536  void Emit(CodeGenFunction &CGF, Flags flags) override {
537  llvm::Value *V = CGF.Builder.CreateLoad(Stack);
538  llvm::Function *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stackrestore);
539  CGF.Builder.CreateCall(F, V);
540  }
541  };
542 
543  struct ExtendGCLifetime final : EHScopeStack::Cleanup {
544  const VarDecl &Var;
545  ExtendGCLifetime(const VarDecl *var) : Var(*var) {}
546 
547  void Emit(CodeGenFunction &CGF, Flags flags) override {
548  // Compute the address of the local variable, in case it's a
549  // byref or something.
550  DeclRefExpr DRE(CGF.getContext(), const_cast<VarDecl *>(&Var), false,
551  Var.getType(), VK_LValue, SourceLocation());
552  llvm::Value *value = CGF.EmitLoadOfScalar(CGF.EmitDeclRefLValue(&DRE),
553  SourceLocation());
554  CGF.EmitExtendGCLifetime(value);
555  }
556  };
557 
558  struct CallCleanupFunction final : EHScopeStack::Cleanup {
559  llvm::Constant *CleanupFn;
560  const CGFunctionInfo &FnInfo;
561  const VarDecl &Var;
562 
563  CallCleanupFunction(llvm::Constant *CleanupFn, const CGFunctionInfo *Info,
564  const VarDecl *Var)
565  : CleanupFn(CleanupFn), FnInfo(*Info), Var(*Var) {}
566 
567  void Emit(CodeGenFunction &CGF, Flags flags) override {
568  DeclRefExpr DRE(CGF.getContext(), const_cast<VarDecl *>(&Var), false,
569  Var.getType(), VK_LValue, SourceLocation());
570  // Compute the address of the local variable, in case it's a byref
571  // or something.
572  llvm::Value *Addr = CGF.EmitDeclRefLValue(&DRE).getPointer();
573 
574  // In some cases, the type of the function argument will be different from
575  // the type of the pointer. An example of this is
576  // void f(void* arg);
577  // __attribute__((cleanup(f))) void *g;
578  //
579  // To fix this we insert a bitcast here.
580  QualType ArgTy = FnInfo.arg_begin()->type;
581  llvm::Value *Arg =
582  CGF.Builder.CreateBitCast(Addr, CGF.ConvertType(ArgTy));
583 
584  CallArgList Args;
585  Args.add(RValue::get(Arg),
586  CGF.getContext().getPointerType(Var.getType()));
587  auto Callee = CGCallee::forDirect(CleanupFn);
588  CGF.EmitCall(FnInfo, Callee, ReturnValueSlot(), Args);
589  }
590  };
591 } // end anonymous namespace
592 
593 /// EmitAutoVarWithLifetime - Does the setup required for an automatic
594 /// variable with lifetime.
595 static void EmitAutoVarWithLifetime(CodeGenFunction &CGF, const VarDecl &var,
596  Address addr,
597  Qualifiers::ObjCLifetime lifetime) {
598  switch (lifetime) {
600  llvm_unreachable("present but none");
601 
603  // nothing to do
604  break;
605 
606  case Qualifiers::OCL_Strong: {
607  CodeGenFunction::Destroyer *destroyer =
608  (var.hasAttr<ObjCPreciseLifetimeAttr>()
611 
612  CleanupKind cleanupKind = CGF.getARCCleanupKind();
613  CGF.pushDestroy(cleanupKind, addr, var.getType(), destroyer,
614  cleanupKind & EHCleanup);
615  break;
616  }
618  // nothing to do
619  break;
620 
622  // __weak objects always get EH cleanups; otherwise, exceptions
623  // could cause really nasty crashes instead of mere leaks.
624  CGF.pushDestroy(NormalAndEHCleanup, addr, var.getType(),
626  /*useEHCleanup*/ true);
627  break;
628  }
629 }
630 
631 static bool isAccessedBy(const VarDecl &var, const Stmt *s) {
632  if (const Expr *e = dyn_cast<Expr>(s)) {
633  // Skip the most common kinds of expressions that make
634  // hierarchy-walking expensive.
635  s = e = e->IgnoreParenCasts();
636 
637  if (const DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e))
638  return (ref->getDecl() == &var);
639  if (const BlockExpr *be = dyn_cast<BlockExpr>(e)) {
640  const BlockDecl *block = be->getBlockDecl();
641  for (const auto &I : block->captures()) {
642  if (I.getVariable() == &var)
643  return true;
644  }
645  }
646  }
647 
648  for (const Stmt *SubStmt : s->children())
649  // SubStmt might be null; as in missing decl or conditional of an if-stmt.
650  if (SubStmt && isAccessedBy(var, SubStmt))
651  return true;
652 
653  return false;
654 }
655 
656 static bool isAccessedBy(const ValueDecl *decl, const Expr *e) {
657  if (!decl) return false;
658  if (!isa<VarDecl>(decl)) return false;
659  const VarDecl *var = cast<VarDecl>(decl);
660  return isAccessedBy(*var, e);
661 }
662 
664  const LValue &destLV, const Expr *init) {
665  bool needsCast = false;
666 
667  while (auto castExpr = dyn_cast<CastExpr>(init->IgnoreParens())) {
668  switch (castExpr->getCastKind()) {
669  // Look through casts that don't require representation changes.
670  case CK_NoOp:
671  case CK_BitCast:
672  case CK_BlockPointerToObjCPointerCast:
673  needsCast = true;
674  break;
675 
676  // If we find an l-value to r-value cast from a __weak variable,
677  // emit this operation as a copy or move.
678  case CK_LValueToRValue: {
679  const Expr *srcExpr = castExpr->getSubExpr();
680  if (srcExpr->getType().getObjCLifetime() != Qualifiers::OCL_Weak)
681  return false;
682 
683  // Emit the source l-value.
684  LValue srcLV = CGF.EmitLValue(srcExpr);
685 
686  // Handle a formal type change to avoid asserting.
687  auto srcAddr = srcLV.getAddress();
688  if (needsCast) {
689  srcAddr = CGF.Builder.CreateElementBitCast(srcAddr,
690  destLV.getAddress().getElementType());
691  }
692 
693  // If it was an l-value, use objc_copyWeak.
694  if (srcExpr->getValueKind() == VK_LValue) {
695  CGF.EmitARCCopyWeak(destLV.getAddress(), srcAddr);
696  } else {
697  assert(srcExpr->getValueKind() == VK_XValue);
698  CGF.EmitARCMoveWeak(destLV.getAddress(), srcAddr);
699  }
700  return true;
701  }
702 
703  // Stop at anything else.
704  default:
705  return false;
706  }
707 
708  init = castExpr->getSubExpr();
709  }
710  return false;
711 }
712 
714  LValue &lvalue,
715  const VarDecl *var) {
716  lvalue.setAddress(CGF.emitBlockByrefAddress(lvalue.getAddress(), var));
717 }
718 
720  SourceLocation Loc) {
721  if (!SanOpts.has(SanitizerKind::NullabilityAssign))
722  return;
723 
724  auto Nullability = LHS.getType()->getNullability(getContext());
726  return;
727 
728  // Check if the right hand side of the assignment is nonnull, if the left
729  // hand side must be nonnull.
730  SanitizerScope SanScope(this);
731  llvm::Value *IsNotNull = Builder.CreateIsNotNull(RHS);
732  llvm::Constant *StaticData[] = {
734  llvm::ConstantInt::get(Int8Ty, 0), // The LogAlignment info is unused.
735  llvm::ConstantInt::get(Int8Ty, TCK_NonnullAssign)};
736  EmitCheck({{IsNotNull, SanitizerKind::NullabilityAssign}},
737  SanitizerHandler::TypeMismatch, StaticData, RHS);
738 }
739 
740 void CodeGenFunction::EmitScalarInit(const Expr *init, const ValueDecl *D,
741  LValue lvalue, bool capturedByInit) {
742  Qualifiers::ObjCLifetime lifetime = lvalue.getObjCLifetime();
743  if (!lifetime) {
744  llvm::Value *value = EmitScalarExpr(init);
745  if (capturedByInit)
746  drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
747  EmitNullabilityCheck(lvalue, value, init->getExprLoc());
748  EmitStoreThroughLValue(RValue::get(value), lvalue, true);
749  return;
750  }
751 
752  if (const CXXDefaultInitExpr *DIE = dyn_cast<CXXDefaultInitExpr>(init))
753  init = DIE->getExpr();
754 
755  // If we're emitting a value with lifetime, we have to do the
756  // initialization *before* we leave the cleanup scopes.
757  if (const FullExpr *fe = dyn_cast<FullExpr>(init)) {
759  init = fe->getSubExpr();
760  }
762 
763  // We have to maintain the illusion that the variable is
764  // zero-initialized. If the variable might be accessed in its
765  // initializer, zero-initialize before running the initializer, then
766  // actually perform the initialization with an assign.
767  bool accessedByInit = false;
768  if (lifetime != Qualifiers::OCL_ExplicitNone)
769  accessedByInit = (capturedByInit || isAccessedBy(D, init));
770  if (accessedByInit) {
771  LValue tempLV = lvalue;
772  // Drill down to the __block object if necessary.
773  if (capturedByInit) {
774  // We can use a simple GEP for this because it can't have been
775  // moved yet.
777  cast<VarDecl>(D),
778  /*follow*/ false));
779  }
780 
781  auto ty = cast<llvm::PointerType>(tempLV.getAddress().getElementType());
782  llvm::Value *zero = CGM.getNullPointer(ty, tempLV.getType());
783 
784  // If __weak, we want to use a barrier under certain conditions.
785  if (lifetime == Qualifiers::OCL_Weak)
786  EmitARCInitWeak(tempLV.getAddress(), zero);
787 
788  // Otherwise just do a simple store.
789  else
790  EmitStoreOfScalar(zero, tempLV, /* isInitialization */ true);
791  }
792 
793  // Emit the initializer.
794  llvm::Value *value = nullptr;
795 
796  switch (lifetime) {
798  llvm_unreachable("present but none");
799 
800  case Qualifiers::OCL_Strong: {
801  if (!D || !isa<VarDecl>(D) || !cast<VarDecl>(D)->isARCPseudoStrong()) {
802  value = EmitARCRetainScalarExpr(init);
803  break;
804  }
805  // If D is pseudo-strong, treat it like __unsafe_unretained here. This means
806  // that we omit the retain, and causes non-autoreleased return values to be
807  // immediately released.
808  LLVM_FALLTHROUGH;
809  }
810 
812  value = EmitARCUnsafeUnretainedScalarExpr(init);
813  break;
814 
815  case Qualifiers::OCL_Weak: {
816  // If it's not accessed by the initializer, try to emit the
817  // initialization with a copy or move.
818  if (!accessedByInit && tryEmitARCCopyWeakInit(*this, lvalue, init)) {
819  return;
820  }
821 
822  // No way to optimize a producing initializer into this. It's not
823  // worth optimizing for, because the value will immediately
824  // disappear in the common case.
825  value = EmitScalarExpr(init);
826 
827  if (capturedByInit) drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
828  if (accessedByInit)
829  EmitARCStoreWeak(lvalue.getAddress(), value, /*ignored*/ true);
830  else
831  EmitARCInitWeak(lvalue.getAddress(), value);
832  return;
833  }
834 
837  break;
838  }
839 
840  if (capturedByInit) drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
841 
842  EmitNullabilityCheck(lvalue, value, init->getExprLoc());
843 
844  // If the variable might have been accessed by its initializer, we
845  // might have to initialize with a barrier. We have to do this for
846  // both __weak and __strong, but __weak got filtered out above.
847  if (accessedByInit && lifetime == Qualifiers::OCL_Strong) {
848  llvm::Value *oldValue = EmitLoadOfScalar(lvalue, init->getExprLoc());
849  EmitStoreOfScalar(value, lvalue, /* isInitialization */ true);
851  return;
852  }
853 
854  EmitStoreOfScalar(value, lvalue, /* isInitialization */ true);
855 }
856 
857 /// Decide whether we can emit the non-zero parts of the specified initializer
858 /// with equal or fewer than NumStores scalar stores.
859 static bool canEmitInitWithFewStoresAfterBZero(llvm::Constant *Init,
860  unsigned &NumStores) {
861  // Zero and Undef never requires any extra stores.
862  if (isa<llvm::ConstantAggregateZero>(Init) ||
863  isa<llvm::ConstantPointerNull>(Init) ||
864  isa<llvm::UndefValue>(Init))
865  return true;
866  if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) ||
867  isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) ||
868  isa<llvm::ConstantExpr>(Init))
869  return Init->isNullValue() || NumStores--;
870 
871  // See if we can emit each element.
872  if (isa<llvm::ConstantArray>(Init) || isa<llvm::ConstantStruct>(Init)) {
873  for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) {
874  llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i));
875  if (!canEmitInitWithFewStoresAfterBZero(Elt, NumStores))
876  return false;
877  }
878  return true;
879  }
880 
881  if (llvm::ConstantDataSequential *CDS =
882  dyn_cast<llvm::ConstantDataSequential>(Init)) {
883  for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
884  llvm::Constant *Elt = CDS->getElementAsConstant(i);
885  if (!canEmitInitWithFewStoresAfterBZero(Elt, NumStores))
886  return false;
887  }
888  return true;
889  }
890 
891  // Anything else is hard and scary.
892  return false;
893 }
894 
895 /// For inits that canEmitInitWithFewStoresAfterBZero returned true for, emit
896 /// the scalar stores that would be required.
898  llvm::Constant *Init, Address Loc,
899  bool isVolatile, CGBuilderTy &Builder) {
900  assert(!Init->isNullValue() && !isa<llvm::UndefValue>(Init) &&
901  "called emitStoresForInitAfterBZero for zero or undef value.");
902 
903  if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) ||
904  isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) ||
905  isa<llvm::ConstantExpr>(Init)) {
906  Builder.CreateStore(Init, Loc, isVolatile);
907  return;
908  }
909 
910  if (llvm::ConstantDataSequential *CDS =
911  dyn_cast<llvm::ConstantDataSequential>(Init)) {
912  for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
913  llvm::Constant *Elt = CDS->getElementAsConstant(i);
914 
915  // If necessary, get a pointer to the element and emit it.
916  if (!Elt->isNullValue() && !isa<llvm::UndefValue>(Elt))
918  CGM, Elt, Builder.CreateConstInBoundsGEP2_32(Loc, 0, i), isVolatile,
919  Builder);
920  }
921  return;
922  }
923 
924  assert((isa<llvm::ConstantStruct>(Init) || isa<llvm::ConstantArray>(Init)) &&
925  "Unknown value type!");
926 
927  for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) {
928  llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i));
929 
930  // If necessary, get a pointer to the element and emit it.
931  if (!Elt->isNullValue() && !isa<llvm::UndefValue>(Elt))
933  Builder.CreateConstInBoundsGEP2_32(Loc, 0, i),
934  isVolatile, Builder);
935  }
936 }
937 
938 /// Decide whether we should use bzero plus some stores to initialize a local
939 /// variable instead of using a memcpy from a constant global. It is beneficial
940 /// to use bzero if the global is all zeros, or mostly zeros and large.
941 static bool shouldUseBZeroPlusStoresToInitialize(llvm::Constant *Init,
942  uint64_t GlobalSize) {
943  // If a global is all zeros, always use a bzero.
944  if (isa<llvm::ConstantAggregateZero>(Init)) return true;
945 
946  // If a non-zero global is <= 32 bytes, always use a memcpy. If it is large,
947  // do it if it will require 6 or fewer scalar stores.
948  // TODO: Should budget depends on the size? Avoiding a large global warrants
949  // plopping in more stores.
950  unsigned StoreBudget = 6;
951  uint64_t SizeLimit = 32;
952 
953  return GlobalSize > SizeLimit &&
954  canEmitInitWithFewStoresAfterBZero(Init, StoreBudget);
955 }
956 
957 /// Decide whether we should use memset to initialize a local variable instead
958 /// of using a memcpy from a constant global. Assumes we've already decided to
959 /// not user bzero.
960 /// FIXME We could be more clever, as we are for bzero above, and generate
961 /// memset followed by stores. It's unclear that's worth the effort.
962 static llvm::Value *shouldUseMemSetToInitialize(llvm::Constant *Init,
963  uint64_t GlobalSize,
964  const llvm::DataLayout &DL) {
965  uint64_t SizeLimit = 32;
966  if (GlobalSize <= SizeLimit)
967  return nullptr;
968  return llvm::isBytewiseValue(Init, DL);
969 }
970 
971 /// Decide whether we want to split a constant structure or array store into a
972 /// sequence of its fields' stores. This may cost us code size and compilation
973 /// speed, but plays better with store optimizations.
975  uint64_t GlobalByteSize) {
976  // Don't break things that occupy more than one cacheline.
977  uint64_t ByteSizeLimit = 64;
978  if (CGM.getCodeGenOpts().OptimizationLevel == 0)
979  return false;
980  if (GlobalByteSize <= ByteSizeLimit)
981  return true;
982  return false;
983 }
984 
985 enum class IsPattern { No, Yes };
986 
987 /// Generate a constant filled with either a pattern or zeroes.
988 static llvm::Constant *patternOrZeroFor(CodeGenModule &CGM, IsPattern isPattern,
989  llvm::Type *Ty) {
990  if (isPattern == IsPattern::Yes)
991  return initializationPatternFor(CGM, Ty);
992  else
993  return llvm::Constant::getNullValue(Ty);
994 }
995 
996 static llvm::Constant *constWithPadding(CodeGenModule &CGM, IsPattern isPattern,
997  llvm::Constant *constant);
998 
999 /// Helper function for constWithPadding() to deal with padding in structures.
1000 static llvm::Constant *constStructWithPadding(CodeGenModule &CGM,
1001  IsPattern isPattern,
1002  llvm::StructType *STy,
1003  llvm::Constant *constant) {
1004  const llvm::DataLayout &DL = CGM.getDataLayout();
1005  const llvm::StructLayout *Layout = DL.getStructLayout(STy);
1006  llvm::Type *Int8Ty = llvm::IntegerType::getInt8Ty(CGM.getLLVMContext());
1007  unsigned SizeSoFar = 0;
1009  bool NestedIntact = true;
1010  for (unsigned i = 0, e = STy->getNumElements(); i != e; i++) {
1011  unsigned CurOff = Layout->getElementOffset(i);
1012  if (SizeSoFar < CurOff) {
1013  assert(!STy->isPacked());
1014  auto *PadTy = llvm::ArrayType::get(Int8Ty, CurOff - SizeSoFar);
1015  Values.push_back(patternOrZeroFor(CGM, isPattern, PadTy));
1016  }
1017  llvm::Constant *CurOp;
1018  if (constant->isZeroValue())
1019  CurOp = llvm::Constant::getNullValue(STy->getElementType(i));
1020  else
1021  CurOp = cast<llvm::Constant>(constant->getAggregateElement(i));
1022  auto *NewOp = constWithPadding(CGM, isPattern, CurOp);
1023  if (CurOp != NewOp)
1024  NestedIntact = false;
1025  Values.push_back(NewOp);
1026  SizeSoFar = CurOff + DL.getTypeAllocSize(CurOp->getType());
1027  }
1028  unsigned TotalSize = Layout->getSizeInBytes();
1029  if (SizeSoFar < TotalSize) {
1030  auto *PadTy = llvm::ArrayType::get(Int8Ty, TotalSize - SizeSoFar);
1031  Values.push_back(patternOrZeroFor(CGM, isPattern, PadTy));
1032  }
1033  if (NestedIntact && Values.size() == STy->getNumElements())
1034  return constant;
1035  return llvm::ConstantStruct::getAnon(Values, STy->isPacked());
1036 }
1037 
1038 /// Replace all padding bytes in a given constant with either a pattern byte or
1039 /// 0x00.
1040 static llvm::Constant *constWithPadding(CodeGenModule &CGM, IsPattern isPattern,
1041  llvm::Constant *constant) {
1042  llvm::Type *OrigTy = constant->getType();
1043  if (const auto STy = dyn_cast<llvm::StructType>(OrigTy))
1044  return constStructWithPadding(CGM, isPattern, STy, constant);
1045  if (auto *STy = dyn_cast<llvm::SequentialType>(OrigTy)) {
1047  unsigned Size = STy->getNumElements();
1048  if (!Size)
1049  return constant;
1050  llvm::Type *ElemTy = STy->getElementType();
1051  bool ZeroInitializer = constant->isZeroValue();
1052  llvm::Constant *OpValue, *PaddedOp;
1053  if (ZeroInitializer) {
1054  OpValue = llvm::Constant::getNullValue(ElemTy);
1055  PaddedOp = constWithPadding(CGM, isPattern, OpValue);
1056  }
1057  for (unsigned Op = 0; Op != Size; ++Op) {
1058  if (!ZeroInitializer) {
1059  OpValue = constant->getAggregateElement(Op);
1060  PaddedOp = constWithPadding(CGM, isPattern, OpValue);
1061  }
1062  Values.push_back(PaddedOp);
1063  }
1064  auto *NewElemTy = Values[0]->getType();
1065  if (NewElemTy == ElemTy)
1066  return constant;
1067  if (OrigTy->isArrayTy()) {
1068  auto *ArrayTy = llvm::ArrayType::get(NewElemTy, Size);
1069  return llvm::ConstantArray::get(ArrayTy, Values);
1070  } else {
1071  return llvm::ConstantVector::get(Values);
1072  }
1073  }
1074  return constant;
1075 }
1076 
1078  llvm::Constant *Constant,
1079  CharUnits Align) {
1080  auto FunctionName = [&](const DeclContext *DC) -> std::string {
1081  if (const auto *FD = dyn_cast<FunctionDecl>(DC)) {
1082  if (const auto *CC = dyn_cast<CXXConstructorDecl>(FD))
1083  return CC->getNameAsString();
1084  if (const auto *CD = dyn_cast<CXXDestructorDecl>(FD))
1085  return CD->getNameAsString();
1086  return getMangledName(FD);
1087  } else if (const auto *OM = dyn_cast<ObjCMethodDecl>(DC)) {
1088  return OM->getNameAsString();
1089  } else if (isa<BlockDecl>(DC)) {
1090  return "<block>";
1091  } else if (isa<CapturedDecl>(DC)) {
1092  return "<captured>";
1093  } else {
1094  llvm_unreachable("expected a function or method");
1095  }
1096  };
1097 
1098  // Form a simple per-variable cache of these values in case we find we
1099  // want to reuse them.
1100  llvm::GlobalVariable *&CacheEntry = InitializerConstants[&D];
1101  if (!CacheEntry || CacheEntry->getInitializer() != Constant) {
1102  auto *Ty = Constant->getType();
1103  bool isConstant = true;
1104  llvm::GlobalVariable *InsertBefore = nullptr;
1105  unsigned AS =
1106  getContext().getTargetAddressSpace(getStringLiteralAddressSpace());
1107  std::string Name;
1108  if (D.hasGlobalStorage())
1109  Name = getMangledName(&D).str() + ".const";
1110  else if (const DeclContext *DC = D.getParentFunctionOrMethod())
1111  Name = ("__const." + FunctionName(DC) + "." + D.getName()).str();
1112  else
1113  llvm_unreachable("local variable has no parent function or method");
1114  llvm::GlobalVariable *GV = new llvm::GlobalVariable(
1115  getModule(), Ty, isConstant, llvm::GlobalValue::PrivateLinkage,
1116  Constant, Name, InsertBefore, llvm::GlobalValue::NotThreadLocal, AS);
1117  GV->setAlignment(Align.getAsAlign());
1118  GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
1119  CacheEntry = GV;
1120  } else if (CacheEntry->getAlignment() < Align.getQuantity()) {
1121  CacheEntry->setAlignment(Align.getAsAlign());
1122  }
1123 
1124  return Address(CacheEntry, Align);
1125 }
1126 
1128  const VarDecl &D,
1130  llvm::Constant *Constant,
1131  CharUnits Align) {
1132  Address SrcPtr = CGM.createUnnamedGlobalFrom(D, Constant, Align);
1133  llvm::Type *BP = llvm::PointerType::getInt8PtrTy(CGM.getLLVMContext(),
1134  SrcPtr.getAddressSpace());
1135  if (SrcPtr.getType() != BP)
1136  SrcPtr = Builder.CreateBitCast(SrcPtr, BP);
1137  return SrcPtr;
1138 }
1139 
1140 static void emitStoresForConstant(CodeGenModule &CGM, const VarDecl &D,
1141  Address Loc, bool isVolatile,
1143  llvm::Constant *constant) {
1144  auto *Ty = constant->getType();
1145  uint64_t ConstantSize = CGM.getDataLayout().getTypeAllocSize(Ty);
1146  if (!ConstantSize)
1147  return;
1148 
1149  bool canDoSingleStore = Ty->isIntOrIntVectorTy() ||
1150  Ty->isPtrOrPtrVectorTy() || Ty->isFPOrFPVectorTy();
1151  if (canDoSingleStore) {
1152  Builder.CreateStore(constant, Loc, isVolatile);
1153  return;
1154  }
1155 
1156  auto *SizeVal = llvm::ConstantInt::get(CGM.IntPtrTy, ConstantSize);
1157 
1158  // If the initializer is all or mostly the same, codegen with bzero / memset
1159  // then do a few stores afterward.
1160  if (shouldUseBZeroPlusStoresToInitialize(constant, ConstantSize)) {
1161  Builder.CreateMemSet(Loc, llvm::ConstantInt::get(CGM.Int8Ty, 0), SizeVal,
1162  isVolatile);
1163 
1164  bool valueAlreadyCorrect =
1165  constant->isNullValue() || isa<llvm::UndefValue>(constant);
1166  if (!valueAlreadyCorrect) {
1167  Loc = Builder.CreateBitCast(Loc, Ty->getPointerTo(Loc.getAddressSpace()));
1168  emitStoresForInitAfterBZero(CGM, constant, Loc, isVolatile, Builder);
1169  }
1170  return;
1171  }
1172 
1173  // If the initializer is a repeated byte pattern, use memset.
1174  llvm::Value *Pattern =
1175  shouldUseMemSetToInitialize(constant, ConstantSize, CGM.getDataLayout());
1176  if (Pattern) {
1177  uint64_t Value = 0x00;
1178  if (!isa<llvm::UndefValue>(Pattern)) {
1179  const llvm::APInt &AP = cast<llvm::ConstantInt>(Pattern)->getValue();
1180  assert(AP.getBitWidth() <= 8);
1181  Value = AP.getLimitedValue();
1182  }
1183  Builder.CreateMemSet(Loc, llvm::ConstantInt::get(CGM.Int8Ty, Value), SizeVal,
1184  isVolatile);
1185  return;
1186  }
1187 
1188  // If the initializer is small, use a handful of stores.
1189  if (shouldSplitConstantStore(CGM, ConstantSize)) {
1190  if (auto *STy = dyn_cast<llvm::StructType>(Ty)) {
1191  // FIXME: handle the case when STy != Loc.getElementType().
1192  if (STy == Loc.getElementType()) {
1193  for (unsigned i = 0; i != constant->getNumOperands(); i++) {
1194  Address EltPtr = Builder.CreateStructGEP(Loc, i);
1196  CGM, D, EltPtr, isVolatile, Builder,
1197  cast<llvm::Constant>(Builder.CreateExtractValue(constant, i)));
1198  }
1199  return;
1200  }
1201  } else if (auto *ATy = dyn_cast<llvm::ArrayType>(Ty)) {
1202  // FIXME: handle the case when ATy != Loc.getElementType().
1203  if (ATy == Loc.getElementType()) {
1204  for (unsigned i = 0; i != ATy->getNumElements(); i++) {
1205  Address EltPtr = Builder.CreateConstArrayGEP(Loc, i);
1207  CGM, D, EltPtr, isVolatile, Builder,
1208  cast<llvm::Constant>(Builder.CreateExtractValue(constant, i)));
1209  }
1210  return;
1211  }
1212  }
1213  }
1214 
1215  // Copy from a global.
1216  Builder.CreateMemCpy(Loc,
1218  CGM, D, Builder, constant, Loc.getAlignment()),
1219  SizeVal, isVolatile);
1220 }
1221 
1222 static void emitStoresForZeroInit(CodeGenModule &CGM, const VarDecl &D,
1223  Address Loc, bool isVolatile,
1224  CGBuilderTy &Builder) {
1225  llvm::Type *ElTy = Loc.getElementType();
1226  llvm::Constant *constant =
1227  constWithPadding(CGM, IsPattern::No, llvm::Constant::getNullValue(ElTy));
1228  emitStoresForConstant(CGM, D, Loc, isVolatile, Builder, constant);
1229 }
1230 
1232  Address Loc, bool isVolatile,
1233  CGBuilderTy &Builder) {
1234  llvm::Type *ElTy = Loc.getElementType();
1235  llvm::Constant *constant = constWithPadding(
1236  CGM, IsPattern::Yes, initializationPatternFor(CGM, ElTy));
1237  assert(!isa<llvm::UndefValue>(constant));
1238  emitStoresForConstant(CGM, D, Loc, isVolatile, Builder, constant);
1239 }
1240 
1241 static bool containsUndef(llvm::Constant *constant) {
1242  auto *Ty = constant->getType();
1243  if (isa<llvm::UndefValue>(constant))
1244  return true;
1245  if (Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy())
1246  for (llvm::Use &Op : constant->operands())
1247  if (containsUndef(cast<llvm::Constant>(Op)))
1248  return true;
1249  return false;
1250 }
1251 
1252 static llvm::Constant *replaceUndef(CodeGenModule &CGM, IsPattern isPattern,
1253  llvm::Constant *constant) {
1254  auto *Ty = constant->getType();
1255  if (isa<llvm::UndefValue>(constant))
1256  return patternOrZeroFor(CGM, isPattern, Ty);
1257  if (!(Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy()))
1258  return constant;
1259  if (!containsUndef(constant))
1260  return constant;
1261  llvm::SmallVector<llvm::Constant *, 8> Values(constant->getNumOperands());
1262  for (unsigned Op = 0, NumOp = constant->getNumOperands(); Op != NumOp; ++Op) {
1263  auto *OpValue = cast<llvm::Constant>(constant->getOperand(Op));
1264  Values[Op] = replaceUndef(CGM, isPattern, OpValue);
1265  }
1266  if (Ty->isStructTy())
1267  return llvm::ConstantStruct::get(cast<llvm::StructType>(Ty), Values);
1268  if (Ty->isArrayTy())
1269  return llvm::ConstantArray::get(cast<llvm::ArrayType>(Ty), Values);
1270  assert(Ty->isVectorTy());
1271  return llvm::ConstantVector::get(Values);
1272 }
1273 
1274 /// EmitAutoVarDecl - Emit code and set up an entry in LocalDeclMap for a
1275 /// variable declaration with auto, register, or no storage class specifier.
1276 /// These turn into simple stack objects, or GlobalValues depending on target.
1278  AutoVarEmission emission = EmitAutoVarAlloca(D);
1279  EmitAutoVarInit(emission);
1280  EmitAutoVarCleanups(emission);
1281 }
1282 
1283 /// Emit a lifetime.begin marker if some criteria are satisfied.
1284 /// \return a pointer to the temporary size Value if a marker was emitted, null
1285 /// otherwise
1287  llvm::Value *Addr) {
1288  if (!ShouldEmitLifetimeMarkers)
1289  return nullptr;
1290 
1291  assert(Addr->getType()->getPointerAddressSpace() ==
1292  CGM.getDataLayout().getAllocaAddrSpace() &&
1293  "Pointer should be in alloca address space");
1294  llvm::Value *SizeV = llvm::ConstantInt::get(Int64Ty, Size);
1295  Addr = Builder.CreateBitCast(Addr, AllocaInt8PtrTy);
1296  llvm::CallInst *C =
1297  Builder.CreateCall(CGM.getLLVMLifetimeStartFn(), {SizeV, Addr});
1298  C->setDoesNotThrow();
1299  return SizeV;
1300 }
1301 
1303  assert(Addr->getType()->getPointerAddressSpace() ==
1304  CGM.getDataLayout().getAllocaAddrSpace() &&
1305  "Pointer should be in alloca address space");
1306  Addr = Builder.CreateBitCast(Addr, AllocaInt8PtrTy);
1307  llvm::CallInst *C =
1308  Builder.CreateCall(CGM.getLLVMLifetimeEndFn(), {Size, Addr});
1309  C->setDoesNotThrow();
1310 }
1311 
1313  CGDebugInfo *DI, const VarDecl &D, bool EmitDebugInfo) {
1314  // For each dimension stores its QualType and corresponding
1315  // size-expression Value.
1317  SmallVector<IdentifierInfo *, 4> VLAExprNames;
1318 
1319  // Break down the array into individual dimensions.
1320  QualType Type1D = D.getType();
1321  while (getContext().getAsVariableArrayType(Type1D)) {
1322  auto VlaSize = getVLAElements1D(Type1D);
1323  if (auto *C = dyn_cast<llvm::ConstantInt>(VlaSize.NumElts))
1324  Dimensions.emplace_back(C, Type1D.getUnqualifiedType());
1325  else {
1326  // Generate a locally unique name for the size expression.
1327  Twine Name = Twine("__vla_expr") + Twine(VLAExprCounter++);
1328  SmallString<12> Buffer;
1329  StringRef NameRef = Name.toStringRef(Buffer);
1330  auto &Ident = getContext().Idents.getOwn(NameRef);
1331  VLAExprNames.push_back(&Ident);
1332  auto SizeExprAddr =
1333  CreateDefaultAlignTempAlloca(VlaSize.NumElts->getType(), NameRef);
1334  Builder.CreateStore(VlaSize.NumElts, SizeExprAddr);
1335  Dimensions.emplace_back(SizeExprAddr.getPointer(),
1336  Type1D.getUnqualifiedType());
1337  }
1338  Type1D = VlaSize.Type;
1339  }
1340 
1341  if (!EmitDebugInfo)
1342  return;
1343 
1344  // Register each dimension's size-expression with a DILocalVariable,
1345  // so that it can be used by CGDebugInfo when instantiating a DISubrange
1346  // to describe this array.
1347  unsigned NameIdx = 0;
1348  for (auto &VlaSize : Dimensions) {
1349  llvm::Metadata *MD;
1350  if (auto *C = dyn_cast<llvm::ConstantInt>(VlaSize.NumElts))
1351  MD = llvm::ConstantAsMetadata::get(C);
1352  else {
1353  // Create an artificial VarDecl to generate debug info for.
1354  IdentifierInfo *NameIdent = VLAExprNames[NameIdx++];
1355  auto VlaExprTy = VlaSize.NumElts->getType()->getPointerElementType();
1356  auto QT = getContext().getIntTypeForBitwidth(
1357  VlaExprTy->getScalarSizeInBits(), false);
1358  auto *ArtificialDecl = VarDecl::Create(
1359  getContext(), const_cast<DeclContext *>(D.getDeclContext()),
1360  D.getLocation(), D.getLocation(), NameIdent, QT,
1362  ArtificialDecl->setImplicit();
1363 
1364  MD = DI->EmitDeclareOfAutoVariable(ArtificialDecl, VlaSize.NumElts,
1365  Builder);
1366  }
1367  assert(MD && "No Size expression debug node created");
1368  DI->registerVLASizeExpression(VlaSize.Type, MD);
1369  }
1370 }
1371 
1372 /// EmitAutoVarAlloca - Emit the alloca and debug information for a
1373 /// local variable. Does not emit initialization or destruction.
1376  QualType Ty = D.getType();
1377  assert(
1378  Ty.getAddressSpace() == LangAS::Default ||
1379  (Ty.getAddressSpace() == LangAS::opencl_private && getLangOpts().OpenCL));
1380 
1381  AutoVarEmission emission(D);
1382 
1383  bool isEscapingByRef = D.isEscapingByref();
1384  emission.IsEscapingByRef = isEscapingByRef;
1385 
1386  CharUnits alignment = getContext().getDeclAlign(&D);
1387 
1388  // If the type is variably-modified, emit all the VLA sizes for it.
1389  if (Ty->isVariablyModifiedType())
1391 
1392  auto *DI = getDebugInfo();
1393  bool EmitDebugInfo = DI && CGM.getCodeGenOpts().getDebugInfo() >=
1395 
1396  Address address = Address::invalid();
1397  Address AllocaAddr = Address::invalid();
1398  Address OpenMPLocalAddr =
1399  getLangOpts().OpenMP
1400  ? CGM.getOpenMPRuntime().getAddressOfLocalVariable(*this, &D)
1401  : Address::invalid();
1402  bool NRVO = getLangOpts().ElideConstructors && D.isNRVOVariable();
1403 
1404  if (getLangOpts().OpenMP && OpenMPLocalAddr.isValid()) {
1405  address = OpenMPLocalAddr;
1406  } else if (Ty->isConstantSizeType()) {
1407  // If this value is an array or struct with a statically determinable
1408  // constant initializer, there are optimizations we can do.
1409  //
1410  // TODO: We should constant-evaluate the initializer of any variable,
1411  // as long as it is initialized by a constant expression. Currently,
1412  // isConstantInitializer produces wrong answers for structs with
1413  // reference or bitfield members, and a few other cases, and checking
1414  // for POD-ness protects us from some of these.
1415  if (D.getInit() && (Ty->isArrayType() || Ty->isRecordType()) &&
1416  (D.isConstexpr() ||
1417  ((Ty.isPODType(getContext()) ||
1419  D.getInit()->isConstantInitializer(getContext(), false)))) {
1420 
1421  // If the variable's a const type, and it's neither an NRVO
1422  // candidate nor a __block variable and has no mutable members,
1423  // emit it as a global instead.
1424  // Exception is if a variable is located in non-constant address space
1425  // in OpenCL.
1426  if ((!getLangOpts().OpenCL ||
1428  (CGM.getCodeGenOpts().MergeAllConstants && !NRVO &&
1429  !isEscapingByRef && CGM.isTypeConstant(Ty, true))) {
1431 
1432  // Signal this condition to later callbacks.
1433  emission.Addr = Address::invalid();
1434  assert(emission.wasEmittedAsGlobal());
1435  return emission;
1436  }
1437 
1438  // Otherwise, tell the initialization code that we're in this case.
1439  emission.IsConstantAggregate = true;
1440  }
1441 
1442  // A normal fixed sized variable becomes an alloca in the entry block,
1443  // unless:
1444  // - it's an NRVO variable.
1445  // - we are compiling OpenMP and it's an OpenMP local variable.
1446  if (NRVO) {
1447  // The named return value optimization: allocate this variable in the
1448  // return slot, so that we can elide the copy when returning this
1449  // variable (C++0x [class.copy]p34).
1450  address = ReturnValue;
1451 
1452  if (const RecordType *RecordTy = Ty->getAs<RecordType>()) {
1453  const auto *RD = RecordTy->getDecl();
1454  const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD);
1455  if ((CXXRD && !CXXRD->hasTrivialDestructor()) ||
1456  RD->isNonTrivialToPrimitiveDestroy()) {
1457  // Create a flag that is used to indicate when the NRVO was applied
1458  // to this variable. Set it to zero to indicate that NRVO was not
1459  // applied.
1460  llvm::Value *Zero = Builder.getFalse();
1461  Address NRVOFlag =
1462  CreateTempAlloca(Zero->getType(), CharUnits::One(), "nrvo");
1464  Builder.CreateStore(Zero, NRVOFlag);
1465 
1466  // Record the NRVO flag for this variable.
1467  NRVOFlags[&D] = NRVOFlag.getPointer();
1468  emission.NRVOFlag = NRVOFlag.getPointer();
1469  }
1470  }
1471  } else {
1472  CharUnits allocaAlignment;
1473  llvm::Type *allocaTy;
1474  if (isEscapingByRef) {
1475  auto &byrefInfo = getBlockByrefInfo(&D);
1476  allocaTy = byrefInfo.Type;
1477  allocaAlignment = byrefInfo.ByrefAlignment;
1478  } else {
1479  allocaTy = ConvertTypeForMem(Ty);
1480  allocaAlignment = alignment;
1481  }
1482 
1483  // Create the alloca. Note that we set the name separately from
1484  // building the instruction so that it's there even in no-asserts
1485  // builds.
1486  address = CreateTempAlloca(allocaTy, allocaAlignment, D.getName(),
1487  /*ArraySize=*/nullptr, &AllocaAddr);
1488 
1489  // Don't emit lifetime markers for MSVC catch parameters. The lifetime of
1490  // the catch parameter starts in the catchpad instruction, and we can't
1491  // insert code in those basic blocks.
1492  bool IsMSCatchParam =
1494 
1495  // Emit a lifetime intrinsic if meaningful. There's no point in doing this
1496  // if we don't have a valid insertion point (?).
1497  if (HaveInsertPoint() && !IsMSCatchParam) {
1498  // If there's a jump into the lifetime of this variable, its lifetime
1499  // gets broken up into several regions in IR, which requires more work
1500  // to handle correctly. For now, just omit the intrinsics; this is a
1501  // rare case, and it's better to just be conservatively correct.
1502  // PR28267.
1503  //
1504  // We have to do this in all language modes if there's a jump past the
1505  // declaration. We also have to do it in C if there's a jump to an
1506  // earlier point in the current block because non-VLA lifetimes begin as
1507  // soon as the containing block is entered, not when its variables
1508  // actually come into scope; suppressing the lifetime annotations
1509  // completely in this case is unnecessarily pessimistic, but again, this
1510  // is rare.
1511  if (!Bypasses.IsBypassed(&D) &&
1512  !(!getLangOpts().CPlusPlus && hasLabelBeenSeenInCurrentScope())) {
1513  uint64_t size = CGM.getDataLayout().getTypeAllocSize(allocaTy);
1514  emission.SizeForLifetimeMarkers =
1515  EmitLifetimeStart(size, AllocaAddr.getPointer());
1516  }
1517  } else {
1518  assert(!emission.useLifetimeMarkers());
1519  }
1520  }
1521  } else {
1523 
1524  if (!DidCallStackSave) {
1525  // Save the stack.
1526  Address Stack =
1527  CreateTempAlloca(Int8PtrTy, getPointerAlign(), "saved_stack");
1528 
1529  llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave);
1530  llvm::Value *V = Builder.CreateCall(F);
1531  Builder.CreateStore(V, Stack);
1532 
1533  DidCallStackSave = true;
1534 
1535  // Push a cleanup block and restore the stack there.
1536  // FIXME: in general circumstances, this should be an EH cleanup.
1538  }
1539 
1540  auto VlaSize = getVLASize(Ty);
1541  llvm::Type *llvmTy = ConvertTypeForMem(VlaSize.Type);
1542 
1543  // Allocate memory for the array.
1544  address = CreateTempAlloca(llvmTy, alignment, "vla", VlaSize.NumElts,
1545  &AllocaAddr);
1546 
1547  // If we have debug info enabled, properly describe the VLA dimensions for
1548  // this type by registering the vla size expression for each of the
1549  // dimensions.
1550  EmitAndRegisterVariableArrayDimensions(DI, D, EmitDebugInfo);
1551  }
1552 
1553  setAddrOfLocalVar(&D, address);
1554  emission.Addr = address;
1555  emission.AllocaAddr = AllocaAddr;
1556 
1557  // Emit debug info for local var declaration.
1558  if (EmitDebugInfo && HaveInsertPoint()) {
1559  Address DebugAddr = address;
1560  bool UsePointerValue = NRVO && ReturnValuePointer.isValid();
1561  DI->setLocation(D.getLocation());
1562 
1563  // If NRVO, use a pointer to the return address.
1564  if (UsePointerValue)
1565  DebugAddr = ReturnValuePointer;
1566 
1567  (void)DI->EmitDeclareOfAutoVariable(&D, DebugAddr.getPointer(), Builder,
1568  UsePointerValue);
1569  }
1570 
1571  if (D.hasAttr<AnnotateAttr>() && HaveInsertPoint())
1572  EmitVarAnnotations(&D, address.getPointer());
1573 
1574  // Make sure we call @llvm.lifetime.end.
1575  if (emission.useLifetimeMarkers())
1577  emission.getOriginalAllocatedAddress(),
1578  emission.getSizeForLifetimeMarkers());
1579 
1580  return emission;
1581 }
1582 
1583 static bool isCapturedBy(const VarDecl &, const Expr *);
1584 
1585 /// Determines whether the given __block variable is potentially
1586 /// captured by the given statement.
1587 static bool isCapturedBy(const VarDecl &Var, const Stmt *S) {
1588  if (const Expr *E = dyn_cast<Expr>(S))
1589  return isCapturedBy(Var, E);
1590  for (const Stmt *SubStmt : S->children())
1591  if (isCapturedBy(Var, SubStmt))
1592  return true;
1593  return false;
1594 }
1595 
1596 /// Determines whether the given __block variable is potentially
1597 /// captured by the given expression.
1598 static bool isCapturedBy(const VarDecl &Var, const Expr *E) {
1599  // Skip the most common kinds of expressions that make
1600  // hierarchy-walking expensive.
1601  E = E->IgnoreParenCasts();
1602 
1603  if (const BlockExpr *BE = dyn_cast<BlockExpr>(E)) {
1604  const BlockDecl *Block = BE->getBlockDecl();
1605  for (const auto &I : Block->captures()) {
1606  if (I.getVariable() == &Var)
1607  return true;
1608  }
1609 
1610  // No need to walk into the subexpressions.
1611  return false;
1612  }
1613 
1614  if (const StmtExpr *SE = dyn_cast<StmtExpr>(E)) {
1615  const CompoundStmt *CS = SE->getSubStmt();
1616  for (const auto *BI : CS->body())
1617  if (const auto *BIE = dyn_cast<Expr>(BI)) {
1618  if (isCapturedBy(Var, BIE))
1619  return true;
1620  }
1621  else if (const auto *DS = dyn_cast<DeclStmt>(BI)) {
1622  // special case declarations
1623  for (const auto *I : DS->decls()) {
1624  if (const auto *VD = dyn_cast<VarDecl>((I))) {
1625  const Expr *Init = VD->getInit();
1626  if (Init && isCapturedBy(Var, Init))
1627  return true;
1628  }
1629  }
1630  }
1631  else
1632  // FIXME. Make safe assumption assuming arbitrary statements cause capturing.
1633  // Later, provide code to poke into statements for capture analysis.
1634  return true;
1635  return false;
1636  }
1637 
1638  for (const Stmt *SubStmt : E->children())
1639  if (isCapturedBy(Var, SubStmt))
1640  return true;
1641 
1642  return false;
1643 }
1644 
1645 /// Determine whether the given initializer is trivial in the sense
1646 /// that it requires no code to be generated.
1648  if (!Init)
1649  return true;
1650 
1651  if (const CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init))
1652  if (CXXConstructorDecl *Constructor = Construct->getConstructor())
1653  if (Constructor->isTrivial() &&
1654  Constructor->isDefaultConstructor() &&
1655  !Construct->requiresZeroInitialization())
1656  return true;
1657 
1658  return false;
1659 }
1660 
1661 void CodeGenFunction::emitZeroOrPatternForAutoVarInit(QualType type,
1662  const VarDecl &D,
1663  Address Loc) {
1664  auto trivialAutoVarInit = getContext().getLangOpts().getTrivialAutoVarInit();
1665  CharUnits Size = getContext().getTypeSizeInChars(type);
1666  bool isVolatile = type.isVolatileQualified();
1667  if (!Size.isZero()) {
1668  switch (trivialAutoVarInit) {
1670  llvm_unreachable("Uninitialized handled by caller");
1672  emitStoresForZeroInit(CGM, D, Loc, isVolatile, Builder);
1673  break;
1675  emitStoresForPatternInit(CGM, D, Loc, isVolatile, Builder);
1676  break;
1677  }
1678  return;
1679  }
1680 
1681  // VLAs look zero-sized to getTypeInfo. We can't emit constant stores to
1682  // them, so emit a memcpy with the VLA size to initialize each element.
1683  // Technically zero-sized or negative-sized VLAs are undefined, and UBSan
1684  // will catch that code, but there exists code which generates zero-sized
1685  // VLAs. Be nice and initialize whatever they requested.
1686  const auto *VlaType = getContext().getAsVariableArrayType(type);
1687  if (!VlaType)
1688  return;
1689  auto VlaSize = getVLASize(VlaType);
1690  auto SizeVal = VlaSize.NumElts;
1691  CharUnits EltSize = getContext().getTypeSizeInChars(VlaSize.Type);
1692  switch (trivialAutoVarInit) {
1694  llvm_unreachable("Uninitialized handled by caller");
1695 
1697  if (!EltSize.isOne())
1698  SizeVal = Builder.CreateNUWMul(SizeVal, CGM.getSize(EltSize));
1699  Builder.CreateMemSet(Loc, llvm::ConstantInt::get(Int8Ty, 0), SizeVal,
1700  isVolatile);
1701  break;
1702 
1704  llvm::Type *ElTy = Loc.getElementType();
1705  llvm::Constant *Constant = constWithPadding(
1706  CGM, IsPattern::Yes, initializationPatternFor(CGM, ElTy));
1707  CharUnits ConstantAlign = getContext().getTypeAlignInChars(VlaSize.Type);
1708  llvm::BasicBlock *SetupBB = createBasicBlock("vla-setup.loop");
1709  llvm::BasicBlock *LoopBB = createBasicBlock("vla-init.loop");
1710  llvm::BasicBlock *ContBB = createBasicBlock("vla-init.cont");
1711  llvm::Value *IsZeroSizedVLA = Builder.CreateICmpEQ(
1712  SizeVal, llvm::ConstantInt::get(SizeVal->getType(), 0),
1713  "vla.iszerosized");
1714  Builder.CreateCondBr(IsZeroSizedVLA, ContBB, SetupBB);
1715  EmitBlock(SetupBB);
1716  if (!EltSize.isOne())
1717  SizeVal = Builder.CreateNUWMul(SizeVal, CGM.getSize(EltSize));
1718  llvm::Value *BaseSizeInChars =
1719  llvm::ConstantInt::get(IntPtrTy, EltSize.getQuantity());
1720  Address Begin = Builder.CreateElementBitCast(Loc, Int8Ty, "vla.begin");
1721  llvm::Value *End =
1722  Builder.CreateInBoundsGEP(Begin.getPointer(), SizeVal, "vla.end");
1723  llvm::BasicBlock *OriginBB = Builder.GetInsertBlock();
1724  EmitBlock(LoopBB);
1725  llvm::PHINode *Cur = Builder.CreatePHI(Begin.getType(), 2, "vla.cur");
1726  Cur->addIncoming(Begin.getPointer(), OriginBB);
1727  CharUnits CurAlign = Loc.getAlignment().alignmentOfArrayElement(EltSize);
1728  Builder.CreateMemCpy(Address(Cur, CurAlign),
1730  CGM, D, Builder, Constant, ConstantAlign),
1731  BaseSizeInChars, isVolatile);
1732  llvm::Value *Next =
1733  Builder.CreateInBoundsGEP(Int8Ty, Cur, BaseSizeInChars, "vla.next");
1734  llvm::Value *Done = Builder.CreateICmpEQ(Next, End, "vla-init.isdone");
1735  Builder.CreateCondBr(Done, ContBB, LoopBB);
1736  Cur->addIncoming(Next, LoopBB);
1737  EmitBlock(ContBB);
1738  } break;
1739  }
1740 }
1741 
1743  assert(emission.Variable && "emission was not valid!");
1744 
1745  // If this was emitted as a global constant, we're done.
1746  if (emission.wasEmittedAsGlobal()) return;
1747 
1748  const VarDecl &D = *emission.Variable;
1750  QualType type = D.getType();
1751 
1752  // If this local has an initializer, emit it now.
1753  const Expr *Init = D.getInit();
1754 
1755  // If we are at an unreachable point, we don't need to emit the initializer
1756  // unless it contains a label.
1757  if (!HaveInsertPoint()) {
1758  if (!Init || !ContainsLabel(Init)) return;
1760  }
1761 
1762  // Initialize the structure of a __block variable.
1763  if (emission.IsEscapingByRef)
1764  emitByrefStructureInit(emission);
1765 
1766  // Initialize the variable here if it doesn't have a initializer and it is a
1767  // C struct that is non-trivial to initialize or an array containing such a
1768  // struct.
1769  if (!Init &&
1770  type.isNonTrivialToPrimitiveDefaultInitialize() ==
1772  LValue Dst = MakeAddrLValue(emission.getAllocatedAddress(), type);
1773  if (emission.IsEscapingByRef)
1774  drillIntoBlockVariable(*this, Dst, &D);
1776  return;
1777  }
1778 
1779  // Check whether this is a byref variable that's potentially
1780  // captured and moved by its own initializer. If so, we'll need to
1781  // emit the initializer first, then copy into the variable.
1782  bool capturedByInit =
1783  Init && emission.IsEscapingByRef && isCapturedBy(D, Init);
1784 
1785  bool locIsByrefHeader = !capturedByInit;
1786  const Address Loc =
1787  locIsByrefHeader ? emission.getObjectAddress(*this) : emission.Addr;
1788 
1789  // Note: constexpr already initializes everything correctly.
1790  LangOptions::TrivialAutoVarInitKind trivialAutoVarInit =
1791  (D.isConstexpr()
1793  : (D.getAttr<UninitializedAttr>()
1795  : getContext().getLangOpts().getTrivialAutoVarInit()));
1796 
1797  auto initializeWhatIsTechnicallyUninitialized = [&](Address Loc) {
1798  if (trivialAutoVarInit ==
1800  return;
1801 
1802  // Only initialize a __block's storage: we always initialize the header.
1803  if (emission.IsEscapingByRef && !locIsByrefHeader)
1804  Loc = emitBlockByrefAddress(Loc, &D, /*follow=*/false);
1805 
1806  return emitZeroOrPatternForAutoVarInit(type, D, Loc);
1807  };
1808 
1809  if (isTrivialInitializer(Init))
1810  return initializeWhatIsTechnicallyUninitialized(Loc);
1811 
1812  llvm::Constant *constant = nullptr;
1813  if (emission.IsConstantAggregate ||
1815  assert(!capturedByInit && "constant init contains a capturing block?");
1816  constant = ConstantEmitter(*this).tryEmitAbstractForInitializer(D);
1817  if (constant && !constant->isZeroValue() &&
1818  (trivialAutoVarInit !=
1820  IsPattern isPattern =
1821  (trivialAutoVarInit == LangOptions::TrivialAutoVarInitKind::Pattern)
1822  ? IsPattern::Yes
1823  : IsPattern::No;
1824  // C guarantees that brace-init with fewer initializers than members in
1825  // the aggregate will initialize the rest of the aggregate as-if it were
1826  // static initialization. In turn static initialization guarantees that
1827  // padding is initialized to zero bits. We could instead pattern-init if D
1828  // has any ImplicitValueInitExpr, but that seems to be unintuitive
1829  // behavior.
1830  constant = constWithPadding(CGM, IsPattern::No,
1831  replaceUndef(CGM, isPattern, constant));
1832  }
1833  }
1834 
1835  if (!constant) {
1836  initializeWhatIsTechnicallyUninitialized(Loc);
1837  LValue lv = MakeAddrLValue(Loc, type);
1838  lv.setNonGC(true);
1839  return EmitExprAsInit(Init, &D, lv, capturedByInit);
1840  }
1841 
1842  if (!emission.IsConstantAggregate) {
1843  // For simple scalar/complex initialization, store the value directly.
1844  LValue lv = MakeAddrLValue(Loc, type);
1845  lv.setNonGC(true);
1846  return EmitStoreThroughLValue(RValue::get(constant), lv, true);
1847  }
1848 
1849  llvm::Type *BP = CGM.Int8Ty->getPointerTo(Loc.getAddressSpace());
1851  CGM, D, (Loc.getType() == BP) ? Loc : Builder.CreateBitCast(Loc, BP),
1852  type.isVolatileQualified(), Builder, constant);
1853 }
1854 
1855 /// Emit an expression as an initializer for an object (variable, field, etc.)
1856 /// at the given location. The expression is not necessarily the normal
1857 /// initializer for the object, and the address is not necessarily
1858 /// its normal location.
1859 ///
1860 /// \param init the initializing expression
1861 /// \param D the object to act as if we're initializing
1862 /// \param loc the address to initialize; its type is a pointer
1863 /// to the LLVM mapping of the object's type
1864 /// \param alignment the alignment of the address
1865 /// \param capturedByInit true if \p D is a __block variable
1866 /// whose address is potentially changed by the initializer
1868  LValue lvalue, bool capturedByInit) {
1869  QualType type = D->getType();
1870 
1871  if (type->isReferenceType()) {
1872  RValue rvalue = EmitReferenceBindingToExpr(init);
1873  if (capturedByInit)
1874  drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
1875  EmitStoreThroughLValue(rvalue, lvalue, true);
1876  return;
1877  }
1878  switch (getEvaluationKind(type)) {
1879  case TEK_Scalar:
1880  EmitScalarInit(init, D, lvalue, capturedByInit);
1881  return;
1882  case TEK_Complex: {
1883  ComplexPairTy complex = EmitComplexExpr(init);
1884  if (capturedByInit)
1885  drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
1886  EmitStoreOfComplex(complex, lvalue, /*init*/ true);
1887  return;
1888  }
1889  case TEK_Aggregate:
1890  if (type->isAtomicType()) {
1891  EmitAtomicInit(const_cast<Expr*>(init), lvalue);
1892  } else {
1894  if (isa<VarDecl>(D))
1895  Overlap = AggValueSlot::DoesNotOverlap;
1896  else if (auto *FD = dyn_cast<FieldDecl>(D))
1897  Overlap = getOverlapForFieldInit(FD);
1898  // TODO: how can we delay here if D is captured by its initializer?
1899  EmitAggExpr(init, AggValueSlot::forLValue(lvalue,
1903  Overlap));
1904  }
1905  return;
1906  }
1907  llvm_unreachable("bad evaluation kind");
1908 }
1909 
1910 /// Enter a destroy cleanup for the given local variable.
1912  const CodeGenFunction::AutoVarEmission &emission,
1913  QualType::DestructionKind dtorKind) {
1914  assert(dtorKind != QualType::DK_none);
1915 
1916  // Note that for __block variables, we want to destroy the
1917  // original stack object, not the possibly forwarded object.
1918  Address addr = emission.getObjectAddress(*this);
1919 
1920  const VarDecl *var = emission.Variable;
1921  QualType type = var->getType();
1922 
1923  CleanupKind cleanupKind = NormalAndEHCleanup;
1924  CodeGenFunction::Destroyer *destroyer = nullptr;
1925 
1926  switch (dtorKind) {
1927  case QualType::DK_none:
1928  llvm_unreachable("no cleanup for trivially-destructible variable");
1929 
1931  // If there's an NRVO flag on the emission, we need a different
1932  // cleanup.
1933  if (emission.NRVOFlag) {
1934  assert(!type->isArrayType());
1936  EHStack.pushCleanup<DestroyNRVOVariableCXX>(cleanupKind, addr, type, dtor,
1937  emission.NRVOFlag);
1938  return;
1939  }
1940  break;
1941 
1943  // Suppress cleanups for pseudo-strong variables.
1944  if (var->isARCPseudoStrong()) return;
1945 
1946  // Otherwise, consider whether to use an EH cleanup or not.
1947  cleanupKind = getARCCleanupKind();
1948 
1949  // Use the imprecise destroyer by default.
1950  if (!var->hasAttr<ObjCPreciseLifetimeAttr>())
1952  break;
1953 
1955  break;
1956 
1959  if (emission.NRVOFlag) {
1960  assert(!type->isArrayType());
1961  EHStack.pushCleanup<DestroyNRVOVariableC>(cleanupKind, addr,
1962  emission.NRVOFlag, type);
1963  return;
1964  }
1965  break;
1966  }
1967 
1968  // If we haven't chosen a more specific destroyer, use the default.
1969  if (!destroyer) destroyer = getDestroyer(dtorKind);
1970 
1971  // Use an EH cleanup in array destructors iff the destructor itself
1972  // is being pushed as an EH cleanup.
1973  bool useEHCleanup = (cleanupKind & EHCleanup);
1974  EHStack.pushCleanup<DestroyObject>(cleanupKind, addr, type, destroyer,
1975  useEHCleanup);
1976 }
1977 
1979  assert(emission.Variable && "emission was not valid!");
1980 
1981  // If this was emitted as a global constant, we're done.
1982  if (emission.wasEmittedAsGlobal()) return;
1983 
1984  // If we don't have an insertion point, we're done. Sema prevents
1985  // us from jumping into any of these scopes anyway.
1986  if (!HaveInsertPoint()) return;
1987 
1988  const VarDecl &D = *emission.Variable;
1989 
1990  // Check the type for a cleanup.
1992  emitAutoVarTypeCleanup(emission, dtorKind);
1993 
1994  // In GC mode, honor objc_precise_lifetime.
1995  if (getLangOpts().getGC() != LangOptions::NonGC &&
1996  D.hasAttr<ObjCPreciseLifetimeAttr>()) {
1997  EHStack.pushCleanup<ExtendGCLifetime>(NormalCleanup, &D);
1998  }
1999 
2000  // Handle the cleanup attribute.
2001  if (const CleanupAttr *CA = D.getAttr<CleanupAttr>()) {
2002  const FunctionDecl *FD = CA->getFunctionDecl();
2003 
2004  llvm::Constant *F = CGM.GetAddrOfFunction(FD);
2005  assert(F && "Could not find function!");
2006 
2007  const CGFunctionInfo &Info = CGM.getTypes().arrangeFunctionDeclaration(FD);
2008  EHStack.pushCleanup<CallCleanupFunction>(NormalAndEHCleanup, F, &Info, &D);
2009  }
2010 
2011  // If this is a block variable, call _Block_object_destroy
2012  // (on the unforwarded address). Don't enter this cleanup if we're in pure-GC
2013  // mode.
2014  if (emission.IsEscapingByRef &&
2015  CGM.getLangOpts().getGC() != LangOptions::GCOnly) {
2017  if (emission.Variable->getType().isObjCGCWeak())
2018  Flags |= BLOCK_FIELD_IS_WEAK;
2019  enterByrefCleanup(NormalAndEHCleanup, emission.Addr, Flags,
2020  /*LoadBlockVarAddr*/ false,
2021  cxxDestructorCanThrow(emission.Variable->getType()));
2022  }
2023 }
2024 
2027  switch (kind) {
2028  case QualType::DK_none: llvm_unreachable("no destroyer for trivial dtor");
2030  return destroyCXXObject;
2032  return destroyARCStrongPrecise;
2034  return destroyARCWeak;
2036  return destroyNonTrivialCStruct;
2037  }
2038  llvm_unreachable("Unknown DestructionKind");
2039 }
2040 
2041 /// pushEHDestroy - Push the standard destructor for the given type as
2042 /// an EH-only cleanup.
2044  Address addr, QualType type) {
2045  assert(dtorKind && "cannot push destructor for trivial type");
2046  assert(needsEHCleanup(dtorKind));
2047 
2048  pushDestroy(EHCleanup, addr, type, getDestroyer(dtorKind), true);
2049 }
2050 
2051 /// pushDestroy - Push the standard destructor for the given type as
2052 /// at least a normal cleanup.
2054  Address addr, QualType type) {
2055  assert(dtorKind && "cannot push destructor for trivial type");
2056 
2057  CleanupKind cleanupKind = getCleanupKind(dtorKind);
2058  pushDestroy(cleanupKind, addr, type, getDestroyer(dtorKind),
2059  cleanupKind & EHCleanup);
2060 }
2061 
2063  QualType type, Destroyer *destroyer,
2064  bool useEHCleanupForArray) {
2065  pushFullExprCleanup<DestroyObject>(cleanupKind, addr, type,
2066  destroyer, useEHCleanupForArray);
2067 }
2068 
2070  EHStack.pushCleanup<CallStackRestore>(Kind, SPMem);
2071 }
2072 
2074  CleanupKind cleanupKind, Address addr, QualType type,
2075  Destroyer *destroyer, bool useEHCleanupForArray) {
2076  // Push an EH-only cleanup for the object now.
2077  // FIXME: When popping normal cleanups, we need to keep this EH cleanup
2078  // around in case a temporary's destructor throws an exception.
2079  if (cleanupKind & EHCleanup)
2080  EHStack.pushCleanup<DestroyObject>(
2081  static_cast<CleanupKind>(cleanupKind & ~NormalCleanup), addr, type,
2082  destroyer, useEHCleanupForArray);
2083 
2084  // Remember that we need to push a full cleanup for the object at the
2085  // end of the full-expression.
2086  pushCleanupAfterFullExpr<DestroyObject>(
2087  cleanupKind, addr, type, destroyer, useEHCleanupForArray);
2088 }
2089 
2090 /// emitDestroy - Immediately perform the destruction of the given
2091 /// object.
2092 ///
2093 /// \param addr - the address of the object; a type*
2094 /// \param type - the type of the object; if an array type, all
2095 /// objects are destroyed in reverse order
2096 /// \param destroyer - the function to call to destroy individual
2097 /// elements
2098 /// \param useEHCleanupForArray - whether an EH cleanup should be
2099 /// used when destroying array elements, in case one of the
2100 /// destructions throws an exception
2102  Destroyer *destroyer,
2103  bool useEHCleanupForArray) {
2104  const ArrayType *arrayType = getContext().getAsArrayType(type);
2105  if (!arrayType)
2106  return destroyer(*this, addr, type);
2107 
2108  llvm::Value *length = emitArrayLength(arrayType, type, addr);
2109 
2110  CharUnits elementAlign =
2111  addr.getAlignment()
2112  .alignmentOfArrayElement(getContext().getTypeSizeInChars(type));
2113 
2114  // Normally we have to check whether the array is zero-length.
2115  bool checkZeroLength = true;
2116 
2117  // But if the array length is constant, we can suppress that.
2118  if (llvm::ConstantInt *constLength = dyn_cast<llvm::ConstantInt>(length)) {
2119  // ...and if it's constant zero, we can just skip the entire thing.
2120  if (constLength->isZero()) return;
2121  checkZeroLength = false;
2122  }
2123 
2124  llvm::Value *begin = addr.getPointer();
2125  llvm::Value *end = Builder.CreateInBoundsGEP(begin, length);
2126  emitArrayDestroy(begin, end, type, elementAlign, destroyer,
2127  checkZeroLength, useEHCleanupForArray);
2128 }
2129 
2130 /// emitArrayDestroy - Destroys all the elements of the given array,
2131 /// beginning from last to first. The array cannot be zero-length.
2132 ///
2133 /// \param begin - a type* denoting the first element of the array
2134 /// \param end - a type* denoting one past the end of the array
2135 /// \param elementType - the element type of the array
2136 /// \param destroyer - the function to call to destroy elements
2137 /// \param useEHCleanup - whether to push an EH cleanup to destroy
2138 /// the remaining elements in case the destruction of a single
2139 /// element throws
2141  llvm::Value *end,
2142  QualType elementType,
2143  CharUnits elementAlign,
2144  Destroyer *destroyer,
2145  bool checkZeroLength,
2146  bool useEHCleanup) {
2147  assert(!elementType->isArrayType());
2148 
2149  // The basic structure here is a do-while loop, because we don't
2150  // need to check for the zero-element case.
2151  llvm::BasicBlock *bodyBB = createBasicBlock("arraydestroy.body");
2152  llvm::BasicBlock *doneBB = createBasicBlock("arraydestroy.done");
2153 
2154  if (checkZeroLength) {
2155  llvm::Value *isEmpty = Builder.CreateICmpEQ(begin, end,
2156  "arraydestroy.isempty");
2157  Builder.CreateCondBr(isEmpty, doneBB, bodyBB);
2158  }
2159 
2160  // Enter the loop body, making that address the current address.
2161  llvm::BasicBlock *entryBB = Builder.GetInsertBlock();
2162  EmitBlock(bodyBB);
2163  llvm::PHINode *elementPast =
2164  Builder.CreatePHI(begin->getType(), 2, "arraydestroy.elementPast");
2165  elementPast->addIncoming(end, entryBB);
2166 
2167  // Shift the address back by one element.
2168  llvm::Value *negativeOne = llvm::ConstantInt::get(SizeTy, -1, true);
2169  llvm::Value *element = Builder.CreateInBoundsGEP(elementPast, negativeOne,
2170  "arraydestroy.element");
2171 
2172  if (useEHCleanup)
2173  pushRegularPartialArrayCleanup(begin, element, elementType, elementAlign,
2174  destroyer);
2175 
2176  // Perform the actual destruction there.
2177  destroyer(*this, Address(element, elementAlign), elementType);
2178 
2179  if (useEHCleanup)
2180  PopCleanupBlock();
2181 
2182  // Check whether we've reached the end.
2183  llvm::Value *done = Builder.CreateICmpEQ(element, begin, "arraydestroy.done");
2184  Builder.CreateCondBr(done, doneBB, bodyBB);
2185  elementPast->addIncoming(element, Builder.GetInsertBlock());
2186 
2187  // Done.
2188  EmitBlock(doneBB);
2189 }
2190 
2191 /// Perform partial array destruction as if in an EH cleanup. Unlike
2192 /// emitArrayDestroy, the element type here may still be an array type.
2194  llvm::Value *begin, llvm::Value *end,
2195  QualType type, CharUnits elementAlign,
2196  CodeGenFunction::Destroyer *destroyer) {
2197  // If the element type is itself an array, drill down.
2198  unsigned arrayDepth = 0;
2199  while (const ArrayType *arrayType = CGF.getContext().getAsArrayType(type)) {
2200  // VLAs don't require a GEP index to walk into.
2201  if (!isa<VariableArrayType>(arrayType))
2202  arrayDepth++;
2203  type = arrayType->getElementType();
2204  }
2205 
2206  if (arrayDepth) {
2207  llvm::Value *zero = llvm::ConstantInt::get(CGF.SizeTy, 0);
2208 
2209  SmallVector<llvm::Value*,4> gepIndices(arrayDepth+1, zero);
2210  begin = CGF.Builder.CreateInBoundsGEP(begin, gepIndices, "pad.arraybegin");
2211  end = CGF.Builder.CreateInBoundsGEP(end, gepIndices, "pad.arrayend");
2212  }
2213 
2214  // Destroy the array. We don't ever need an EH cleanup because we
2215  // assume that we're in an EH cleanup ourselves, so a throwing
2216  // destructor causes an immediate terminate.
2217  CGF.emitArrayDestroy(begin, end, type, elementAlign, destroyer,
2218  /*checkZeroLength*/ true, /*useEHCleanup*/ false);
2219 }
2220 
2221 namespace {
2222  /// RegularPartialArrayDestroy - a cleanup which performs a partial
2223  /// array destroy where the end pointer is regularly determined and
2224  /// does not need to be loaded from a local.
2225  class RegularPartialArrayDestroy final : public EHScopeStack::Cleanup {
2226  llvm::Value *ArrayBegin;
2227  llvm::Value *ArrayEnd;
2228  QualType ElementType;
2230  CharUnits ElementAlign;
2231  public:
2232  RegularPartialArrayDestroy(llvm::Value *arrayBegin, llvm::Value *arrayEnd,
2233  QualType elementType, CharUnits elementAlign,
2234  CodeGenFunction::Destroyer *destroyer)
2235  : ArrayBegin(arrayBegin), ArrayEnd(arrayEnd),
2236  ElementType(elementType), Destroyer(destroyer),
2237  ElementAlign(elementAlign) {}
2238 
2239  void Emit(CodeGenFunction &CGF, Flags flags) override {
2240  emitPartialArrayDestroy(CGF, ArrayBegin, ArrayEnd,
2241  ElementType, ElementAlign, Destroyer);
2242  }
2243  };
2244 
2245  /// IrregularPartialArrayDestroy - a cleanup which performs a
2246  /// partial array destroy where the end pointer is irregularly
2247  /// determined and must be loaded from a local.
2248  class IrregularPartialArrayDestroy final : public EHScopeStack::Cleanup {
2249  llvm::Value *ArrayBegin;
2250  Address ArrayEndPointer;
2251  QualType ElementType;
2253  CharUnits ElementAlign;
2254  public:
2255  IrregularPartialArrayDestroy(llvm::Value *arrayBegin,
2256  Address arrayEndPointer,
2257  QualType elementType,
2258  CharUnits elementAlign,
2259  CodeGenFunction::Destroyer *destroyer)
2260  : ArrayBegin(arrayBegin), ArrayEndPointer(arrayEndPointer),
2261  ElementType(elementType), Destroyer(destroyer),
2262  ElementAlign(elementAlign) {}
2263 
2264  void Emit(CodeGenFunction &CGF, Flags flags) override {
2265  llvm::Value *arrayEnd = CGF.Builder.CreateLoad(ArrayEndPointer);
2266  emitPartialArrayDestroy(CGF, ArrayBegin, arrayEnd,
2267  ElementType, ElementAlign, Destroyer);
2268  }
2269  };
2270 } // end anonymous namespace
2271 
2272 /// pushIrregularPartialArrayCleanup - Push an EH cleanup to destroy
2273 /// already-constructed elements of the given array. The cleanup
2274 /// may be popped with DeactivateCleanupBlock or PopCleanupBlock.
2275 ///
2276 /// \param elementType - the immediate element type of the array;
2277 /// possibly still an array type
2279  Address arrayEndPointer,
2280  QualType elementType,
2281  CharUnits elementAlign,
2282  Destroyer *destroyer) {
2283  pushFullExprCleanup<IrregularPartialArrayDestroy>(EHCleanup,
2284  arrayBegin, arrayEndPointer,
2285  elementType, elementAlign,
2286  destroyer);
2287 }
2288 
2289 /// pushRegularPartialArrayCleanup - Push an EH cleanup to destroy
2290 /// already-constructed elements of the given array. The cleanup
2291 /// may be popped with DeactivateCleanupBlock or PopCleanupBlock.
2292 ///
2293 /// \param elementType - the immediate element type of the array;
2294 /// possibly still an array type
2296  llvm::Value *arrayEnd,
2297  QualType elementType,
2298  CharUnits elementAlign,
2299  Destroyer *destroyer) {
2300  pushFullExprCleanup<RegularPartialArrayDestroy>(EHCleanup,
2301  arrayBegin, arrayEnd,
2302  elementType, elementAlign,
2303  destroyer);
2304 }
2305 
2306 /// Lazily declare the @llvm.lifetime.start intrinsic.
2308  if (LifetimeStartFn)
2309  return LifetimeStartFn;
2310  LifetimeStartFn = llvm::Intrinsic::getDeclaration(&getModule(),
2311  llvm::Intrinsic::lifetime_start, AllocaInt8PtrTy);
2312  return LifetimeStartFn;
2313 }
2314 
2315 /// Lazily declare the @llvm.lifetime.end intrinsic.
2317  if (LifetimeEndFn)
2318  return LifetimeEndFn;
2319  LifetimeEndFn = llvm::Intrinsic::getDeclaration(&getModule(),
2320  llvm::Intrinsic::lifetime_end, AllocaInt8PtrTy);
2321  return LifetimeEndFn;
2322 }
2323 
2324 namespace {
2325  /// A cleanup to perform a release of an object at the end of a
2326  /// function. This is used to balance out the incoming +1 of a
2327  /// ns_consumed argument when we can't reasonably do that just by
2328  /// not doing the initial retain for a __block argument.
2329  struct ConsumeARCParameter final : EHScopeStack::Cleanup {
2330  ConsumeARCParameter(llvm::Value *param,
2331  ARCPreciseLifetime_t precise)
2332  : Param(param), Precise(precise) {}
2333 
2334  llvm::Value *Param;
2335  ARCPreciseLifetime_t Precise;
2336 
2337  void Emit(CodeGenFunction &CGF, Flags flags) override {
2338  CGF.EmitARCRelease(Param, Precise);
2339  }
2340  };
2341 } // end anonymous namespace
2342 
2343 /// Emit an alloca (or GlobalValue depending on target)
2344 /// for the specified parameter and set up LocalDeclMap.
2346  unsigned ArgNo) {
2347  // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl?
2348  assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) &&
2349  "Invalid argument to EmitParmDecl");
2350 
2351  Arg.getAnyValue()->setName(D.getName());
2352 
2353  QualType Ty = D.getType();
2354 
2355  // Use better IR generation for certain implicit parameters.
2356  if (auto IPD = dyn_cast<ImplicitParamDecl>(&D)) {
2357  // The only implicit argument a block has is its literal.
2358  // This may be passed as an inalloca'ed value on Windows x86.
2359  if (BlockInfo) {
2360  llvm::Value *V = Arg.isIndirect()
2362  : Arg.getDirectValue();
2363  setBlockContextParameter(IPD, ArgNo, V);
2364  return;
2365  }
2366  }
2367 
2368  Address DeclPtr = Address::invalid();
2369  bool DoStore = false;
2370  bool IsScalar = hasScalarEvaluationKind(Ty);
2371  // If we already have a pointer to the argument, reuse the input pointer.
2372  if (Arg.isIndirect()) {
2373  DeclPtr = Arg.getIndirectAddress();
2374  // If we have a prettier pointer type at this point, bitcast to that.
2375  unsigned AS = DeclPtr.getType()->getAddressSpace();
2376  llvm::Type *IRTy = ConvertTypeForMem(Ty)->getPointerTo(AS);
2377  if (DeclPtr.getType() != IRTy)
2378  DeclPtr = Builder.CreateBitCast(DeclPtr, IRTy, D.getName());
2379  // Indirect argument is in alloca address space, which may be different
2380  // from the default address space.
2381  auto AllocaAS = CGM.getASTAllocaAddressSpace();
2382  auto *V = DeclPtr.getPointer();
2383  auto SrcLangAS = getLangOpts().OpenCL ? LangAS::opencl_private : AllocaAS;
2384  auto DestLangAS =
2386  if (SrcLangAS != DestLangAS) {
2387  assert(getContext().getTargetAddressSpace(SrcLangAS) ==
2388  CGM.getDataLayout().getAllocaAddrSpace());
2389  auto DestAS = getContext().getTargetAddressSpace(DestLangAS);
2390  auto *T = V->getType()->getPointerElementType()->getPointerTo(DestAS);
2391  DeclPtr = Address(getTargetHooks().performAddrSpaceCast(
2392  *this, V, SrcLangAS, DestLangAS, T, true),
2393  DeclPtr.getAlignment());
2394  }
2395 
2396  // Push a destructor cleanup for this parameter if the ABI requires it.
2397  // Don't push a cleanup in a thunk for a method that will also emit a
2398  // cleanup.
2400  Ty->castAs<RecordType>()->getDecl()->isParamDestroyedInCallee()) {
2401  if (QualType::DestructionKind DtorKind =
2403  assert((DtorKind == QualType::DK_cxx_destructor ||
2404  DtorKind == QualType::DK_nontrivial_c_struct) &&
2405  "unexpected destructor type");
2406  pushDestroy(DtorKind, DeclPtr, Ty);
2407  CalleeDestructedParamCleanups[cast<ParmVarDecl>(&D)] =
2409  }
2410  }
2411  } else {
2412  // Check if the parameter address is controlled by OpenMP runtime.
2413  Address OpenMPLocalAddr =
2414  getLangOpts().OpenMP
2415  ? CGM.getOpenMPRuntime().getAddressOfLocalVariable(*this, &D)
2416  : Address::invalid();
2417  if (getLangOpts().OpenMP && OpenMPLocalAddr.isValid()) {
2418  DeclPtr = OpenMPLocalAddr;
2419  } else {
2420  // Otherwise, create a temporary to hold the value.
2421  DeclPtr = CreateMemTemp(Ty, getContext().getDeclAlign(&D),
2422  D.getName() + ".addr");
2423  }
2424  DoStore = true;
2425  }
2426 
2427  llvm::Value *ArgVal = (DoStore ? Arg.getDirectValue() : nullptr);
2428 
2429  LValue lv = MakeAddrLValue(DeclPtr, Ty);
2430  if (IsScalar) {
2431  Qualifiers qs = Ty.getQualifiers();
2432  if (Qualifiers::ObjCLifetime lt = qs.getObjCLifetime()) {
2433  // We honor __attribute__((ns_consumed)) for types with lifetime.
2434  // For __strong, it's handled by just skipping the initial retain;
2435  // otherwise we have to balance out the initial +1 with an extra
2436  // cleanup to do the release at the end of the function.
2437  bool isConsumed = D.hasAttr<NSConsumedAttr>();
2438 
2439  // If a parameter is pseudo-strong then we can omit the implicit retain.
2440  if (D.isARCPseudoStrong()) {
2441  assert(lt == Qualifiers::OCL_Strong &&
2442  "pseudo-strong variable isn't strong?");
2443  assert(qs.hasConst() && "pseudo-strong variable should be const!");
2445  }
2446 
2447  // Load objects passed indirectly.
2448  if (Arg.isIndirect() && !ArgVal)
2449  ArgVal = Builder.CreateLoad(DeclPtr);
2450 
2451  if (lt == Qualifiers::OCL_Strong) {
2452  if (!isConsumed) {
2453  if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
2454  // use objc_storeStrong(&dest, value) for retaining the
2455  // object. But first, store a null into 'dest' because
2456  // objc_storeStrong attempts to release its old value.
2458  EmitStoreOfScalar(Null, lv, /* isInitialization */ true);
2459  EmitARCStoreStrongCall(lv.getAddress(), ArgVal, true);
2460  DoStore = false;
2461  }
2462  else
2463  // Don't use objc_retainBlock for block pointers, because we
2464  // don't want to Block_copy something just because we got it
2465  // as a parameter.
2466  ArgVal = EmitARCRetainNonBlock(ArgVal);
2467  }
2468  } else {
2469  // Push the cleanup for a consumed parameter.
2470  if (isConsumed) {
2471  ARCPreciseLifetime_t precise = (D.hasAttr<ObjCPreciseLifetimeAttr>()
2473  EHStack.pushCleanup<ConsumeARCParameter>(getARCCleanupKind(), ArgVal,
2474  precise);
2475  }
2476 
2477  if (lt == Qualifiers::OCL_Weak) {
2478  EmitARCInitWeak(DeclPtr, ArgVal);
2479  DoStore = false; // The weak init is a store, no need to do two.
2480  }
2481  }
2482 
2483  // Enter the cleanup scope.
2484  EmitAutoVarWithLifetime(*this, D, DeclPtr, lt);
2485  }
2486  }
2487 
2488  // Store the initial value into the alloca.
2489  if (DoStore)
2490  EmitStoreOfScalar(ArgVal, lv, /* isInitialization */ true);
2491 
2492  setAddrOfLocalVar(&D, DeclPtr);
2493 
2494  // Emit debug info for param declarations in non-thunk functions.
2495  if (CGDebugInfo *DI = getDebugInfo()) {
2496  if (CGM.getCodeGenOpts().getDebugInfo() >=
2498  !CurFuncIsThunk) {
2499  DI->EmitDeclareOfArgVariable(&D, DeclPtr.getPointer(), ArgNo, Builder);
2500  }
2501  }
2502 
2503  if (D.hasAttr<AnnotateAttr>())
2504  EmitVarAnnotations(&D, DeclPtr.getPointer());
2505 
2506  // We can only check return value nullability if all arguments to the
2507  // function satisfy their nullability preconditions. This makes it necessary
2508  // to emit null checks for args in the function body itself.
2509  if (requiresReturnValueNullabilityCheck()) {
2510  auto Nullability = Ty->getNullability(getContext());
2512  SanitizerScope SanScope(this);
2513  RetValNullabilityPrecondition =
2514  Builder.CreateAnd(RetValNullabilityPrecondition,
2515  Builder.CreateIsNotNull(Arg.getAnyValue()));
2516  }
2517  }
2518 }
2519 
2521  CodeGenFunction *CGF) {
2522  if (!LangOpts.OpenMP || (!LangOpts.EmitAllDecls && !D->isUsed()))
2523  return;
2524  getOpenMPRuntime().emitUserDefinedReduction(CGF, D);
2525 }
2526 
2528  CodeGenFunction *CGF) {
2529  if (!LangOpts.OpenMP || LangOpts.OpenMPSimd ||
2530  (!LangOpts.EmitAllDecls && !D->isUsed()))
2531  return;
2532  getOpenMPRuntime().emitUserDefinedMapper(D, CGF);
2533 }
2534 
2536  getOpenMPRuntime().checkArchForUnifiedAddressing(D);
2537 }
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:859
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:2275
void setImplicit(bool I=true)
Definition: DeclBase.h:559
Represents a function declaration or definition.
Definition: Decl.h:1784
void EmitStaticVarDecl(const VarDecl &D, llvm::GlobalValue::LinkageTypes Linkage)
Definition: CGDecl.cpp:377
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:2527
llvm::Constant * initializationPatternFor(CodeGenModule &, llvm::Type *)
Definition: PatternInit.cpp:14
Destroyer * getDestroyer(QualType::DestructionKind destructionKind)
Definition: CGDecl.cpp:2026
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:2188
bool isArrayType() const
Definition: Type.h:6447
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:2789
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:6471
llvm::Function * getLLVMLifetimeEndFn()
Lazily declare the .lifetime.end intrinsic.
Definition: CGDecl.cpp:2316
IsPattern
Definition: CGDecl.cpp:985
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:1911
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:3003
void EmitAutoVarDecl(const VarDecl &D)
EmitAutoVarDecl - Emit an auto variable declaration.
Definition: CGDecl.cpp:1277
static Destroyer destroyARCStrongPrecise
void pushLifetimeExtendedDestroy(CleanupKind kind, Address addr, QualType type, Destroyer *destroyer, bool useEHCleanupForArray)
Definition: CGDecl.cpp:2073
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:1925
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:897
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition: Type.h:2860
Represents a call to a C++ constructor.
Definition: ExprCXX.h:1332
bool isZero() const
isZero - Test whether the quantity equals zero.
Definition: CharUnits.h:116
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:2352
const DeclContext * getParentFunctionOrMethod() const
If this decl is defined inside a function/method/block it returns the corresponding DeclContext...
Definition: DeclBase.cpp:254
The type is a struct containing a field whose type is not PCK_Trivial.
Definition: Type.h:1096
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:941
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:1040
Represents a variable declaration or definition.
Definition: Decl.h:827
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:6858
AggValueSlot::Overlap_t getOverlapForFieldInit(const FieldDecl *FD)
Determine whether a field initialization may overlap some other object.
Definition: CGExprAgg.cpp:1872
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:962
void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type, bool ForVirtualBase, bool Delegating, Address This, QualType ThisTy)
Definition: CGClass.cpp:2411
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
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:1395
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:1060
void emitDestroy(Address addr, QualType type, Destroyer *destroyer, bool useEHCleanupForArray)
emitDestroy - Immediately perform the destruction of the given object.
Definition: CGDecl.cpp:2101
void emitByrefStructureInit(const AutoVarEmission &emission)
Initialize the structural components of a __block variable, i.e.
Definition: CGBlocks.cpp:2818
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
QualType::DestructionKind needsDestruction(const ASTContext &Ctx) const
Do we need to emit an exit-time destructor for this variable, and if so, what kind?
Definition: Decl.cpp:2596
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:1867
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:1000
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:925
static llvm::Constant * patternOrZeroFor(CodeGenModule &CGM, IsPattern isPattern, llvm::Type *Ty)
Generate a constant filled with either a pattern or zeroes.
Definition: CGDecl.cpp:988
bool Zero(InterpState &S, CodePtr OpPC)
Definition: Interp.h:815
Qualifiers::ObjCLifetime getObjCLifetime() const
Definition: CGValue.h:265
bool isReferenceType() const
Definition: Type.h:6403
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:2043
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:1241
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:579
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:2295
static bool hasScalarEvaluationKind(QualType T)
static void drillIntoBlockVariable(CodeGenFunction &CGF, LValue &lvalue, const VarDecl *var)
Definition: CGDecl.cpp:713
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:38
void EmitOMPRequiresDecl(const OMPRequiresDecl *D)
Emit a code for requires directive.
Definition: CGDecl.cpp:2535
llvm::Align getAsAlign() const
getAsAlign - Returns Quantity as a valid llvm::Align, Beware llvm::Align assumes power of two 8-bit b...
Definition: CharUnits.h:183
void EmitOMPDeclareReduction(const OMPDeclareReductionDecl *D, CodeGenFunction *CGF=nullptr)
Emit a code for declare reduction construct.
Definition: CGDecl.cpp:2520
bool isOne() const
isOne - Test whether the quantity equals one.
Definition: CharUnits.h:119
An x-value expression is a reference to an object with independent storage but which can be "moved"...
Definition: Specifiers.h:134
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:223
bool isVolatileQualified() const
Determine whether this type is volatile-qualified.
Definition: Type.h:6218
static void emitStoresForConstant(CodeGenModule &CGM, const VarDecl &D, Address Loc, bool isVolatile, CGBuilderTy &Builder, llvm::Constant *constant)
Definition: CGDecl.cpp:1140
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:1417
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:2971
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:1231
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:2005
const internal::VariadicDynCastAllOfMatcher< Stmt, CastExpr > castExpr
Matches any cast nodes of Clang&#39;s AST.
static VarDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, StorageClass S)
Definition: Decl.cpp:1963
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:1765
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:1598
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:1095
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:58
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:1375
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1692
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:179
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:974
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:283
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:3951
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition: Decl.h:644
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:1742
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:1050
Enters a new scope for capturing cleanups, all of which will be executed once the scope is exited...
const T * castAs() const
Member-template castAs<specific type>.
Definition: Type.h:6923
static CGCallee forDirect(llvm::Constant *functionPtr, const CGCalleeInfo &abstractInfo=CGCalleeInfo())
Definition: CGCall.h:133
#define V(N, I)
Definition: ASTContext.h:2921
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:5551
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2616
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:1342
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:719
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:201
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:2307
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:2140
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:1475
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:1252
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:313
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:2053
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:1312
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:2512
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:6264
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:2137
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:3779
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:2193
const Decl * getDecl() const
Definition: GlobalDecl.h:77
const BlockByrefInfo & getBlockByrefInfo(const VarDecl *var)
BuildByrefInfo - This routine changes a __block variable declared as T x into:
Definition: CGBlocks.cpp:2730
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:1286
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:1801
static bool isAccessedBy(const VarDecl &var, const Stmt *s)
Definition: CGDecl.cpp:631
bool hasGlobalStorage() const
Returns true for all variables that do not have local storage.
Definition: Decl.h:1092
static void emitStoresForZeroInit(CodeGenModule &CGM, const VarDecl &D, Address Loc, bool isVolatile, CGBuilderTy &Builder)
Definition: CGDecl.cpp:1222
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:1647
bool isObjCObjectPointerType() const
Definition: Type.h:6495
Address createUnnamedGlobalFrom(const VarDecl &D, llvm::Constant *Constant, CharUnits Align)
Definition: CGDecl.cpp:1077
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:1127
llvm::APInt APInt
Definition: Integral.h:27
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.
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:595
bool isUsed(bool CheckUsedAttr=true) const
Whether any (re-)declaration of the entity was used, meaning that a definition is required...
Definition: DeclBase.cpp:398
void enterByrefCleanup(CleanupKind Kind, Address Addr, BlockFieldFlags Flags, bool LoadBlockVarAddr, bool CanThrow)
Enter a cleanup to destroy a __block variable.
Definition: CGBlocks.cpp:2938
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:2345
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:3926
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:221
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:1056
Base class for declarations which introduce a typedef-name.
Definition: Decl.h:2985
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
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:4078
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:3040
const Expr * getInit() const
Definition: Decl.h:1234
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:992
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:2459
llvm::Module & getModule() const
static bool tryEmitARCCopyWeakInit(CodeGenFunction &CGF, const LValue &destLV, const Expr *init)
Definition: CGDecl.cpp:663
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:1848
static bool hasAggregateEvaluationKind(QualType T)
void EmitAutoVarCleanups(const AutoVarEmission &emission)
Definition: CGDecl.cpp:1978
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:4444
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:2108
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:3132
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:6508
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:740
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:461
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:3825
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:1189
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:6228
Represents a C++ struct/union/class.
Definition: DeclCXX.h:255
bool isNRVOVariable() const
Determine whether this local variable can be used with the named return value optimization (NRVO)...
Definition: Decl.h:1360
llvm::Type * ConvertType(QualType T)
Qualifiers getQualifiers() const
Retrieve the set of qualifiers applied to this type.
Definition: Type.h:6175
LValue EmitLValue(const Expr *E)
EmitLValue - Emit code to compute a designator that specifies the location of the expression...
Definition: CGExpr.cpp:1243
Address ReturnValue
ReturnValue - The temporary alloca to hold the return value.
bool isSamplerT() const
Definition: Type.h:6556
void pushStackRestore(CleanupKind kind, Address SPMem)
Definition: CGDecl.cpp:2069
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:1083
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:2453
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:1146
static RValue get(llvm::Value *V)
Definition: CGValue.h:85
void EmitLifetimeEnd(llvm::Value *Size, llvm::Value *Addr)
Definition: CGDecl.cpp:1302
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:2870
bool Null(InterpState &S, CodePtr OpPC)
Definition: Interp.h:821
bool isLocalVarDecl() const
Returns true for local variable declarations other than parameters.
Definition: Decl.h:1119
QualType getType() const
Definition: Decl.h:655
An l-value expression is a reference to an object with independent storage.
Definition: Specifiers.h:129
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:308
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:2541
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:723
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:2695
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:2278
bool isExternallyVisible() const
Definition: Decl.h:387
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:2962
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:1066