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