clang  9.0.0svn
CGDecl.cpp
Go to the documentation of this file.
1 //===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This contains code to emit Decl nodes as LLVM code.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "CGBlocks.h"
14 #include "CGCXXABI.h"
15 #include "CGCleanup.h"
16 #include "CGDebugInfo.h"
17 #include "CGOpenCLRuntime.h"
18 #include "CGOpenMPRuntime.h"
19 #include "CodeGenFunction.h"
20 #include "CodeGenModule.h"
21 #include "ConstantEmitter.h"
22 #include "PatternInit.h"
23 #include "TargetInfo.h"
24 #include "clang/AST/ASTContext.h"
25 #include "clang/AST/CharUnits.h"
26 #include "clang/AST/Decl.h"
27 #include "clang/AST/DeclObjC.h"
28 #include "clang/AST/DeclOpenMP.h"
31 #include "clang/Basic/TargetInfo.h"
33 #include "llvm/Analysis/ValueTracking.h"
34 #include "llvm/IR/DataLayout.h"
35 #include "llvm/IR/GlobalVariable.h"
36 #include "llvm/IR/Intrinsics.h"
37 #include "llvm/IR/Type.h"
38 
39 using namespace clang;
40 using namespace CodeGen;
41 
43  switch (D.getKind()) {
44  case Decl::BuiltinTemplate:
45  case Decl::TranslationUnit:
46  case Decl::ExternCContext:
47  case Decl::Namespace:
48  case Decl::UnresolvedUsingTypename:
49  case Decl::ClassTemplateSpecialization:
50  case Decl::ClassTemplatePartialSpecialization:
51  case Decl::VarTemplateSpecialization:
52  case Decl::VarTemplatePartialSpecialization:
53  case Decl::TemplateTypeParm:
54  case Decl::UnresolvedUsingValue:
55  case Decl::NonTypeTemplateParm:
56  case Decl::CXXDeductionGuide:
57  case Decl::CXXMethod:
58  case Decl::CXXConstructor:
59  case Decl::CXXDestructor:
60  case Decl::CXXConversion:
61  case Decl::Field:
62  case Decl::MSProperty:
63  case Decl::IndirectField:
64  case Decl::ObjCIvar:
65  case Decl::ObjCAtDefsField:
66  case Decl::ParmVar:
67  case Decl::ImplicitParam:
68  case Decl::ClassTemplate:
69  case Decl::VarTemplate:
70  case Decl::FunctionTemplate:
71  case Decl::TypeAliasTemplate:
72  case Decl::TemplateTemplateParm:
73  case Decl::ObjCMethod:
74  case Decl::ObjCCategory:
75  case Decl::ObjCProtocol:
76  case Decl::ObjCInterface:
77  case Decl::ObjCCategoryImpl:
78  case Decl::ObjCImplementation:
79  case Decl::ObjCProperty:
80  case Decl::ObjCCompatibleAlias:
81  case Decl::PragmaComment:
82  case Decl::PragmaDetectMismatch:
83  case Decl::AccessSpec:
84  case Decl::LinkageSpec:
85  case Decl::Export:
86  case Decl::ObjCPropertyImpl:
87  case Decl::FileScopeAsm:
88  case Decl::Friend:
89  case Decl::FriendTemplate:
90  case Decl::Block:
91  case Decl::Captured:
92  case Decl::ClassScopeFunctionSpecialization:
93  case Decl::UsingShadow:
94  case Decl::ConstructorUsingShadow:
95  case Decl::ObjCTypeParam:
96  case Decl::Binding:
97  llvm_unreachable("Declaration should not be in declstmts!");
98  case Decl::Function: // void X();
99  case Decl::Record: // struct/union/class X;
100  case Decl::Enum: // enum X;
101  case Decl::EnumConstant: // enum ? { X = ? }
102  case Decl::CXXRecord: // struct/union/class X; [C++]
103  case Decl::StaticAssert: // static_assert(X, ""); [C++0x]
104  case Decl::Label: // __label__ x;
105  case Decl::Import:
106  case Decl::OMPThreadPrivate:
107  case Decl::OMPAllocate:
108  case Decl::OMPCapturedExpr:
109  case Decl::OMPRequires:
110  case Decl::Empty:
111  // None of these decls require codegen support.
112  return;
113 
114  case Decl::NamespaceAlias:
115  if (CGDebugInfo *DI = getDebugInfo())
116  DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(D));
117  return;
118  case Decl::Using: // using X; [C++]
119  if (CGDebugInfo *DI = getDebugInfo())
120  DI->EmitUsingDecl(cast<UsingDecl>(D));
121  return;
122  case Decl::UsingPack:
123  for (auto *Using : cast<UsingPackDecl>(D).expansions())
124  EmitDecl(*Using);
125  return;
126  case Decl::UsingDirective: // using namespace X; [C++]
127  if (CGDebugInfo *DI = getDebugInfo())
128  DI->EmitUsingDirective(cast<UsingDirectiveDecl>(D));
129  return;
130  case Decl::Var:
131  case Decl::Decomposition: {
132  const VarDecl &VD = cast<VarDecl>(D);
133  assert(VD.isLocalVarDecl() &&
134  "Should not see file-scope variables inside a function!");
135  EmitVarDecl(VD);
136  if (auto *DD = dyn_cast<DecompositionDecl>(&VD))
137  for (auto *B : DD->bindings())
138  if (auto *HD = B->getHoldingVar())
139  EmitVarDecl(*HD);
140  return;
141  }
142 
143  case Decl::OMPDeclareReduction:
144  return CGM.EmitOMPDeclareReduction(cast<OMPDeclareReductionDecl>(&D), this);
145 
146  case Decl::OMPDeclareMapper:
147  return CGM.EmitOMPDeclareMapper(cast<OMPDeclareMapperDecl>(&D), this);
148 
149  case Decl::Typedef: // typedef int X;
150  case Decl::TypeAlias: { // using X = int; [C++0x]
151  const TypedefNameDecl &TD = cast<TypedefNameDecl>(D);
152  QualType Ty = TD.getUnderlyingType();
153 
154  if (Ty->isVariablyModifiedType())
156  }
157  }
158 }
159 
160 /// EmitVarDecl - This method handles emission of any variable declaration
161 /// inside a function, including static vars etc.
163  if (D.hasExternalStorage())
164  // Don't emit it now, allow it to be emitted lazily on its first use.
165  return;
166 
167  // Some function-scope variable does not have static storage but still
168  // needs to be emitted like a static variable, e.g. a function-scope
169  // variable in constant address space in OpenCL.
170  if (D.getStorageDuration() != SD_Automatic) {
171  // Static sampler variables translated to function calls.
172  if (D.getType()->isSamplerT())
173  return;
174 
175  llvm::GlobalValue::LinkageTypes Linkage =
176  CGM.getLLVMLinkageVarDefinition(&D, /*isConstant=*/false);
177 
178  // FIXME: We need to force the emission/use of a guard variable for
179  // some variables even if we can constant-evaluate them because
180  // we can't guarantee every translation unit will constant-evaluate them.
181 
182  return EmitStaticVarDecl(D, Linkage);
183  }
184 
187 
188  assert(D.hasLocalStorage());
189  return EmitAutoVarDecl(D);
190 }
191 
192 static std::string getStaticDeclName(CodeGenModule &CGM, const VarDecl &D) {
193  if (CGM.getLangOpts().CPlusPlus)
194  return CGM.getMangledName(&D).str();
195 
196  // If this isn't C++, we don't need a mangled name, just a pretty one.
197  assert(!D.isExternallyVisible() && "name shouldn't matter");
198  std::string ContextName;
199  const DeclContext *DC = D.getDeclContext();
200  if (auto *CD = dyn_cast<CapturedDecl>(DC))
201  DC = cast<DeclContext>(CD->getNonClosureContext());
202  if (const auto *FD = dyn_cast<FunctionDecl>(DC))
203  ContextName = CGM.getMangledName(FD);
204  else if (const auto *BD = dyn_cast<BlockDecl>(DC))
205  ContextName = CGM.getBlockMangledName(GlobalDecl(), BD);
206  else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(DC))
207  ContextName = OMD->getSelector().getAsString();
208  else
209  llvm_unreachable("Unknown context for static var decl");
210 
211  ContextName += "." + D.getNameAsString();
212  return ContextName;
213 }
214 
216  const VarDecl &D, llvm::GlobalValue::LinkageTypes Linkage) {
217  // In general, we don't always emit static var decls once before we reference
218  // them. It is possible to reference them before emitting the function that
219  // contains them, and it is possible to emit the containing function multiple
220  // times.
221  if (llvm::Constant *ExistingGV = StaticLocalDeclMap[&D])
222  return ExistingGV;
223 
224  QualType Ty = D.getType();
225  assert(Ty->isConstantSizeType() && "VLAs can't be static");
226 
227  // Use the label if the variable is renamed with the asm-label extension.
228  std::string Name;
229  if (D.hasAttr<AsmLabelAttr>())
230  Name = getMangledName(&D);
231  else
232  Name = getStaticDeclName(*this, D);
233 
235  LangAS AS = GetGlobalVarAddressSpace(&D);
236  unsigned TargetAS = getContext().getTargetAddressSpace(AS);
237 
238  // OpenCL variables in local address space and CUDA shared
239  // variables cannot have an initializer.
240  llvm::Constant *Init = nullptr;
242  D.hasAttr<CUDASharedAttr>())
243  Init = llvm::UndefValue::get(LTy);
244  else
245  Init = EmitNullConstant(Ty);
246 
247  llvm::GlobalVariable *GV = new llvm::GlobalVariable(
248  getModule(), LTy, Ty.isConstant(getContext()), Linkage, Init, Name,
249  nullptr, llvm::GlobalVariable::NotThreadLocal, TargetAS);
250  GV->setAlignment(getContext().getDeclAlign(&D).getQuantity());
251 
252  if (supportsCOMDAT() && GV->isWeakForLinker())
253  GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
254 
255  if (D.getTLSKind())
256  setTLSMode(GV, D);
257 
258  setGVProperties(GV, &D);
259 
260  // Make sure the result is of the correct type.
261  LangAS ExpectedAS = Ty.getAddressSpace();
262  llvm::Constant *Addr = GV;
263  if (AS != ExpectedAS) {
264  Addr = getTargetCodeGenInfo().performAddrSpaceCast(
265  *this, GV, AS, ExpectedAS,
266  LTy->getPointerTo(getContext().getTargetAddressSpace(ExpectedAS)));
267  }
268 
269  setStaticLocalDeclAddress(&D, Addr);
270 
271  // Ensure that the static local gets initialized by making sure the parent
272  // function gets emitted eventually.
273  const Decl *DC = cast<Decl>(D.getDeclContext());
274 
275  // We can't name blocks or captured statements directly, so try to emit their
276  // parents.
277  if (isa<BlockDecl>(DC) || isa<CapturedDecl>(DC)) {
278  DC = DC->getNonClosureContext();
279  // FIXME: Ensure that global blocks get emitted.
280  if (!DC)
281  return Addr;
282  }
283 
284  GlobalDecl GD;
285  if (const auto *CD = dyn_cast<CXXConstructorDecl>(DC))
286  GD = GlobalDecl(CD, Ctor_Base);
287  else if (const auto *DD = dyn_cast<CXXDestructorDecl>(DC))
288  GD = GlobalDecl(DD, Dtor_Base);
289  else if (const auto *FD = dyn_cast<FunctionDecl>(DC))
290  GD = GlobalDecl(FD);
291  else {
292  // Don't do anything for Obj-C method decls or global closures. We should
293  // never defer them.
294  assert(isa<ObjCMethodDecl>(DC) && "unexpected parent code decl");
295  }
296  if (GD.getDecl()) {
297  // Disable emission of the parent function for the OpenMP device codegen.
299  (void)GetAddrOfGlobal(GD);
300  }
301 
302  return Addr;
303 }
304 
305 /// hasNontrivialDestruction - Determine whether a type's destruction is
306 /// non-trivial. If so, and the variable uses static initialization, we must
307 /// register its destructor to run on exit.
310  return RD && !RD->hasTrivialDestructor();
311 }
312 
313 /// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the
314 /// global variable that has already been created for it. If the initializer
315 /// has a different type than GV does, this may free GV and return a different
316 /// one. Otherwise it just returns GV.
317 llvm::GlobalVariable *
319  llvm::GlobalVariable *GV) {
320  ConstantEmitter emitter(*this);
321  llvm::Constant *Init = emitter.tryEmitForInitializer(D);
322 
323  // If constant emission failed, then this should be a C++ static
324  // initializer.
325  if (!Init) {
326  if (!getLangOpts().CPlusPlus)
327  CGM.ErrorUnsupported(D.getInit(), "constant l-value expression");
328  else if (HaveInsertPoint()) {
329  // Since we have a static initializer, this global variable can't
330  // be constant.
331  GV->setConstant(false);
332 
333  EmitCXXGuardedInit(D, GV, /*PerformInit*/true);
334  }
335  return GV;
336  }
337 
338  // The initializer may differ in type from the global. Rewrite
339  // the global to match the initializer. (We have to do this
340  // because some types, like unions, can't be completely represented
341  // in the LLVM type system.)
342  if (GV->getType()->getElementType() != Init->getType()) {
343  llvm::GlobalVariable *OldGV = GV;
344 
345  GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
346  OldGV->isConstant(),
347  OldGV->getLinkage(), Init, "",
348  /*InsertBefore*/ OldGV,
349  OldGV->getThreadLocalMode(),
351  GV->setVisibility(OldGV->getVisibility());
352  GV->setDSOLocal(OldGV->isDSOLocal());
353  GV->setComdat(OldGV->getComdat());
354 
355  // Steal the name of the old global
356  GV->takeName(OldGV);
357 
358  // Replace all uses of the old global with the new global
359  llvm::Constant *NewPtrForOldDecl =
360  llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
361  OldGV->replaceAllUsesWith(NewPtrForOldDecl);
362 
363  // Erase the old global, since it is no longer used.
364  OldGV->eraseFromParent();
365  }
366 
367  GV->setConstant(CGM.isTypeConstant(D.getType(), true));
368  GV->setInitializer(Init);
369 
370  emitter.finalize(GV);
371 
373  // We have a constant initializer, but a nontrivial destructor. We still
374  // need to perform a guarded "initialization" in order to register the
375  // destructor.
376  EmitCXXGuardedInit(D, GV, /*PerformInit*/false);
377  }
378 
379  return GV;
380 }
381 
383  llvm::GlobalValue::LinkageTypes Linkage) {
384  // Check to see if we already have a global variable for this
385  // declaration. This can happen when double-emitting function
386  // bodies, e.g. with complete and base constructors.
387  llvm::Constant *addr = CGM.getOrCreateStaticVarDecl(D, Linkage);
388  CharUnits alignment = getContext().getDeclAlign(&D);
389 
390  // Store into LocalDeclMap before generating initializer to handle
391  // circular references.
392  setAddrOfLocalVar(&D, Address(addr, alignment));
393 
394  // We can't have a VLA here, but we can have a pointer to a VLA,
395  // even though that doesn't really make any sense.
396  // Make sure to evaluate VLA bounds now so that we have them for later.
397  if (D.getType()->isVariablyModifiedType())
399 
400  // Save the type in case adding the initializer forces a type change.
401  llvm::Type *expectedType = addr->getType();
402 
403  llvm::GlobalVariable *var =
404  cast<llvm::GlobalVariable>(addr->stripPointerCasts());
405 
406  // CUDA's local and local static __shared__ variables should not
407  // have any non-empty initializers. This is ensured by Sema.
408  // Whatever initializer such variable may have when it gets here is
409  // a no-op and should not be emitted.
410  bool isCudaSharedVar = getLangOpts().CUDA && getLangOpts().CUDAIsDevice &&
411  D.hasAttr<CUDASharedAttr>();
412  // If this value has an initializer, emit it.
413  if (D.getInit() && !isCudaSharedVar)
414  var = AddInitializerToStaticVarDecl(D, var);
415 
416  var->setAlignment(alignment.getQuantity());
417 
418  if (D.hasAttr<AnnotateAttr>())
419  CGM.AddGlobalAnnotations(&D, var);
420 
421  if (auto *SA = D.getAttr<PragmaClangBSSSectionAttr>())
422  var->addAttribute("bss-section", SA->getName());
423  if (auto *SA = D.getAttr<PragmaClangDataSectionAttr>())
424  var->addAttribute("data-section", SA->getName());
425  if (auto *SA = D.getAttr<PragmaClangRodataSectionAttr>())
426  var->addAttribute("rodata-section", SA->getName());
427 
428  if (const SectionAttr *SA = D.getAttr<SectionAttr>())
429  var->setSection(SA->getName());
430 
431  if (D.hasAttr<UsedAttr>())
432  CGM.addUsedGlobal(var);
433 
434  // We may have to cast the constant because of the initializer
435  // mismatch above.
436  //
437  // FIXME: It is really dangerous to store this in the map; if anyone
438  // RAUW's the GV uses of this constant will be invalid.
439  llvm::Constant *castedAddr =
440  llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(var, expectedType);
441  if (var != castedAddr)
442  LocalDeclMap.find(&D)->second = Address(castedAddr, alignment);
443  CGM.setStaticLocalDeclAddress(&D, castedAddr);
444 
446 
447  // Emit global variable debug descriptor for static vars.
448  CGDebugInfo *DI = getDebugInfo();
449  if (DI &&
451  DI->setLocation(D.getLocation());
452  DI->EmitGlobalVariable(var, &D);
453  }
454 }
455 
456 namespace {
457  struct DestroyObject final : EHScopeStack::Cleanup {
458  DestroyObject(Address addr, QualType type,
459  CodeGenFunction::Destroyer *destroyer,
460  bool useEHCleanupForArray)
461  : addr(addr), type(type), destroyer(destroyer),
462  useEHCleanupForArray(useEHCleanupForArray) {}
463 
464  Address addr;
465  QualType type;
466  CodeGenFunction::Destroyer *destroyer;
467  bool useEHCleanupForArray;
468 
469  void Emit(CodeGenFunction &CGF, Flags flags) override {
470  // Don't use an EH cleanup recursively from an EH cleanup.
471  bool useEHCleanupForArray =
472  flags.isForNormalCleanup() && this->useEHCleanupForArray;
473 
474  CGF.emitDestroy(addr, type, destroyer, useEHCleanupForArray);
475  }
476  };
477 
478  template <class Derived>
479  struct DestroyNRVOVariable : EHScopeStack::Cleanup {
480  DestroyNRVOVariable(Address addr, llvm::Value *NRVOFlag)
481  : NRVOFlag(NRVOFlag), Loc(addr) {}
482 
483  llvm::Value *NRVOFlag;
484  Address Loc;
485 
486  void Emit(CodeGenFunction &CGF, Flags flags) override {
487  // Along the exceptions path we always execute the dtor.
488  bool NRVO = flags.isForNormalCleanup() && NRVOFlag;
489 
490  llvm::BasicBlock *SkipDtorBB = nullptr;
491  if (NRVO) {
492  // If we exited via NRVO, we skip the destructor call.
493  llvm::BasicBlock *RunDtorBB = CGF.createBasicBlock("nrvo.unused");
494  SkipDtorBB = CGF.createBasicBlock("nrvo.skipdtor");
495  llvm::Value *DidNRVO =
496  CGF.Builder.CreateFlagLoad(NRVOFlag, "nrvo.val");
497  CGF.Builder.CreateCondBr(DidNRVO, SkipDtorBB, RunDtorBB);
498  CGF.EmitBlock(RunDtorBB);
499  }
500 
501  static_cast<Derived *>(this)->emitDestructorCall(CGF);
502 
503  if (NRVO) CGF.EmitBlock(SkipDtorBB);
504  }
505 
506  virtual ~DestroyNRVOVariable() = default;
507  };
508 
509  struct DestroyNRVOVariableCXX final
510  : DestroyNRVOVariable<DestroyNRVOVariableCXX> {
511  DestroyNRVOVariableCXX(Address addr, const CXXDestructorDecl *Dtor,
512  llvm::Value *NRVOFlag)
513  : DestroyNRVOVariable<DestroyNRVOVariableCXX>(addr, NRVOFlag),
514  Dtor(Dtor) {}
515 
516  const CXXDestructorDecl *Dtor;
517 
518  void emitDestructorCall(CodeGenFunction &CGF) {
520  /*ForVirtualBase=*/false,
521  /*Delegating=*/false, Loc);
522  }
523  };
524 
525  struct DestroyNRVOVariableC final
526  : DestroyNRVOVariable<DestroyNRVOVariableC> {
527  DestroyNRVOVariableC(Address addr, llvm::Value *NRVOFlag, QualType Ty)
528  : DestroyNRVOVariable<DestroyNRVOVariableC>(addr, NRVOFlag), Ty(Ty) {}
529 
530  QualType Ty;
531 
532  void emitDestructorCall(CodeGenFunction &CGF) {
533  CGF.destroyNonTrivialCStruct(CGF, Loc, Ty);
534  }
535  };
536 
537  struct CallStackRestore final : EHScopeStack::Cleanup {
538  Address Stack;
539  CallStackRestore(Address Stack) : Stack(Stack) {}
540  void Emit(CodeGenFunction &CGF, Flags flags) override {
541  llvm::Value *V = CGF.Builder.CreateLoad(Stack);
542  llvm::Function *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stackrestore);
543  CGF.Builder.CreateCall(F, V);
544  }
545  };
546 
547  struct ExtendGCLifetime final : EHScopeStack::Cleanup {
548  const VarDecl &Var;
549  ExtendGCLifetime(const VarDecl *var) : Var(*var) {}
550 
551  void Emit(CodeGenFunction &CGF, Flags flags) override {
552  // Compute the address of the local variable, in case it's a
553  // byref or something.
554  DeclRefExpr DRE(CGF.getContext(), const_cast<VarDecl *>(&Var), false,
555  Var.getType(), VK_LValue, SourceLocation());
556  llvm::Value *value = CGF.EmitLoadOfScalar(CGF.EmitDeclRefLValue(&DRE),
557  SourceLocation());
558  CGF.EmitExtendGCLifetime(value);
559  }
560  };
561 
562  struct CallCleanupFunction final : EHScopeStack::Cleanup {
563  llvm::Constant *CleanupFn;
564  const CGFunctionInfo &FnInfo;
565  const VarDecl &Var;
566 
567  CallCleanupFunction(llvm::Constant *CleanupFn, const CGFunctionInfo *Info,
568  const VarDecl *Var)
569  : CleanupFn(CleanupFn), FnInfo(*Info), Var(*Var) {}
570 
571  void Emit(CodeGenFunction &CGF, Flags flags) override {
572  DeclRefExpr DRE(CGF.getContext(), const_cast<VarDecl *>(&Var), false,
573  Var.getType(), VK_LValue, SourceLocation());
574  // Compute the address of the local variable, in case it's a byref
575  // or something.
576  llvm::Value *Addr = CGF.EmitDeclRefLValue(&DRE).getPointer();
577 
578  // In some cases, the type of the function argument will be different from
579  // the type of the pointer. An example of this is
580  // void f(void* arg);
581  // __attribute__((cleanup(f))) void *g;
582  //
583  // To fix this we insert a bitcast here.
584  QualType ArgTy = FnInfo.arg_begin()->type;
585  llvm::Value *Arg =
586  CGF.Builder.CreateBitCast(Addr, CGF.ConvertType(ArgTy));
587 
588  CallArgList Args;
589  Args.add(RValue::get(Arg),
590  CGF.getContext().getPointerType(Var.getType()));
591  auto Callee = CGCallee::forDirect(CleanupFn);
592  CGF.EmitCall(FnInfo, Callee, ReturnValueSlot(), Args);
593  }
594  };
595 } // end anonymous namespace
596 
597 /// EmitAutoVarWithLifetime - Does the setup required for an automatic
598 /// variable with lifetime.
599 static void EmitAutoVarWithLifetime(CodeGenFunction &CGF, const VarDecl &var,
600  Address addr,
601  Qualifiers::ObjCLifetime lifetime) {
602  switch (lifetime) {
604  llvm_unreachable("present but none");
605 
607  // nothing to do
608  break;
609 
610  case Qualifiers::OCL_Strong: {
611  CodeGenFunction::Destroyer *destroyer =
612  (var.hasAttr<ObjCPreciseLifetimeAttr>()
615 
616  CleanupKind cleanupKind = CGF.getARCCleanupKind();
617  CGF.pushDestroy(cleanupKind, addr, var.getType(), destroyer,
618  cleanupKind & EHCleanup);
619  break;
620  }
622  // nothing to do
623  break;
624 
626  // __weak objects always get EH cleanups; otherwise, exceptions
627  // could cause really nasty crashes instead of mere leaks.
628  CGF.pushDestroy(NormalAndEHCleanup, addr, var.getType(),
630  /*useEHCleanup*/ true);
631  break;
632  }
633 }
634 
635 static bool isAccessedBy(const VarDecl &var, const Stmt *s) {
636  if (const Expr *e = dyn_cast<Expr>(s)) {
637  // Skip the most common kinds of expressions that make
638  // hierarchy-walking expensive.
639  s = e = e->IgnoreParenCasts();
640 
641  if (const DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e))
642  return (ref->getDecl() == &var);
643  if (const BlockExpr *be = dyn_cast<BlockExpr>(e)) {
644  const BlockDecl *block = be->getBlockDecl();
645  for (const auto &I : block->captures()) {
646  if (I.getVariable() == &var)
647  return true;
648  }
649  }
650  }
651 
652  for (const Stmt *SubStmt : s->children())
653  // SubStmt might be null; as in missing decl or conditional of an if-stmt.
654  if (SubStmt && isAccessedBy(var, SubStmt))
655  return true;
656 
657  return false;
658 }
659 
660 static bool isAccessedBy(const ValueDecl *decl, const Expr *e) {
661  if (!decl) return false;
662  if (!isa<VarDecl>(decl)) return false;
663  const VarDecl *var = cast<VarDecl>(decl);
664  return isAccessedBy(*var, e);
665 }
666 
668  const LValue &destLV, const Expr *init) {
669  bool needsCast = false;
670 
671  while (auto castExpr = dyn_cast<CastExpr>(init->IgnoreParens())) {
672  switch (castExpr->getCastKind()) {
673  // Look through casts that don't require representation changes.
674  case CK_NoOp:
675  case CK_BitCast:
676  case CK_BlockPointerToObjCPointerCast:
677  needsCast = true;
678  break;
679 
680  // If we find an l-value to r-value cast from a __weak variable,
681  // emit this operation as a copy or move.
682  case CK_LValueToRValue: {
683  const Expr *srcExpr = castExpr->getSubExpr();
684  if (srcExpr->getType().getObjCLifetime() != Qualifiers::OCL_Weak)
685  return false;
686 
687  // Emit the source l-value.
688  LValue srcLV = CGF.EmitLValue(srcExpr);
689 
690  // Handle a formal type change to avoid asserting.
691  auto srcAddr = srcLV.getAddress();
692  if (needsCast) {
693  srcAddr = CGF.Builder.CreateElementBitCast(srcAddr,
694  destLV.getAddress().getElementType());
695  }
696 
697  // If it was an l-value, use objc_copyWeak.
698  if (srcExpr->getValueKind() == VK_LValue) {
699  CGF.EmitARCCopyWeak(destLV.getAddress(), srcAddr);
700  } else {
701  assert(srcExpr->getValueKind() == VK_XValue);
702  CGF.EmitARCMoveWeak(destLV.getAddress(), srcAddr);
703  }
704  return true;
705  }
706 
707  // Stop at anything else.
708  default:
709  return false;
710  }
711 
712  init = castExpr->getSubExpr();
713  }
714  return false;
715 }
716 
718  LValue &lvalue,
719  const VarDecl *var) {
720  lvalue.setAddress(CGF.emitBlockByrefAddress(lvalue.getAddress(), var));
721 }
722 
724  SourceLocation Loc) {
725  if (!SanOpts.has(SanitizerKind::NullabilityAssign))
726  return;
727 
728  auto Nullability = LHS.getType()->getNullability(getContext());
730  return;
731 
732  // Check if the right hand side of the assignment is nonnull, if the left
733  // hand side must be nonnull.
734  SanitizerScope SanScope(this);
735  llvm::Value *IsNotNull = Builder.CreateIsNotNull(RHS);
736  llvm::Constant *StaticData[] = {
738  llvm::ConstantInt::get(Int8Ty, 0), // The LogAlignment info is unused.
739  llvm::ConstantInt::get(Int8Ty, TCK_NonnullAssign)};
740  EmitCheck({{IsNotNull, SanitizerKind::NullabilityAssign}},
741  SanitizerHandler::TypeMismatch, StaticData, RHS);
742 }
743 
744 void CodeGenFunction::EmitScalarInit(const Expr *init, const ValueDecl *D,
745  LValue lvalue, bool capturedByInit) {
746  Qualifiers::ObjCLifetime lifetime = lvalue.getObjCLifetime();
747  if (!lifetime) {
748  llvm::Value *value = EmitScalarExpr(init);
749  if (capturedByInit)
750  drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
751  EmitNullabilityCheck(lvalue, value, init->getExprLoc());
752  EmitStoreThroughLValue(RValue::get(value), lvalue, true);
753  return;
754  }
755 
756  if (const CXXDefaultInitExpr *DIE = dyn_cast<CXXDefaultInitExpr>(init))
757  init = DIE->getExpr();
758 
759  // If we're emitting a value with lifetime, we have to do the
760  // initialization *before* we leave the cleanup scopes.
761  if (const FullExpr *fe = dyn_cast<FullExpr>(init)) {
763  init = fe->getSubExpr();
764  }
766 
767  // We have to maintain the illusion that the variable is
768  // zero-initialized. If the variable might be accessed in its
769  // initializer, zero-initialize before running the initializer, then
770  // actually perform the initialization with an assign.
771  bool accessedByInit = false;
772  if (lifetime != Qualifiers::OCL_ExplicitNone)
773  accessedByInit = (capturedByInit || isAccessedBy(D, init));
774  if (accessedByInit) {
775  LValue tempLV = lvalue;
776  // Drill down to the __block object if necessary.
777  if (capturedByInit) {
778  // We can use a simple GEP for this because it can't have been
779  // moved yet.
781  cast<VarDecl>(D),
782  /*follow*/ false));
783  }
784 
785  auto ty = cast<llvm::PointerType>(tempLV.getAddress().getElementType());
786  llvm::Value *zero = CGM.getNullPointer(ty, tempLV.getType());
787 
788  // If __weak, we want to use a barrier under certain conditions.
789  if (lifetime == Qualifiers::OCL_Weak)
790  EmitARCInitWeak(tempLV.getAddress(), zero);
791 
792  // Otherwise just do a simple store.
793  else
794  EmitStoreOfScalar(zero, tempLV, /* isInitialization */ true);
795  }
796 
797  // Emit the initializer.
798  llvm::Value *value = nullptr;
799 
800  switch (lifetime) {
802  llvm_unreachable("present but none");
803 
804  case Qualifiers::OCL_Strong: {
805  if (!D || !isa<VarDecl>(D) || !cast<VarDecl>(D)->isARCPseudoStrong()) {
806  value = EmitARCRetainScalarExpr(init);
807  break;
808  }
809  // If D is pseudo-strong, treat it like __unsafe_unretained here. This means
810  // that we omit the retain, and causes non-autoreleased return values to be
811  // immediately released.
812  LLVM_FALLTHROUGH;
813  }
814 
816  value = EmitARCUnsafeUnretainedScalarExpr(init);
817  break;
818 
819  case Qualifiers::OCL_Weak: {
820  // If it's not accessed by the initializer, try to emit the
821  // initialization with a copy or move.
822  if (!accessedByInit && tryEmitARCCopyWeakInit(*this, lvalue, init)) {
823  return;
824  }
825 
826  // No way to optimize a producing initializer into this. It's not
827  // worth optimizing for, because the value will immediately
828  // disappear in the common case.
829  value = EmitScalarExpr(init);
830 
831  if (capturedByInit) drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
832  if (accessedByInit)
833  EmitARCStoreWeak(lvalue.getAddress(), value, /*ignored*/ true);
834  else
835  EmitARCInitWeak(lvalue.getAddress(), value);
836  return;
837  }
838 
841  break;
842  }
843 
844  if (capturedByInit) drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
845 
846  EmitNullabilityCheck(lvalue, value, init->getExprLoc());
847 
848  // If the variable might have been accessed by its initializer, we
849  // might have to initialize with a barrier. We have to do this for
850  // both __weak and __strong, but __weak got filtered out above.
851  if (accessedByInit && lifetime == Qualifiers::OCL_Strong) {
852  llvm::Value *oldValue = EmitLoadOfScalar(lvalue, init->getExprLoc());
853  EmitStoreOfScalar(value, lvalue, /* isInitialization */ true);
855  return;
856  }
857 
858  EmitStoreOfScalar(value, lvalue, /* isInitialization */ true);
859 }
860 
861 /// Decide whether we can emit the non-zero parts of the specified initializer
862 /// with equal or fewer than NumStores scalar stores.
863 static bool canEmitInitWithFewStoresAfterBZero(llvm::Constant *Init,
864  unsigned &NumStores) {
865  // Zero and Undef never requires any extra stores.
866  if (isa<llvm::ConstantAggregateZero>(Init) ||
867  isa<llvm::ConstantPointerNull>(Init) ||
868  isa<llvm::UndefValue>(Init))
869  return true;
870  if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) ||
871  isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) ||
872  isa<llvm::ConstantExpr>(Init))
873  return Init->isNullValue() || NumStores--;
874 
875  // See if we can emit each element.
876  if (isa<llvm::ConstantArray>(Init) || isa<llvm::ConstantStruct>(Init)) {
877  for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) {
878  llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i));
879  if (!canEmitInitWithFewStoresAfterBZero(Elt, NumStores))
880  return false;
881  }
882  return true;
883  }
884 
885  if (llvm::ConstantDataSequential *CDS =
886  dyn_cast<llvm::ConstantDataSequential>(Init)) {
887  for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
888  llvm::Constant *Elt = CDS->getElementAsConstant(i);
889  if (!canEmitInitWithFewStoresAfterBZero(Elt, NumStores))
890  return false;
891  }
892  return true;
893  }
894 
895  // Anything else is hard and scary.
896  return false;
897 }
898 
899 /// For inits that canEmitInitWithFewStoresAfterBZero returned true for, emit
900 /// the scalar stores that would be required.
902  llvm::Constant *Init, Address Loc,
903  bool isVolatile, CGBuilderTy &Builder) {
904  assert(!Init->isNullValue() && !isa<llvm::UndefValue>(Init) &&
905  "called emitStoresForInitAfterBZero for zero or undef value.");
906 
907  if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) ||
908  isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) ||
909  isa<llvm::ConstantExpr>(Init)) {
910  Builder.CreateStore(Init, Loc, isVolatile);
911  return;
912  }
913 
914  if (llvm::ConstantDataSequential *CDS =
915  dyn_cast<llvm::ConstantDataSequential>(Init)) {
916  for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
917  llvm::Constant *Elt = CDS->getElementAsConstant(i);
918 
919  // If necessary, get a pointer to the element and emit it.
920  if (!Elt->isNullValue() && !isa<llvm::UndefValue>(Elt))
922  CGM, Elt, Builder.CreateConstInBoundsGEP2_32(Loc, 0, i), isVolatile,
923  Builder);
924  }
925  return;
926  }
927 
928  assert((isa<llvm::ConstantStruct>(Init) || isa<llvm::ConstantArray>(Init)) &&
929  "Unknown value type!");
930 
931  for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) {
932  llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i));
933 
934  // If necessary, get a pointer to the element and emit it.
935  if (!Elt->isNullValue() && !isa<llvm::UndefValue>(Elt))
937  Builder.CreateConstInBoundsGEP2_32(Loc, 0, i),
938  isVolatile, Builder);
939  }
940 }
941 
942 /// Decide whether we should use bzero plus some stores to initialize a local
943 /// variable instead of using a memcpy from a constant global. It is beneficial
944 /// to use bzero if the global is all zeros, or mostly zeros and large.
945 static bool shouldUseBZeroPlusStoresToInitialize(llvm::Constant *Init,
946  uint64_t GlobalSize) {
947  // If a global is all zeros, always use a bzero.
948  if (isa<llvm::ConstantAggregateZero>(Init)) return true;
949 
950  // If a non-zero global is <= 32 bytes, always use a memcpy. If it is large,
951  // do it if it will require 6 or fewer scalar stores.
952  // TODO: Should budget depends on the size? Avoiding a large global warrants
953  // plopping in more stores.
954  unsigned StoreBudget = 6;
955  uint64_t SizeLimit = 32;
956 
957  return GlobalSize > SizeLimit &&
958  canEmitInitWithFewStoresAfterBZero(Init, StoreBudget);
959 }
960 
961 /// Decide whether we should use memset to initialize a local variable instead
962 /// of using a memcpy from a constant global. Assumes we've already decided to
963 /// not user bzero.
964 /// FIXME We could be more clever, as we are for bzero above, and generate
965 /// memset followed by stores. It's unclear that's worth the effort.
966 static llvm::Value *shouldUseMemSetToInitialize(llvm::Constant *Init,
967  uint64_t GlobalSize) {
968  uint64_t SizeLimit = 32;
969  if (GlobalSize <= SizeLimit)
970  return nullptr;
971  return llvm::isBytewiseValue(Init);
972 }
973 
974 /// Decide whether we want to split a constant structure or array store into a
975 /// sequence of its fields' stores. This may cost us code size and compilation
976 /// speed, but plays better with store optimizations.
978  uint64_t GlobalByteSize) {
979  // Don't break things that occupy more than one cacheline.
980  uint64_t ByteSizeLimit = 64;
981  if (CGM.getCodeGenOpts().OptimizationLevel == 0)
982  return false;
983  if (GlobalByteSize <= ByteSizeLimit)
984  return true;
985  return false;
986 }
987 
988 enum class IsPattern { No, Yes };
989 
990 /// Generate a constant filled with either a pattern or zeroes.
991 static llvm::Constant *patternOrZeroFor(CodeGenModule &CGM, IsPattern isPattern,
992  llvm::Type *Ty) {
993  if (isPattern == IsPattern::Yes)
994  return initializationPatternFor(CGM, Ty);
995  else
996  return llvm::Constant::getNullValue(Ty);
997 }
998 
999 static llvm::Constant *constWithPadding(CodeGenModule &CGM, IsPattern isPattern,
1000  llvm::Constant *constant);
1001 
1002 /// Helper function for constWithPadding() to deal with padding in structures.
1003 static llvm::Constant *constStructWithPadding(CodeGenModule &CGM,
1004  IsPattern isPattern,
1005  llvm::StructType *STy,
1006  llvm::Constant *constant) {
1007  const llvm::DataLayout &DL = CGM.getDataLayout();
1008  const llvm::StructLayout *Layout = DL.getStructLayout(STy);
1009  llvm::Type *Int8Ty = llvm::IntegerType::getInt8Ty(CGM.getLLVMContext());
1010  unsigned SizeSoFar = 0;
1012  bool NestedIntact = true;
1013  for (unsigned i = 0, e = STy->getNumElements(); i != e; i++) {
1014  unsigned CurOff = Layout->getElementOffset(i);
1015  if (SizeSoFar < CurOff) {
1016  assert(!STy->isPacked());
1017  auto *PadTy = llvm::ArrayType::get(Int8Ty, CurOff - SizeSoFar);
1018  Values.push_back(patternOrZeroFor(CGM, isPattern, PadTy));
1019  }
1020  llvm::Constant *CurOp;
1021  if (constant->isZeroValue())
1022  CurOp = llvm::Constant::getNullValue(STy->getElementType(i));
1023  else
1024  CurOp = cast<llvm::Constant>(constant->getAggregateElement(i));
1025  auto *NewOp = constWithPadding(CGM, isPattern, CurOp);
1026  if (CurOp != NewOp)
1027  NestedIntact = false;
1028  Values.push_back(NewOp);
1029  SizeSoFar = CurOff + DL.getTypeAllocSize(CurOp->getType());
1030  }
1031  unsigned TotalSize = Layout->getSizeInBytes();
1032  if (SizeSoFar < TotalSize) {
1033  auto *PadTy = llvm::ArrayType::get(Int8Ty, TotalSize - SizeSoFar);
1034  Values.push_back(patternOrZeroFor(CGM, isPattern, PadTy));
1035  }
1036  if (NestedIntact && Values.size() == STy->getNumElements())
1037  return constant;
1038  return llvm::ConstantStruct::getAnon(Values, STy->isPacked());
1039 }
1040 
1041 /// Replace all padding bytes in a given constant with either a pattern byte or
1042 /// 0x00.
1043 static llvm::Constant *constWithPadding(CodeGenModule &CGM, IsPattern isPattern,
1044  llvm::Constant *constant) {
1045  llvm::Type *OrigTy = constant->getType();
1046  if (const auto STy = dyn_cast<llvm::StructType>(OrigTy))
1047  return constStructWithPadding(CGM, isPattern, STy, constant);
1048  if (auto *STy = dyn_cast<llvm::SequentialType>(OrigTy)) {
1050  unsigned Size = STy->getNumElements();
1051  if (!Size)
1052  return constant;
1053  llvm::Type *ElemTy = STy->getElementType();
1054  bool ZeroInitializer = constant->isZeroValue();
1055  llvm::Constant *OpValue, *PaddedOp;
1056  if (ZeroInitializer) {
1057  OpValue = llvm::Constant::getNullValue(ElemTy);
1058  PaddedOp = constWithPadding(CGM, isPattern, OpValue);
1059  }
1060  for (unsigned Op = 0; Op != Size; ++Op) {
1061  if (!ZeroInitializer) {
1062  OpValue = constant->getAggregateElement(Op);
1063  PaddedOp = constWithPadding(CGM, isPattern, OpValue);
1064  }
1065  Values.push_back(PaddedOp);
1066  }
1067  auto *NewElemTy = Values[0]->getType();
1068  if (NewElemTy == ElemTy)
1069  return constant;
1070  if (OrigTy->isArrayTy()) {
1071  auto *ArrayTy = llvm::ArrayType::get(NewElemTy, Size);
1072  return llvm::ConstantArray::get(ArrayTy, Values);
1073  } else {
1074  return llvm::ConstantVector::get(Values);
1075  }
1076  }
1077  return constant;
1078 }
1079 
1081  llvm::Constant *Constant,
1082  CharUnits Align) {
1083  auto FunctionName = [&](const DeclContext *DC) -> std::string {
1084  if (const auto *FD = dyn_cast<FunctionDecl>(DC)) {
1085  if (const auto *CC = dyn_cast<CXXConstructorDecl>(FD))
1086  return CC->getNameAsString();
1087  if (const auto *CD = dyn_cast<CXXDestructorDecl>(FD))
1088  return CD->getNameAsString();
1089  return getMangledName(FD);
1090  } else if (const auto *OM = dyn_cast<ObjCMethodDecl>(DC)) {
1091  return OM->getNameAsString();
1092  } else if (isa<BlockDecl>(DC)) {
1093  return "<block>";
1094  } else if (isa<CapturedDecl>(DC)) {
1095  return "<captured>";
1096  } else {
1097  llvm_unreachable("expected a function or method");
1098  }
1099  };
1100 
1101  // Form a simple per-variable cache of these values in case we find we
1102  // want to reuse them.
1103  llvm::GlobalVariable *&CacheEntry = InitializerConstants[&D];
1104  if (!CacheEntry || CacheEntry->getInitializer() != Constant) {
1105  auto *Ty = Constant->getType();
1106  bool isConstant = true;
1107  llvm::GlobalVariable *InsertBefore = nullptr;
1108  unsigned AS =
1109  getContext().getTargetAddressSpace(getStringLiteralAddressSpace());
1110  std::string Name;
1111  if (D.hasGlobalStorage())
1112  Name = getMangledName(&D).str() + ".const";
1113  else if (const DeclContext *DC = D.getParentFunctionOrMethod())
1114  Name = ("__const." + FunctionName(DC) + "." + D.getName()).str();
1115  else
1116  llvm_unreachable("local variable has no parent function or method");
1117  llvm::GlobalVariable *GV = new llvm::GlobalVariable(
1118  getModule(), Ty, isConstant, llvm::GlobalValue::PrivateLinkage,
1119  Constant, Name, InsertBefore, llvm::GlobalValue::NotThreadLocal, AS);
1120  GV->setAlignment(Align.getQuantity());
1121  GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
1122  CacheEntry = GV;
1123  } else if (CacheEntry->getAlignment() < Align.getQuantity()) {
1124  CacheEntry->setAlignment(Align.getQuantity());
1125  }
1126 
1127  return Address(CacheEntry, Align);
1128 }
1129 
1131  const VarDecl &D,
1133  llvm::Constant *Constant,
1134  CharUnits Align) {
1135  Address SrcPtr = CGM.createUnnamedGlobalFrom(D, Constant, Align);
1136  llvm::Type *BP = llvm::PointerType::getInt8PtrTy(CGM.getLLVMContext(),
1137  SrcPtr.getAddressSpace());
1138  if (SrcPtr.getType() != BP)
1139  SrcPtr = Builder.CreateBitCast(SrcPtr, BP);
1140  return SrcPtr;
1141 }
1142 
1143 static void emitStoresForConstant(CodeGenModule &CGM, const VarDecl &D,
1144  Address Loc, bool isVolatile,
1146  llvm::Constant *constant) {
1147  auto *Ty = constant->getType();
1148  bool canDoSingleStore = Ty->isIntOrIntVectorTy() ||
1149  Ty->isPtrOrPtrVectorTy() || Ty->isFPOrFPVectorTy();
1150  if (canDoSingleStore) {
1151  Builder.CreateStore(constant, Loc, isVolatile);
1152  return;
1153  }
1154 
1155  auto *Int8Ty = llvm::IntegerType::getInt8Ty(CGM.getLLVMContext());
1156  auto *IntPtrTy = CGM.getDataLayout().getIntPtrType(CGM.getLLVMContext());
1157 
1158  uint64_t ConstantSize = CGM.getDataLayout().getTypeAllocSize(Ty);
1159  if (!ConstantSize)
1160  return;
1161  auto *SizeVal = llvm::ConstantInt::get(IntPtrTy, ConstantSize);
1162 
1163  // If the initializer is all or mostly the same, codegen with bzero / memset
1164  // then do a few stores afterward.
1165  if (shouldUseBZeroPlusStoresToInitialize(constant, ConstantSize)) {
1166  Builder.CreateMemSet(Loc, llvm::ConstantInt::get(Int8Ty, 0), SizeVal,
1167  isVolatile);
1168 
1169  bool valueAlreadyCorrect =
1170  constant->isNullValue() || isa<llvm::UndefValue>(constant);
1171  if (!valueAlreadyCorrect) {
1172  Loc = Builder.CreateBitCast(Loc, Ty->getPointerTo(Loc.getAddressSpace()));
1173  emitStoresForInitAfterBZero(CGM, constant, Loc, isVolatile, Builder);
1174  }
1175  return;
1176  }
1177 
1178  // If the initializer is a repeated byte pattern, use memset.
1179  llvm::Value *Pattern = shouldUseMemSetToInitialize(constant, ConstantSize);
1180  if (Pattern) {
1181  uint64_t Value = 0x00;
1182  if (!isa<llvm::UndefValue>(Pattern)) {
1183  const llvm::APInt &AP = cast<llvm::ConstantInt>(Pattern)->getValue();
1184  assert(AP.getBitWidth() <= 8);
1185  Value = AP.getLimitedValue();
1186  }
1187  Builder.CreateMemSet(Loc, llvm::ConstantInt::get(Int8Ty, Value), SizeVal,
1188  isVolatile);
1189  return;
1190  }
1191 
1192  // If the initializer is small, use a handful of stores.
1193  if (shouldSplitConstantStore(CGM, ConstantSize)) {
1194  if (auto *STy = dyn_cast<llvm::StructType>(Ty)) {
1195  // FIXME: handle the case when STy != Loc.getElementType().
1196  if (STy == Loc.getElementType()) {
1197  for (unsigned i = 0; i != constant->getNumOperands(); i++) {
1198  Address EltPtr = Builder.CreateStructGEP(Loc, i);
1200  CGM, D, EltPtr, isVolatile, Builder,
1201  cast<llvm::Constant>(Builder.CreateExtractValue(constant, i)));
1202  }
1203  return;
1204  }
1205  } else if (auto *ATy = dyn_cast<llvm::ArrayType>(Ty)) {
1206  // FIXME: handle the case when ATy != Loc.getElementType().
1207  if (ATy == Loc.getElementType()) {
1208  for (unsigned i = 0; i != ATy->getNumElements(); i++) {
1209  Address EltPtr = Builder.CreateConstArrayGEP(Loc, i);
1211  CGM, D, EltPtr, isVolatile, Builder,
1212  cast<llvm::Constant>(Builder.CreateExtractValue(constant, i)));
1213  }
1214  return;
1215  }
1216  }
1217  }
1218 
1219  // Copy from a global.
1220  Builder.CreateMemCpy(Loc,
1222  CGM, D, Builder, constant, Loc.getAlignment()),
1223  SizeVal, isVolatile);
1224 }
1225 
1226 static void emitStoresForZeroInit(CodeGenModule &CGM, const VarDecl &D,
1227  Address Loc, bool isVolatile,
1228  CGBuilderTy &Builder) {
1229  llvm::Type *ElTy = Loc.getElementType();
1230  llvm::Constant *constant =
1231  constWithPadding(CGM, IsPattern::No, llvm::Constant::getNullValue(ElTy));
1232  emitStoresForConstant(CGM, D, Loc, isVolatile, Builder, constant);
1233 }
1234 
1236  Address Loc, bool isVolatile,
1237  CGBuilderTy &Builder) {
1238  llvm::Type *ElTy = Loc.getElementType();
1239  llvm::Constant *constant = constWithPadding(
1240  CGM, IsPattern::Yes, initializationPatternFor(CGM, ElTy));
1241  assert(!isa<llvm::UndefValue>(constant));
1242  emitStoresForConstant(CGM, D, Loc, isVolatile, Builder, constant);
1243 }
1244 
1245 static bool containsUndef(llvm::Constant *constant) {
1246  auto *Ty = constant->getType();
1247  if (isa<llvm::UndefValue>(constant))
1248  return true;
1249  if (Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy())
1250  for (llvm::Use &Op : constant->operands())
1251  if (containsUndef(cast<llvm::Constant>(Op)))
1252  return true;
1253  return false;
1254 }
1255 
1256 static llvm::Constant *replaceUndef(CodeGenModule &CGM, IsPattern isPattern,
1257  llvm::Constant *constant) {
1258  auto *Ty = constant->getType();
1259  if (isa<llvm::UndefValue>(constant))
1260  return patternOrZeroFor(CGM, isPattern, Ty);
1261  if (!(Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy()))
1262  return constant;
1263  if (!containsUndef(constant))
1264  return constant;
1265  llvm::SmallVector<llvm::Constant *, 8> Values(constant->getNumOperands());
1266  for (unsigned Op = 0, NumOp = constant->getNumOperands(); Op != NumOp; ++Op) {
1267  auto *OpValue = cast<llvm::Constant>(constant->getOperand(Op));
1268  Values[Op] = replaceUndef(CGM, isPattern, OpValue);
1269  }
1270  if (Ty->isStructTy())
1271  return llvm::ConstantStruct::get(cast<llvm::StructType>(Ty), Values);
1272  if (Ty->isArrayTy())
1273  return llvm::ConstantArray::get(cast<llvm::ArrayType>(Ty), Values);
1274  assert(Ty->isVectorTy());
1275  return llvm::ConstantVector::get(Values);
1276 }
1277 
1278 /// EmitAutoVarDecl - Emit code and set up an entry in LocalDeclMap for a
1279 /// variable declaration with auto, register, or no storage class specifier.
1280 /// These turn into simple stack objects, or GlobalValues depending on target.
1282  AutoVarEmission emission = EmitAutoVarAlloca(D);
1283  EmitAutoVarInit(emission);
1284  EmitAutoVarCleanups(emission);
1285 }
1286 
1287 /// Emit a lifetime.begin marker if some criteria are satisfied.
1288 /// \return a pointer to the temporary size Value if a marker was emitted, null
1289 /// otherwise
1291  llvm::Value *Addr) {
1292  if (!ShouldEmitLifetimeMarkers)
1293  return nullptr;
1294 
1295  assert(Addr->getType()->getPointerAddressSpace() ==
1296  CGM.getDataLayout().getAllocaAddrSpace() &&
1297  "Pointer should be in alloca address space");
1298  llvm::Value *SizeV = llvm::ConstantInt::get(Int64Ty, Size);
1299  Addr = Builder.CreateBitCast(Addr, AllocaInt8PtrTy);
1300  llvm::CallInst *C =
1301  Builder.CreateCall(CGM.getLLVMLifetimeStartFn(), {SizeV, Addr});
1302  C->setDoesNotThrow();
1303  return SizeV;
1304 }
1305 
1307  assert(Addr->getType()->getPointerAddressSpace() ==
1308  CGM.getDataLayout().getAllocaAddrSpace() &&
1309  "Pointer should be in alloca address space");
1310  Addr = Builder.CreateBitCast(Addr, AllocaInt8PtrTy);
1311  llvm::CallInst *C =
1312  Builder.CreateCall(CGM.getLLVMLifetimeEndFn(), {Size, Addr});
1313  C->setDoesNotThrow();
1314 }
1315 
1317  CGDebugInfo *DI, const VarDecl &D, bool EmitDebugInfo) {
1318  // For each dimension stores its QualType and corresponding
1319  // size-expression Value.
1321  SmallVector<IdentifierInfo *, 4> VLAExprNames;
1322 
1323  // Break down the array into individual dimensions.
1324  QualType Type1D = D.getType();
1325  while (getContext().getAsVariableArrayType(Type1D)) {
1326  auto VlaSize = getVLAElements1D(Type1D);
1327  if (auto *C = dyn_cast<llvm::ConstantInt>(VlaSize.NumElts))
1328  Dimensions.emplace_back(C, Type1D.getUnqualifiedType());
1329  else {
1330  // Generate a locally unique name for the size expression.
1331  Twine Name = Twine("__vla_expr") + Twine(VLAExprCounter++);
1332  SmallString<12> Buffer;
1333  StringRef NameRef = Name.toStringRef(Buffer);
1334  auto &Ident = getContext().Idents.getOwn(NameRef);
1335  VLAExprNames.push_back(&Ident);
1336  auto SizeExprAddr =
1337  CreateDefaultAlignTempAlloca(VlaSize.NumElts->getType(), NameRef);
1338  Builder.CreateStore(VlaSize.NumElts, SizeExprAddr);
1339  Dimensions.emplace_back(SizeExprAddr.getPointer(),
1340  Type1D.getUnqualifiedType());
1341  }
1342  Type1D = VlaSize.Type;
1343  }
1344 
1345  if (!EmitDebugInfo)
1346  return;
1347 
1348  // Register each dimension's size-expression with a DILocalVariable,
1349  // so that it can be used by CGDebugInfo when instantiating a DISubrange
1350  // to describe this array.
1351  unsigned NameIdx = 0;
1352  for (auto &VlaSize : Dimensions) {
1353  llvm::Metadata *MD;
1354  if (auto *C = dyn_cast<llvm::ConstantInt>(VlaSize.NumElts))
1355  MD = llvm::ConstantAsMetadata::get(C);
1356  else {
1357  // Create an artificial VarDecl to generate debug info for.
1358  IdentifierInfo *NameIdent = VLAExprNames[NameIdx++];
1359  auto VlaExprTy = VlaSize.NumElts->getType()->getPointerElementType();
1360  auto QT = getContext().getIntTypeForBitwidth(
1361  VlaExprTy->getScalarSizeInBits(), false);
1362  auto *ArtificialDecl = VarDecl::Create(
1363  getContext(), const_cast<DeclContext *>(D.getDeclContext()),
1364  D.getLocation(), D.getLocation(), NameIdent, QT,
1366  ArtificialDecl->setImplicit();
1367 
1368  MD = DI->EmitDeclareOfAutoVariable(ArtificialDecl, VlaSize.NumElts,
1369  Builder);
1370  }
1371  assert(MD && "No Size expression debug node created");
1372  DI->registerVLASizeExpression(VlaSize.Type, MD);
1373  }
1374 }
1375 
1376 /// EmitAutoVarAlloca - Emit the alloca and debug information for a
1377 /// local variable. Does not emit initialization or destruction.
1380  QualType Ty = D.getType();
1381  assert(
1382  Ty.getAddressSpace() == LangAS::Default ||
1383  (Ty.getAddressSpace() == LangAS::opencl_private && getLangOpts().OpenCL));
1384 
1385  AutoVarEmission emission(D);
1386 
1387  bool isEscapingByRef = D.isEscapingByref();
1388  emission.IsEscapingByRef = isEscapingByRef;
1389 
1390  CharUnits alignment = getContext().getDeclAlign(&D);
1391 
1392  // If the type is variably-modified, emit all the VLA sizes for it.
1393  if (Ty->isVariablyModifiedType())
1395 
1396  auto *DI = getDebugInfo();
1397  bool EmitDebugInfo = DI && CGM.getCodeGenOpts().getDebugInfo() >=
1399 
1400  Address address = Address::invalid();
1401  Address AllocaAddr = Address::invalid();
1402  Address OpenMPLocalAddr =
1403  getLangOpts().OpenMP
1404  ? CGM.getOpenMPRuntime().getAddressOfLocalVariable(*this, &D)
1405  : Address::invalid();
1406  bool NRVO = getLangOpts().ElideConstructors && D.isNRVOVariable();
1407 
1408  if (getLangOpts().OpenMP && OpenMPLocalAddr.isValid()) {
1409  address = OpenMPLocalAddr;
1410  } else if (Ty->isConstantSizeType()) {
1411  // If this value is an array or struct with a statically determinable
1412  // constant initializer, there are optimizations we can do.
1413  //
1414  // TODO: We should constant-evaluate the initializer of any variable,
1415  // as long as it is initialized by a constant expression. Currently,
1416  // isConstantInitializer produces wrong answers for structs with
1417  // reference or bitfield members, and a few other cases, and checking
1418  // for POD-ness protects us from some of these.
1419  if (D.getInit() && (Ty->isArrayType() || Ty->isRecordType()) &&
1420  (D.isConstexpr() ||
1421  ((Ty.isPODType(getContext()) ||
1423  D.getInit()->isConstantInitializer(getContext(), false)))) {
1424 
1425  // If the variable's a const type, and it's neither an NRVO
1426  // candidate nor a __block variable and has no mutable members,
1427  // emit it as a global instead.
1428  // Exception is if a variable is located in non-constant address space
1429  // in OpenCL.
1430  if ((!getLangOpts().OpenCL ||
1432  (CGM.getCodeGenOpts().MergeAllConstants && !NRVO &&
1433  !isEscapingByRef && CGM.isTypeConstant(Ty, true))) {
1435 
1436  // Signal this condition to later callbacks.
1437  emission.Addr = Address::invalid();
1438  assert(emission.wasEmittedAsGlobal());
1439  return emission;
1440  }
1441 
1442  // Otherwise, tell the initialization code that we're in this case.
1443  emission.IsConstantAggregate = true;
1444  }
1445 
1446  // A normal fixed sized variable becomes an alloca in the entry block,
1447  // unless:
1448  // - it's an NRVO variable.
1449  // - we are compiling OpenMP and it's an OpenMP local variable.
1450  if (NRVO) {
1451  // The named return value optimization: allocate this variable in the
1452  // return slot, so that we can elide the copy when returning this
1453  // variable (C++0x [class.copy]p34).
1454  address = ReturnValue;
1455 
1456  if (const RecordType *RecordTy = Ty->getAs<RecordType>()) {
1457  const auto *RD = RecordTy->getDecl();
1458  const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD);
1459  if ((CXXRD && !CXXRD->hasTrivialDestructor()) ||
1460  RD->isNonTrivialToPrimitiveDestroy()) {
1461  // Create a flag that is used to indicate when the NRVO was applied
1462  // to this variable. Set it to zero to indicate that NRVO was not
1463  // applied.
1464  llvm::Value *Zero = Builder.getFalse();
1465  Address NRVOFlag =
1466  CreateTempAlloca(Zero->getType(), CharUnits::One(), "nrvo");
1468  Builder.CreateStore(Zero, NRVOFlag);
1469 
1470  // Record the NRVO flag for this variable.
1471  NRVOFlags[&D] = NRVOFlag.getPointer();
1472  emission.NRVOFlag = NRVOFlag.getPointer();
1473  }
1474  }
1475  } else {
1476  CharUnits allocaAlignment;
1477  llvm::Type *allocaTy;
1478  if (isEscapingByRef) {
1479  auto &byrefInfo = getBlockByrefInfo(&D);
1480  allocaTy = byrefInfo.Type;
1481  allocaAlignment = byrefInfo.ByrefAlignment;
1482  } else {
1483  allocaTy = ConvertTypeForMem(Ty);
1484  allocaAlignment = alignment;
1485  }
1486 
1487  // Create the alloca. Note that we set the name separately from
1488  // building the instruction so that it's there even in no-asserts
1489  // builds.
1490  address = CreateTempAlloca(allocaTy, allocaAlignment, D.getName(),
1491  /*ArraySize=*/nullptr, &AllocaAddr);
1492 
1493  // Don't emit lifetime markers for MSVC catch parameters. The lifetime of
1494  // the catch parameter starts in the catchpad instruction, and we can't
1495  // insert code in those basic blocks.
1496  bool IsMSCatchParam =
1498 
1499  // Emit a lifetime intrinsic if meaningful. There's no point in doing this
1500  // if we don't have a valid insertion point (?).
1501  if (HaveInsertPoint() && !IsMSCatchParam) {
1502  // If there's a jump into the lifetime of this variable, its lifetime
1503  // gets broken up into several regions in IR, which requires more work
1504  // to handle correctly. For now, just omit the intrinsics; this is a
1505  // rare case, and it's better to just be conservatively correct.
1506  // PR28267.
1507  //
1508  // We have to do this in all language modes if there's a jump past the
1509  // declaration. We also have to do it in C if there's a jump to an
1510  // earlier point in the current block because non-VLA lifetimes begin as
1511  // soon as the containing block is entered, not when its variables
1512  // actually come into scope; suppressing the lifetime annotations
1513  // completely in this case is unnecessarily pessimistic, but again, this
1514  // is rare.
1515  if (!Bypasses.IsBypassed(&D) &&
1516  !(!getLangOpts().CPlusPlus && hasLabelBeenSeenInCurrentScope())) {
1517  uint64_t size = CGM.getDataLayout().getTypeAllocSize(allocaTy);
1518  emission.SizeForLifetimeMarkers =
1519  EmitLifetimeStart(size, AllocaAddr.getPointer());
1520  }
1521  } else {
1522  assert(!emission.useLifetimeMarkers());
1523  }
1524  }
1525  } else {
1527 
1528  if (!DidCallStackSave) {
1529  // Save the stack.
1530  Address Stack =
1531  CreateTempAlloca(Int8PtrTy, getPointerAlign(), "saved_stack");
1532 
1533  llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave);
1534  llvm::Value *V = Builder.CreateCall(F);
1535  Builder.CreateStore(V, Stack);
1536 
1537  DidCallStackSave = true;
1538 
1539  // Push a cleanup block and restore the stack there.
1540  // FIXME: in general circumstances, this should be an EH cleanup.
1542  }
1543 
1544  auto VlaSize = getVLASize(Ty);
1545  llvm::Type *llvmTy = ConvertTypeForMem(VlaSize.Type);
1546 
1547  // Allocate memory for the array.
1548  address = CreateTempAlloca(llvmTy, alignment, "vla", VlaSize.NumElts,
1549  &AllocaAddr);
1550 
1551  // If we have debug info enabled, properly describe the VLA dimensions for
1552  // this type by registering the vla size expression for each of the
1553  // dimensions.
1554  EmitAndRegisterVariableArrayDimensions(DI, D, EmitDebugInfo);
1555  }
1556 
1557  setAddrOfLocalVar(&D, address);
1558  emission.Addr = address;
1559  emission.AllocaAddr = AllocaAddr;
1560 
1561  // Emit debug info for local var declaration.
1562  if (EmitDebugInfo && HaveInsertPoint()) {
1563  Address DebugAddr = address;
1564  bool UsePointerValue = NRVO && ReturnValuePointer.isValid();
1565  DI->setLocation(D.getLocation());
1566 
1567  // If NRVO, use a pointer to the return address.
1568  if (UsePointerValue)
1569  DebugAddr = ReturnValuePointer;
1570 
1571  (void)DI->EmitDeclareOfAutoVariable(&D, DebugAddr.getPointer(), Builder,
1572  UsePointerValue);
1573  }
1574 
1575  if (D.hasAttr<AnnotateAttr>() && HaveInsertPoint())
1576  EmitVarAnnotations(&D, address.getPointer());
1577 
1578  // Make sure we call @llvm.lifetime.end.
1579  if (emission.useLifetimeMarkers())
1581  emission.getOriginalAllocatedAddress(),
1582  emission.getSizeForLifetimeMarkers());
1583 
1584  return emission;
1585 }
1586 
1587 static bool isCapturedBy(const VarDecl &, const Expr *);
1588 
1589 /// Determines whether the given __block variable is potentially
1590 /// captured by the given statement.
1591 static bool isCapturedBy(const VarDecl &Var, const Stmt *S) {
1592  if (const Expr *E = dyn_cast<Expr>(S))
1593  return isCapturedBy(Var, E);
1594  for (const Stmt *SubStmt : S->children())
1595  if (isCapturedBy(Var, SubStmt))
1596  return true;
1597  return false;
1598 }
1599 
1600 /// Determines whether the given __block variable is potentially
1601 /// captured by the given expression.
1602 static bool isCapturedBy(const VarDecl &Var, const Expr *E) {
1603  // Skip the most common kinds of expressions that make
1604  // hierarchy-walking expensive.
1605  E = E->IgnoreParenCasts();
1606 
1607  if (const BlockExpr *BE = dyn_cast<BlockExpr>(E)) {
1608  const BlockDecl *Block = BE->getBlockDecl();
1609  for (const auto &I : Block->captures()) {
1610  if (I.getVariable() == &Var)
1611  return true;
1612  }
1613 
1614  // No need to walk into the subexpressions.
1615  return false;
1616  }
1617 
1618  if (const StmtExpr *SE = dyn_cast<StmtExpr>(E)) {
1619  const CompoundStmt *CS = SE->getSubStmt();
1620  for (const auto *BI : CS->body())
1621  if (const auto *BIE = dyn_cast<Expr>(BI)) {
1622  if (isCapturedBy(Var, BIE))
1623  return true;
1624  }
1625  else if (const auto *DS = dyn_cast<DeclStmt>(BI)) {
1626  // special case declarations
1627  for (const auto *I : DS->decls()) {
1628  if (const auto *VD = dyn_cast<VarDecl>((I))) {
1629  const Expr *Init = VD->getInit();
1630  if (Init && isCapturedBy(Var, Init))
1631  return true;
1632  }
1633  }
1634  }
1635  else
1636  // FIXME. Make safe assumption assuming arbitrary statements cause capturing.
1637  // Later, provide code to poke into statements for capture analysis.
1638  return true;
1639  return false;
1640  }
1641 
1642  for (const Stmt *SubStmt : E->children())
1643  if (isCapturedBy(Var, SubStmt))
1644  return true;
1645 
1646  return false;
1647 }
1648 
1649 /// Determine whether the given initializer is trivial in the sense
1650 /// that it requires no code to be generated.
1652  if (!Init)
1653  return true;
1654 
1655  if (const CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init))
1656  if (CXXConstructorDecl *Constructor = Construct->getConstructor())
1657  if (Constructor->isTrivial() &&
1658  Constructor->isDefaultConstructor() &&
1659  !Construct->requiresZeroInitialization())
1660  return true;
1661 
1662  return false;
1663 }
1664 
1666  assert(emission.Variable && "emission was not valid!");
1667 
1668  // If this was emitted as a global constant, we're done.
1669  if (emission.wasEmittedAsGlobal()) return;
1670 
1671  const VarDecl &D = *emission.Variable;
1673  QualType type = D.getType();
1674 
1675  bool isVolatile = type.isVolatileQualified();
1676 
1677  // If this local has an initializer, emit it now.
1678  const Expr *Init = D.getInit();
1679 
1680  // If we are at an unreachable point, we don't need to emit the initializer
1681  // unless it contains a label.
1682  if (!HaveInsertPoint()) {
1683  if (!Init || !ContainsLabel(Init)) return;
1685  }
1686 
1687  // Initialize the structure of a __block variable.
1688  if (emission.IsEscapingByRef)
1689  emitByrefStructureInit(emission);
1690 
1691  // Initialize the variable here if it doesn't have a initializer and it is a
1692  // C struct that is non-trivial to initialize or an array containing such a
1693  // struct.
1694  if (!Init &&
1695  type.isNonTrivialToPrimitiveDefaultInitialize() ==
1697  LValue Dst = MakeAddrLValue(emission.getAllocatedAddress(), type);
1698  if (emission.IsEscapingByRef)
1699  drillIntoBlockVariable(*this, Dst, &D);
1701  return;
1702  }
1703 
1704  // Check whether this is a byref variable that's potentially
1705  // captured and moved by its own initializer. If so, we'll need to
1706  // emit the initializer first, then copy into the variable.
1707  bool capturedByInit =
1708  Init && emission.IsEscapingByRef && isCapturedBy(D, Init);
1709 
1710  bool locIsByrefHeader = !capturedByInit;
1711  const Address Loc =
1712  locIsByrefHeader ? emission.getObjectAddress(*this) : emission.Addr;
1713 
1714  // Note: constexpr already initializes everything correctly.
1715  LangOptions::TrivialAutoVarInitKind trivialAutoVarInit =
1716  (D.isConstexpr()
1718  : (D.getAttr<UninitializedAttr>()
1720  : getContext().getLangOpts().getTrivialAutoVarInit()));
1721 
1722  auto initializeWhatIsTechnicallyUninitialized = [&](Address Loc) {
1723  if (trivialAutoVarInit ==
1725  return;
1726 
1727  // Only initialize a __block's storage: we always initialize the header.
1728  if (emission.IsEscapingByRef && !locIsByrefHeader)
1729  Loc = emitBlockByrefAddress(Loc, &D, /*follow=*/false);
1730 
1731  CharUnits Size = getContext().getTypeSizeInChars(type);
1732  if (!Size.isZero()) {
1733  switch (trivialAutoVarInit) {
1735  llvm_unreachable("Uninitialized handled above");
1737  emitStoresForZeroInit(CGM, D, Loc, isVolatile, Builder);
1738  break;
1740  emitStoresForPatternInit(CGM, D, Loc, isVolatile, Builder);
1741  break;
1742  }
1743  return;
1744  }
1745 
1746  // VLAs look zero-sized to getTypeInfo. We can't emit constant stores to
1747  // them, so emit a memcpy with the VLA size to initialize each element.
1748  // Technically zero-sized or negative-sized VLAs are undefined, and UBSan
1749  // will catch that code, but there exists code which generates zero-sized
1750  // VLAs. Be nice and initialize whatever they requested.
1751  const auto *VlaType = getContext().getAsVariableArrayType(type);
1752  if (!VlaType)
1753  return;
1754  auto VlaSize = getVLASize(VlaType);
1755  auto SizeVal = VlaSize.NumElts;
1756  CharUnits EltSize = getContext().getTypeSizeInChars(VlaSize.Type);
1757  switch (trivialAutoVarInit) {
1759  llvm_unreachable("Uninitialized handled above");
1760 
1762  if (!EltSize.isOne())
1763  SizeVal = Builder.CreateNUWMul(SizeVal, CGM.getSize(EltSize));
1764  Builder.CreateMemSet(Loc, llvm::ConstantInt::get(Int8Ty, 0), SizeVal,
1765  isVolatile);
1766  break;
1767 
1769  llvm::Type *ElTy = Loc.getElementType();
1770  llvm::Constant *Constant = constWithPadding(
1771  CGM, IsPattern::Yes, initializationPatternFor(CGM, ElTy));
1772  CharUnits ConstantAlign = getContext().getTypeAlignInChars(VlaSize.Type);
1773  llvm::BasicBlock *SetupBB = createBasicBlock("vla-setup.loop");
1774  llvm::BasicBlock *LoopBB = createBasicBlock("vla-init.loop");
1775  llvm::BasicBlock *ContBB = createBasicBlock("vla-init.cont");
1776  llvm::Value *IsZeroSizedVLA = Builder.CreateICmpEQ(
1777  SizeVal, llvm::ConstantInt::get(SizeVal->getType(), 0),
1778  "vla.iszerosized");
1779  Builder.CreateCondBr(IsZeroSizedVLA, ContBB, SetupBB);
1780  EmitBlock(SetupBB);
1781  if (!EltSize.isOne())
1782  SizeVal = Builder.CreateNUWMul(SizeVal, CGM.getSize(EltSize));
1783  llvm::Value *BaseSizeInChars =
1784  llvm::ConstantInt::get(IntPtrTy, EltSize.getQuantity());
1785  Address Begin = Builder.CreateElementBitCast(Loc, Int8Ty, "vla.begin");
1786  llvm::Value *End =
1787  Builder.CreateInBoundsGEP(Begin.getPointer(), SizeVal, "vla.end");
1788  llvm::BasicBlock *OriginBB = Builder.GetInsertBlock();
1789  EmitBlock(LoopBB);
1790  llvm::PHINode *Cur = Builder.CreatePHI(Begin.getType(), 2, "vla.cur");
1791  Cur->addIncoming(Begin.getPointer(), OriginBB);
1792  CharUnits CurAlign = Loc.getAlignment().alignmentOfArrayElement(EltSize);
1793  Builder.CreateMemCpy(Address(Cur, CurAlign),
1795  CGM, D, Builder, Constant, ConstantAlign),
1796  BaseSizeInChars, isVolatile);
1797  llvm::Value *Next =
1798  Builder.CreateInBoundsGEP(Int8Ty, Cur, BaseSizeInChars, "vla.next");
1799  llvm::Value *Done = Builder.CreateICmpEQ(Next, End, "vla-init.isdone");
1800  Builder.CreateCondBr(Done, ContBB, LoopBB);
1801  Cur->addIncoming(Next, LoopBB);
1802  EmitBlock(ContBB);
1803  } break;
1804  }
1805  };
1806 
1807  if (isTrivialInitializer(Init)) {
1808  initializeWhatIsTechnicallyUninitialized(Loc);
1809  return;
1810  }
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  isVolatile, 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, 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.
1991  if (QualType::DestructionKind dtorKind = D.getType().isDestructedType())
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->getAs<RecordType>()->getDecl()->isParamDestroyedInCallee()) {
2401  if (QualType::DestructionKind DtorKind = Ty.isDestructedType()) {
2402  assert((DtorKind == QualType::DK_cxx_destructor ||
2403  DtorKind == QualType::DK_nontrivial_c_struct) &&
2404  "unexpected destructor type");
2405  pushDestroy(DtorKind, DeclPtr, Ty);
2406  CalleeDestructedParamCleanups[cast<ParmVarDecl>(&D)] =
2408  }
2409  }
2410  } else {
2411  // Check if the parameter address is controlled by OpenMP runtime.
2412  Address OpenMPLocalAddr =
2413  getLangOpts().OpenMP
2414  ? CGM.getOpenMPRuntime().getAddressOfLocalVariable(*this, &D)
2415  : Address::invalid();
2416  if (getLangOpts().OpenMP && OpenMPLocalAddr.isValid()) {
2417  DeclPtr = OpenMPLocalAddr;
2418  } else {
2419  // Otherwise, create a temporary to hold the value.
2420  DeclPtr = CreateMemTemp(Ty, getContext().getDeclAlign(&D),
2421  D.getName() + ".addr");
2422  }
2423  DoStore = true;
2424  }
2425 
2426  llvm::Value *ArgVal = (DoStore ? Arg.getDirectValue() : nullptr);
2427 
2428  LValue lv = MakeAddrLValue(DeclPtr, Ty);
2429  if (IsScalar) {
2430  Qualifiers qs = Ty.getQualifiers();
2431  if (Qualifiers::ObjCLifetime lt = qs.getObjCLifetime()) {
2432  // We honor __attribute__((ns_consumed)) for types with lifetime.
2433  // For __strong, it's handled by just skipping the initial retain;
2434  // otherwise we have to balance out the initial +1 with an extra
2435  // cleanup to do the release at the end of the function.
2436  bool isConsumed = D.hasAttr<NSConsumedAttr>();
2437 
2438  // If a parameter is pseudo-strong then we can omit the implicit retain.
2439  if (D.isARCPseudoStrong()) {
2440  assert(lt == Qualifiers::OCL_Strong &&
2441  "pseudo-strong variable isn't strong?");
2442  assert(qs.hasConst() && "pseudo-strong variable should be const!");
2444  }
2445 
2446  // Load objects passed indirectly.
2447  if (Arg.isIndirect() && !ArgVal)
2448  ArgVal = Builder.CreateLoad(DeclPtr);
2449 
2450  if (lt == Qualifiers::OCL_Strong) {
2451  if (!isConsumed) {
2452  if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
2453  // use objc_storeStrong(&dest, value) for retaining the
2454  // object. But first, store a null into 'dest' because
2455  // objc_storeStrong attempts to release its old value.
2456  llvm::Value *Null = CGM.EmitNullConstant(D.getType());
2457  EmitStoreOfScalar(Null, lv, /* isInitialization */ true);
2458  EmitARCStoreStrongCall(lv.getAddress(), ArgVal, true);
2459  DoStore = false;
2460  }
2461  else
2462  // Don't use objc_retainBlock for block pointers, because we
2463  // don't want to Block_copy something just because we got it
2464  // as a parameter.
2465  ArgVal = EmitARCRetainNonBlock(ArgVal);
2466  }
2467  } else {
2468  // Push the cleanup for a consumed parameter.
2469  if (isConsumed) {
2470  ARCPreciseLifetime_t precise = (D.hasAttr<ObjCPreciseLifetimeAttr>()
2472  EHStack.pushCleanup<ConsumeARCParameter>(getARCCleanupKind(), ArgVal,
2473  precise);
2474  }
2475 
2476  if (lt == Qualifiers::OCL_Weak) {
2477  EmitARCInitWeak(DeclPtr, ArgVal);
2478  DoStore = false; // The weak init is a store, no need to do two.
2479  }
2480  }
2481 
2482  // Enter the cleanup scope.
2483  EmitAutoVarWithLifetime(*this, D, DeclPtr, lt);
2484  }
2485  }
2486 
2487  // Store the initial value into the alloca.
2488  if (DoStore)
2489  EmitStoreOfScalar(ArgVal, lv, /* isInitialization */ true);
2490 
2491  setAddrOfLocalVar(&D, DeclPtr);
2492 
2493  // Emit debug info for param declaration.
2494  if (CGDebugInfo *DI = getDebugInfo()) {
2495  if (CGM.getCodeGenOpts().getDebugInfo() >=
2497  DI->EmitDeclareOfArgVariable(&D, DeclPtr.getPointer(), ArgNo, Builder);
2498  }
2499  }
2500 
2501  if (D.hasAttr<AnnotateAttr>())
2502  EmitVarAnnotations(&D, DeclPtr.getPointer());
2503 
2504  // We can only check return value nullability if all arguments to the
2505  // function satisfy their nullability preconditions. This makes it necessary
2506  // to emit null checks for args in the function body itself.
2507  if (requiresReturnValueNullabilityCheck()) {
2508  auto Nullability = Ty->getNullability(getContext());
2510  SanitizerScope SanScope(this);
2511  RetValNullabilityPrecondition =
2512  Builder.CreateAnd(RetValNullabilityPrecondition,
2513  Builder.CreateIsNotNull(Arg.getAnyValue()));
2514  }
2515  }
2516 }
2517 
2519  CodeGenFunction *CGF) {
2520  if (!LangOpts.OpenMP || (!LangOpts.EmitAllDecls && !D->isUsed()))
2521  return;
2522  getOpenMPRuntime().emitUserDefinedReduction(CGF, D);
2523 }
2524 
2526  CodeGenFunction *CGF) {
2527  if (!LangOpts.OpenMP || (!LangOpts.EmitAllDecls && !D->isUsed()))
2528  return;
2529  // FIXME: need to implement mapper code generation
2530 }
2531 
2533  getOpenMPRuntime().checkArchForUnifiedAddressing(D);
2534 }
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:863
bool mightBeUsableInConstantExpressions(ASTContext &C) const
Determine whether this variable&#39;s value might be usable in a constant expression, according to the re...
Definition: Decl.cpp:2248
void setImplicit(bool I=true)
Definition: DeclBase.h:559
Represents a function declaration or definition.
Definition: Decl.h:1748
void EmitStaticVarDecl(const VarDecl &D, llvm::GlobalValue::LinkageTypes Linkage)
Definition: CGDecl.cpp:382
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:2525
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:2132
bool isArrayType() const
Definition: Type.h:6407
llvm::Type * ConvertTypeForMem(QualType T)
const CodeGenOptions & getCodeGenOpts() const
void EmitVarDecl(const VarDecl &D)
EmitVarDecl - Emit a local variable declaration.
Definition: CGDecl.cpp:162
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:138
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:2784
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:6431
llvm::Function * getLLVMLifetimeEndFn()
Lazily declare the .lifetime.end intrinsic.
Definition: CGDecl.cpp:2316
IsPattern
Definition: CGDecl.cpp:988
__v8hi zero
Definition: emmintrin.h:1397
bool hasLabelBeenSeenInCurrentScope() const
Return true if a label was seen in the current scope.
void emitAutoVarTypeCleanup(const AutoVarEmission &emission, QualType::DestructionKind dtorKind)
Enter a destroy cleanup for the given local variable.
Definition: CGDecl.cpp: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:2998
void EmitAutoVarDecl(const VarDecl &D)
EmitAutoVarDecl - Emit an auto variable declaration.
Definition: CGDecl.cpp:1281
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:901
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition: Type.h:2829
Represents a call to a C++ constructor.
Definition: ExprCXX.h:1326
bool isZero() const
isZero - Test whether the quantity equals zero.
Definition: CharUnits.h:115
stable_iterator stable_begin() const
Create a stable reference to the top of the EH stack.
Definition: EHScopeStack.h:378
void EmitARCCopyWeak(Address dst, Address src)
void @objc_copyWeak(i8** dest, i8** src) Disregards the current value in dest.
Definition: CGObjC.cpp:2461
constexpr XRayInstrMask Function
Definition: XRayInstr.h:38
llvm::Value * EmitARCRetainNonBlock(llvm::Value *value)
Retain the given object, with normal retain semantics.
Definition: CGObjC.cpp:2122
llvm::IntegerType * Int8Ty
i8, i16, i32, and i64
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2566
const DeclContext * getParentFunctionOrMethod() const
If this decl is defined inside a function/method/block it returns the corresponding DeclContext...
Definition: DeclBase.cpp:253
The type is a struct containing a field whose type is not PCK_Trivial.
Definition: Type.h:1093
static bool shouldUseBZeroPlusStoresToInitialize(llvm::Constant *Init, uint64_t GlobalSize)
Decide whether we should use bzero plus some stores to initialize a local variable instead of using a...
Definition: CGDecl.cpp:945
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:1043
static llvm::Value * shouldUseMemSetToInitialize(llvm::Constant *Init, uint64_t GlobalSize)
Decide whether we should use memset to initialize a local variable instead of using a memcpy from a c...
Definition: CGDecl.cpp:966
Represents a variable declaration or definition.
Definition: Decl.h:812
Address getObjectAddress(CodeGenFunction &CGF) const
Returns the address of the object within this declaration.
RAII object to set/unset CodeGenFunction::IsSanitizerScope.
const T * getAs() const
Member-template getAs<specific type>&#39;.
Definition: Type.h:6818
AggValueSlot::Overlap_t getOverlapForFieldInit(const FieldDecl *FD)
Determine whether a field initialization may overlap some other object.
Definition: CGExprAgg.cpp:1850
LangAS
Defines the address space values used by the address space qualifier of QualType. ...
Definition: AddressSpaces.h:25
This class gathers all debug information during compilation and is responsible for emitting to llvm g...
Definition: CGDebugInfo.h:54
void EmitVariablyModifiedType(QualType Ty)
EmitVLASize - Capture all the sizes for the VLA expressions in the given variably-modified type and s...
llvm::Value * getPointer() const
Definition: Address.h:37
llvm::Type * ConvertTypeForMem(QualType T)
ConvertTypeForMem - Convert type T into a llvm::Type.
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have...
Definition: Linkage.h:23
long i
Definition: xmmintrin.h:1456
unsigned getAddressSpace() const
Return the address space that this address resides in.
Definition: Address.h:56
The collection of all-type qualifiers we support.
Definition: Type.h:137
void add(RValue rvalue, QualType type)
Definition: CGCall.h:287
bool isARCPseudoStrong() const
Determine whether this variable is an ARC pseudo-__strong variable.
Definition: Decl.h:1364
const TargetInfo & getTarget() const
One of these records is kept for each identifier that is lexed.
TargetCXXABI getCXXABI() const
Get the C++ ABI currently in use.
Definition: TargetInfo.h:1052
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:2819
VlaSizePair getVLAElements1D(const VariableArrayType *vla)
Return the number of elements for a single dimension for the given array type.
Address getAddress() const
Definition: CGValue.h:326
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
llvm::Constant * tryEmitAbstractForInitializer(const VarDecl &D)
Try to emit the initializer of the given declaration as an abstract constant.
void EmitExprAsInit(const Expr *init, const ValueDecl *D, LValue lvalue, bool capturedByInit)
EmitExprAsInit - Emits the code necessary to initialize a location in memory with the given initializ...
Definition: CGDecl.cpp: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:1003
RValue EmitReferenceBindingToExpr(const Expr *E)
Emits a reference binding to the passed in expression.
Definition: CGExpr.cpp:592
FullExpr - Represents a "full-expression" node.
Definition: Expr.h:920
static llvm::Constant * patternOrZeroFor(CodeGenModule &CGM, IsPattern isPattern, llvm::Type *Ty)
Generate a constant filled with either a pattern or zeroes.
Definition: CGDecl.cpp:991
Qualifiers::ObjCLifetime getObjCLifetime() const
Definition: CGValue.h:265
bool isReferenceType() const
Definition: Type.h:6363
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:1245
void reportGlobalToASan(llvm::GlobalVariable *GV, const VarDecl &D, bool IsDynInit=false)
Address getAllocatedAddress() const
Returns the raw, allocated address, which is not necessarily the address of the object itself...
CleanupKind getCleanupKind(QualType::DestructionKind kind)
IdentifierTable & Idents
Definition: ASTContext.h:569
void EmitStoreOfScalar(llvm::Value *Value, Address Addr, bool Volatile, QualType Ty, AlignmentSource Source=AlignmentSource::Type, bool isInit=false, bool isNontemporal=false)
EmitStoreOfScalar - Store a scalar value to an address, taking care to appropriately convert from the...
void setNonGC(bool Value)
Definition: CGValue.h:276
llvm::Value * EmitARCStoreStrongCall(Address addr, llvm::Value *value, bool resultIgnored)
Store into a strong object.
Definition: CGObjC.cpp:2276
void pushRegularPartialArrayCleanup(llvm::Value *arrayBegin, llvm::Value *arrayEnd, QualType elementType, CharUnits elementAlignment, Destroyer *destroyer)
pushRegularPartialArrayCleanup - Push an EH cleanup to destroy already-constructed elements of the gi...
Definition: CGDecl.cpp:2295
static bool hasScalarEvaluationKind(QualType T)
static void drillIntoBlockVariable(CodeGenFunction &CGF, LValue &lvalue, const VarDecl *var)
Definition: CGDecl.cpp:717
Base object ctor.
Definition: ABI.h:26
void defaultInitNonTrivialCStructVar(LValue Dst)
Address CreateElementBitCast(Address Addr, llvm::Type *Ty, const llvm::Twine &Name="")
Cast the element type of the given address to a different type, preserving information like the align...
Definition: CGBuilder.h:156
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:37
void EmitOMPRequiresDecl(const OMPRequiresDecl *D)
Emit a code for requires directive.
Definition: CGDecl.cpp:2532
void EmitOMPDeclareReduction(const OMPDeclareReductionDecl *D, CodeGenFunction *CGF=nullptr)
Emit a code for declare reduction construct.
Definition: CGDecl.cpp:2518
bool isOne() const
isOne - Test whether the quantity equals one.
Definition: CharUnits.h:118
An x-value expression is a reference to an object with independent storage but which can be "moved"...
Definition: Specifiers.h:133
ExprValueKind getValueKind() const
getValueKind - The value kind that this expression produces.
Definition: Expr.h:414
void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl)
Emit information about a global variable.
CharUnits getAlignment() const
Return the alignment of this pointer.
Definition: Address.h:66
void setStaticLocalDeclAddress(const VarDecl *D, llvm::Constant *C)
child_range children()
Definition: Stmt.cpp:212
bool isVolatileQualified() const
Determine whether this type is volatile-qualified.
Definition: Type.h:6197
static void emitStoresForConstant(CodeGenModule &CGM, const VarDecl &D, Address Loc, bool isVolatile, CGBuilderTy &Builder, llvm::Constant *constant)
Definition: CGDecl.cpp:1143
bool needsEHCleanup(QualType::DestructionKind kind)
Determines whether an EH cleanup is required to destroy a type with the given destruction kind...
const_arg_iterator arg_begin() const
llvm::Value * EmitARCUnsafeUnretainedScalarExpr(const Expr *expr)
EmitARCUnsafeUnretainedScalarExpr - Semantically equivalent to immediately releasing the resut of Emi...
Definition: CGObjC.cpp:3367
llvm::CallInst * CreateMemCpy(Address Dest, Address Src, llvm::Value *Size, bool IsVolatile=false)
Definition: CGBuilder.h:274
bool isConstexpr() const
Whether this variable is (C++11) constexpr.
Definition: Decl.h:1386
CharUnits getDeclAlign(const Decl *D, bool ForAlignof=false) const
Return a conservative estimate of the alignment of the specified decl D.
Expr * IgnoreParenCasts() LLVM_READONLY
Skip past any parentheses and casts which might surround this expression until reaching a fixed point...
Definition: Expr.cpp:2946
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:1235
llvm::BasicBlock * createBasicBlock(const Twine &name="", llvm::Function *parent=nullptr, llvm::BasicBlock *before=nullptr)
createBasicBlock - Create an LLVM basic block.
llvm::Value * EmitARCRetainScalarExpr(const Expr *expr)
EmitARCRetainScalarExpr - Semantically equivalent to EmitARCRetainObject(e->getType(), EmitScalarExpr(e)), but making a best-effort attempt to peephole expressions that naturally produce retained objects.
Definition: CGObjC.cpp:3240
Denotes a cleanup that should run when a scope is exited using normal control flow (falling off the e...
Definition: EHScopeStack.h:84
void EmitAtomicInit(Expr *E, LValue lvalue)
Definition: CGAtomic.cpp:2004
const internal::VariadicDynCastAllOfMatcher< Stmt, CastExpr > castExpr
Matches any cast nodes of Clang&#39;s AST.
bool hasTrivialDestructor() const
Determine whether this class has a trivial destructor (C++ [class.dtor]p3)
Definition: DeclCXX.h:1491
static VarDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, StorageClass S)
Definition: Decl.cpp:1952
static bool ContainsLabel(const Stmt *S, bool IgnoreCaseStmts=false)
ContainsLabel - Return true if the statement contains a label in it.
CXXDestructorDecl * getDestructor() const
Returns the destructor decl for this class.
Definition: DeclCXX.cpp:1736
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:105
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:1602
void registerVLASizeExpression(QualType Ty, llvm::Metadata *SizeExpr)
Register VLA size expression debug node with the qualified type.
Definition: CGDebugInfo.h:348
This object can be modified without requiring retains or releases.
Definition: Type.h:158
bool isTypeConstant(QualType QTy, bool ExcludeCtorDtor)
isTypeConstant - Determine whether an object of this type can be emitted as a constant.
StorageDuration getStorageDuration() const
Get the storage duration of this variable, per C++ [basic.stc].
Definition: Decl.h:1080
llvm::Value * EmitLoadOfScalar(Address Addr, bool Volatile, QualType Ty, SourceLocation Loc, AlignmentSource Source=AlignmentSource::Type, bool isNontemporal=false)
EmitLoadOfScalar - Load a scalar value from an address, taking care to appropriately convert from the...
bool hasAttr() const
Definition: DeclBase.h:542
bool isValid() const
Definition: Address.h:35
static CharUnits One()
One - Construct a CharUnits quantity of one.
Definition: CharUnits.h:57
CompoundStmt - This represents a group of statements like { stmt stmt }.
Definition: Stmt.h:1310
std::pair< llvm::Value *, llvm::Value * > ComplexPairTy
AutoVarEmission EmitAutoVarAlloca(const VarDecl &var)
EmitAutoVarAlloca - Emit the alloca and debug information for a local variable.
Definition: CGDecl.cpp:1379
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1636
const CodeGen::CGBlockInfo * BlockInfo
IdentifierInfo & getOwn(StringRef Name)
Gets an IdentifierInfo for the given name without consulting external sources.
RValue - This trivial value class is used to represent the result of an expression that is evaluated...
Definition: CGValue.h:38
void setAddress(Address address)
Definition: CGValue.h:327
CleanupKind getARCCleanupKind()
Retrieves the default cleanup kind for an ARC cleanup.
StringRef getBlockMangledName(GlobalDecl GD, const BlockDecl *BD)
llvm::Constant * getNullPointer(llvm::PointerType *T, QualType QT)
Get target specific null pointer.
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition: CharUnits.h:178
static bool shouldSplitConstantStore(CodeGenModule &CGM, uint64_t GlobalByteSize)
Decide whether we want to split a constant structure or array store into a sequence of its fields&#39; st...
Definition: CGDecl.cpp:977
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:118
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:272
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:3889
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition: Decl.h:636
This represents one expression.
Definition: Expr.h:108
SourceLocation End
void EmitARCMoveWeak(Address dst, Address src)
void @objc_moveWeak(i8** dest, i8** src) Disregards the current value in dest.
Definition: CGObjC.cpp:2452
Emit only debug info necessary for generating line number tables (-gline-tables-only).
Address getOriginalAllocatedAddress() const
Returns the address for the original alloca instruction.
void EmitAutoVarInit(const AutoVarEmission &emission)
Definition: CGDecl.cpp:1665
static Address invalid()
Definition: Address.h:34
std::string Label
void AddGlobalAnnotations(const ValueDecl *D, llvm::GlobalValue *GV)
Add global annotations that are set on D, for the global GV.
bool hasLocalStorage() const
Returns true if a variable with function scope is a non-static local variable.
Definition: Decl.h:1035
Enters a new scope for capturing cleanups, all of which will be executed once the scope is exited...
static CGCallee forDirect(llvm::Constant *functionPtr, const CGCalleeInfo &abstractInfo=CGCalleeInfo())
Definition: CGCall.h:133
#define V(N, I)
Definition: ASTContext.h:2907
virtual void EmitWorkGroupLocalVarDecl(CodeGenFunction &CGF, const VarDecl &D)
Emit the IR required for a work-group-local variable declaration, and add an entry to CGF&#39;s LocalDecl...
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition: Expr.h:5541
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2830
const internal::VariadicAllOfMatcher< Decl > decl
Matches declarations.
bool isExceptionVariable() const
Determine whether this variable is the exception variable in a C++ catch statememt or an Objective-C ...
Definition: Decl.h:1311
VlaSizePair getVLASize(const VariableArrayType *vla)
Returns an LLVM value that corresponds to the size, in non-variably-sized elements, of a variable length array type, plus that largest non-variably-sized element type.
llvm::PointerType * getType() const
Return the type of the pointer value.
Definition: Address.h:43
ObjCLifetime getObjCLifetime() const
Definition: Type.h:327
CharUnits getTypeAlignInChars(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in characters.
DeclContext * getDeclContext()
Definition: DeclBase.h:438
SourceLocation Begin
static SVal getValue(SVal val, SValBuilder &svalBuilder)
const AstTypeMatcher< ArrayType > arrayType
Matches all kinds of arrays.
Base object dtor.
Definition: ABI.h:36
QualType getType() const
Definition: Expr.h:137
void EmitNullabilityCheck(LValue LHS, llvm::Value *RHS, SourceLocation Loc)
Given an assignment *LHS = RHS, emit a test that checks if RHS is nonnull, if LHS is marked _Nonnull...
Definition: CGDecl.cpp:723
llvm::GlobalValue::LinkageTypes getLLVMLinkageVarDefinition(const VarDecl *VD, bool IsConstant)
Returns LLVM linkage for a declarator.
Checking the value assigned to a _Nonnull pointer. Must not be null.
CharUnits alignmentOfArrayElement(CharUnits elementSize) const
Given that this is the alignment of the first element of an array, return the minimum alignment of an...
Definition: CharUnits.h:196
llvm::PointerType * AllocaInt8PtrTy
llvm::CallInst * CreateMemSet(Address Dest, llvm::Value *Value, llvm::Value *Size, bool IsVolatile=false)
Definition: CGBuilder.h:296
llvm::Function * getLLVMLifetimeStartFn()
Lazily declare the .lifetime.start intrinsic.
Definition: CGDecl.cpp: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
const Type * getBaseElementTypeUnsafe() const
Get the base element type of this type, potentially discarding type qualifiers.
Definition: Type.h:6758
static bool hasNontrivialDestruction(QualType T)
hasNontrivialDestruction - Determine whether a type&#39;s destruction is non-trivial. ...
Definition: CGDecl.cpp:308
float __ovld __cnfn length(float p)
Return the length of vector p, i.e., sqrt(p.x2 + p.y 2 + ...)
const LangOptions & getLangOpts() const
ASTContext & getContext() const
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:40
The l-value was considered opaque, so the alignment was determined from a type.
void setBlockContextParameter(const ImplicitParamDecl *D, unsigned argNum, llvm::Value *ptr)
Definition: CGBlocks.cpp:1476
llvm::Constant * getOrCreateStaticVarDecl(const VarDecl &D, llvm::GlobalValue::LinkageTypes Linkage)
Definition: CGDecl.cpp:215
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:1256
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:318
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:1316
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:6243
static std::string getStaticDeclName(CodeGenModule &CGM, const VarDecl &D)
Definition: CGDecl.cpp:192
bool isVariablyModifiedType() const
Whether this type is a variably-modified type (C99 6.7.5).
Definition: Type.h:2109
Address CreateConstInBoundsGEP2_32(Address Addr, unsigned Idx0, unsigned Idx1, const llvm::Twine &Name="")
Definition: CGBuilder.h:258
RValue EmitCall(const CGFunctionInfo &CallInfo, const CGCallee &Callee, ReturnValueSlot ReturnValue, const CallArgList &Args, llvm::CallBase **callOrInvoke, SourceLocation Loc)
EmitCall - Generate a call of the given function, expecting the given result type, and using the given argument list which specifies both the LLVM arguments and the types they were derived from.
Definition: CGCall.cpp:3776
std::string getNameAsString() const
Get a human-readable name for the declaration, even if it is one of the special kinds of names (C++ c...
Definition: Decl.h:291
TypeSourceInfo * CreateTypeSourceInfo(QualType T, unsigned Size=0) const
Allocate an uninitialized TypeSourceInfo.
static void emitPartialArrayDestroy(CodeGenFunction &CGF, llvm::Value *begin, llvm::Value *end, QualType type, CharUnits elementAlign, CodeGenFunction::Destroyer *destroyer)
Perform partial array destruction as if in an EH cleanup.
Definition: CGDecl.cpp:2193
const Decl * getDecl() const
Definition: GlobalDecl.h:76
const BlockByrefInfo & getBlockByrefInfo(const VarDecl *var)
BuildByrefInfo - This routine changes a __block variable declared as T x into:
Definition: CGBlocks.cpp:2731
Address CreateConstArrayGEP(Address Addr, uint64_t Index, const llvm::Twine &Name="")
Given addr = [n x T]* ...
Definition: CGBuilder.h:198
llvm::Value * EmitLifetimeStart(uint64_t Size, llvm::Value *Addr)
Emit a lifetime.begin marker if some criteria are satisfied.
Definition: CGDecl.cpp:1290
QualType getBaseElementType(const ArrayType *VAT) const
Return the innermost element type of an array type.
static bool cxxDestructorCanThrow(QualType T)
Check if T is a C++ class that has a destructor that can throw.
Definition: CGBlocks.cpp:1802
static bool isAccessedBy(const VarDecl &var, const Stmt *s)
Definition: CGDecl.cpp:635
bool hasGlobalStorage() const
Returns true for all variables that do not have local storage.
Definition: Decl.h:1077
static void emitStoresForZeroInit(CodeGenModule &CGM, const VarDecl &D, Address Loc, bool isVolatile, CGBuilderTy &Builder)
Definition: CGDecl.cpp:1226
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:1651
bool isObjCObjectPointerType() const
Definition: Type.h:6455
Address createUnnamedGlobalFrom(const VarDecl &D, llvm::Constant *Constant, CharUnits Align)
Definition: CGDecl.cpp:1080
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:1130
llvm::DILocalVariable * EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI, CGBuilderTy &Builder, const bool UsePointerValue=false)
Emit call to llvm.dbg.declare for an automatic variable declaration.
DestructionKind isDestructedType() const
Returns a nonzero value if objects of this type require non-trivial work to clean up after...
Definition: Type.h:1163
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:599
bool isUsed(bool CheckUsedAttr=true) const
Whether any (re-)declaration of the entity was used, meaning that a definition is required...
Definition: DeclBase.cpp:397
void enterByrefCleanup(CleanupKind Kind, Address Addr, BlockFieldFlags Flags, bool LoadBlockVarAddr, bool CanThrow)
Enter a cleanup to destroy a __block variable.
Definition: CGBlocks.cpp:2939
Complete object dtor.
Definition: ABI.h:35
void EmitParmDecl(const VarDecl &D, ParamValue Arg, unsigned ArgNo)
EmitParmDecl - Emit a ParmVarDecl or an ImplicitParamDecl.
Definition: CGDecl.cpp: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:3922
QualType getType() const
Definition: CGValue.h:263
void EmitDecl(const Decl &D)
EmitDecl - Emit a declaration.
Definition: CGDecl.cpp:42
const TargetCodeGenInfo & getTargetHooks() const
static Destroyer destroyARCStrongImprecise
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition: Expr.cpp:215
llvm::Value * EmitScalarExpr(const Expr *E, bool IgnoreResultAssign=false)
EmitScalarExpr - Emit the computation of the specified expression of LLVM scalar type, returning the result.
void addUsedGlobal(llvm::GlobalValue *GV)
Add a global to a list to be added to the llvm.used metadata.
bool isObjCGCWeak() const
true when Type is objc&#39;s weak.
Definition: Type.h:1053
Base class for declarations which introduce a typedef-name.
Definition: Decl.h:2947
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:737
void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type, bool ForVirtualBase, bool Delegating, Address This)
Definition: CGClass.cpp:2402
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1271
ArrayRef< Capture > captures() const
Definition: Decl.h:4016
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:3002
const Expr * getInit() const
Definition: Decl.h:1219
llvm::LoadInst * CreateLoad(Address Addr, const llvm::Twine &Name="")
Definition: CGBuilder.h:69
Kind getKind() const
Definition: DeclBase.h:432
Decl * getNonClosureContext()
Find the innermost non-closure ancestor of this declaration, walking up through blocks, lambdas, etc.
Definition: DeclBase.cpp:990
llvm::Constant * EmitNullConstant(QualType T)
Return the result of value-initializing the given type, i.e.
llvm::Function * getIntrinsic(unsigned IID, ArrayRef< llvm::Type *> Tys=None)
llvm::StoreInst * CreateStore(llvm::Value *Val, Address Addr, bool IsVolatile=false)
Definition: CGBuilder.h:107
static AggValueSlot forLValue(const LValue &LV, IsDestructed_t isDestructed, NeedsGCBarriers_t needsGC, IsAliased_t isAliased, Overlap_t mayOverlap, IsZeroed_t isZeroed=IsNotZeroed, IsSanitizerChecked_t isChecked=IsNotSanitizerChecked)
Definition: CGValue.h:539
bool isEscapingByref() const
Indicates the capture is a __block variable that is captured by a block that can potentially escape (...
Definition: Decl.cpp:2432
llvm::Module & getModule() const
static bool tryEmitARCCopyWeakInit(CodeGenFunction &CGF, const LValue &destLV, const Expr *init)
Definition: CGDecl.cpp:667
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:1826
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:4423
bool isConstantSizeType() const
Return true if this is not a variable sized type, according to the rules of C99 6.7.5p3.
Definition: Type.cpp:2052
CodeGenTypes & getTypes() const
void EmitStoreOfComplex(ComplexPairTy V, LValue dest, bool isInit)
EmitStoreOfComplex - Store a complex number into the specified l-value.
bool isConstantInitializer(ASTContext &Ctx, bool ForRef, const Expr **Culprit=nullptr) const
isConstantInitializer - Returns true if this expression can be emitted to IR as a constant...
Definition: Expr.cpp:3107
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:6468
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:744
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:450
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:3799
static Destroyer destroyNonTrivialCStruct
bool IsBypassed(const VarDecl *D) const
Returns true if the variable declaration was by bypassed by any goto or switch statement.
ARCPreciseLifetime_t
Does an ARC strong l-value have precise lifetime?
Definition: CGValue.h:119
ComplexPairTy EmitComplexExpr(const Expr *E, bool IgnoreReal=false, bool IgnoreImag=false)
EmitComplexExpr - Emit the computation of the specified expression of complex type, returning the result.
llvm::ConstantInt * getSize(CharUnits numChars)
Emit the given number of characters as a value of type size_t.
A use of a default initializer in a constructor or in aggregate initialization.
Definition: ExprCXX.h:1183
static llvm::Constant * EmitNullConstant(CodeGenModule &CGM, const RecordDecl *record, bool asCompleteObject)
Reading or writing from this object requires a barrier call.
Definition: Type.h:168
llvm::DenseMap< const VarDecl *, llvm::Value * > NRVOFlags
A mapping from NRVO variables to the flags used to indicate when the NRVO has been applied to this va...
QualType getUnqualifiedType() const
Retrieve the unqualified variant of the given type, removing as little sugar as possible.
Definition: Type.h:6207
Represents a C++ struct/union/class.
Definition: DeclCXX.h:300
bool isNRVOVariable() const
Determine whether this local variable can be used with the named return value optimization (NRVO)...
Definition: Decl.h:1329
llvm::Type * ConvertType(QualType T)
Qualifiers getQualifiers() const
Retrieve the set of qualifiers applied to this type.
Definition: Type.h:6154
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:6516
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:1068
This represents &#39;#pragma omp declare mapper ...&#39; directive.
Definition: DeclOpenMP.h:217
bool has(SanitizerMask K) const
Check if a certain (single) sanitizer is enabled.
Definition: Sanitizers.h:152
Defines the clang::TargetInfo interface.
void finalize(llvm::GlobalVariable *global)
bool isMicrosoft() const
Is this ABI an MSVC-compatible ABI?
Definition: TargetCXXABI.h:153
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition: Decl.h:275
void setLocation(SourceLocation Loc)
Update the current source location.
const VariableArrayType * getAsVariableArrayType(QualType T) const
Definition: ASTContext.h:2444
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:1141
static RValue get(llvm::Value *V)
Definition: CGValue.h:85
void EmitLifetimeEnd(llvm::Value *Size, llvm::Value *Addr)
Definition: CGDecl.cpp:1306
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:2865
bool isLocalVarDecl() const
Returns true for local variable declarations other than parameters.
Definition: Decl.h:1104
QualType getType() const
Definition: Decl.h:647
An l-value expression is a reference to an object with independent storage.
Definition: Specifiers.h:128
LValue - This represents an lvalue references.
Definition: CGValue.h:166
Information for lazily generating a cleanup.
Definition: EHScopeStack.h:146
void EmitVarAnnotations(const VarDecl *D, llvm::Value *V)
Emit local annotations for the local variable V, declared by D.
Automatic storage duration (most local variables).
Definition: Specifiers.h:307
SanitizerMetadata * getSanitizerMetadata()
bool isConstant(const ASTContext &Ctx) const
Definition: Type.h:778
bool CurFuncIsThunk
In C++, whether we are code generating a thunk.
const LangOptions & getLangOpts() const
unsigned getTargetAddressSpace(QualType T) const
Definition: ASTContext.h:2532
llvm::Value * emitArrayLength(const ArrayType *arrayType, QualType &baseType, Address &addr)
emitArrayLength - Compute the length of an array, even if it&#39;s a VLA, and drill down to the base elem...
CallArgList - Type for representing both the value and type of arguments in a call.
Definition: CGCall.h:262
const LangOptions & getLangOpts() const
Definition: ASTContext.h:710
void PopCleanupBlock(bool FallThroughIsBranchThrough=false)
PopCleanupBlock - Will pop the cleanup entry on the stack and process all branch fixups.
Definition: CGCleanup.cpp:644
llvm::Value * getPointer() const
Definition: CGValue.h:322
Address emitBlockByrefAddress(Address baseAddr, const VarDecl *V, bool followForward=true)
BuildBlockByrefAddress - Computes the location of the data in a variable which is declared as __block...
Definition: CGBlocks.cpp:2696
SourceLocation getLocation() const
Definition: DeclBase.h:429
void pushIrregularPartialArrayCleanup(llvm::Value *arrayBegin, Address arrayEndPointer, QualType elementType, CharUnits elementAlignment, Destroyer *destroyer)
pushIrregularPartialArrayCleanup - Push an EH cleanup to destroy already-constructed elements of the ...
Definition: CGDecl.cpp:2278
bool isExternallyVisible() const
Definition: Decl.h:379
QualType getIntTypeForBitwidth(unsigned DestWidth, unsigned Signed) const
getIntTypeForBitwidth - sets integer QualTy according to specified details: bitwidth, signed/unsigned.
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point...
Definition: Expr.cpp:2937
void Destroyer(CodeGenFunction &CGF, Address addr, QualType ty)
static CharUnits getDeclAlign(Expr *E, CharUnits TypeAlign, ASTContext &Context)
A helper function to get the alignment of a Decl referred to by DeclRefExpr or MemberExpr.
Qualifiers::ObjCLifetime getObjCLifetime() const
Returns lifetime attribute of this type.
Definition: Type.h:1063