1 //===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This contains code to emit Decl nodes as LLVM code.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CodeGenFunction.h"
15 #include "CGDebugInfo.h"
16 #include "CGOpenCLRuntime.h"
17 #include "CodeGenModule.h"
18 #include "clang/AST/ASTContext.h"
19 #include "clang/AST/CharUnits.h"
20 #include "clang/AST/Decl.h"
21 #include "clang/AST/DeclObjC.h"
22 #include "clang/Basic/SourceManager.h"
23 #include "clang/Basic/TargetInfo.h"
24 #include "clang/Frontend/CodeGenOptions.h"
25 #include "llvm/IR/DataLayout.h"
26 #include "llvm/IR/GlobalVariable.h"
27 #include "llvm/IR/Intrinsics.h"
28 #include "llvm/IR/Type.h"
29 using namespace clang;
30 using namespace CodeGen;
31 
32 
33 void CodeGenFunction::EmitDecl(const Decl &D) {
34   switch (D.getKind()) {
35   case Decl::TranslationUnit:
36   case Decl::Namespace:
37   case Decl::UnresolvedUsingTypename:
38   case Decl::ClassTemplateSpecialization:
39   case Decl::ClassTemplatePartialSpecialization:
40   case Decl::TemplateTypeParm:
41   case Decl::UnresolvedUsingValue:
42   case Decl::NonTypeTemplateParm:
43   case Decl::CXXMethod:
44   case Decl::CXXConstructor:
45   case Decl::CXXDestructor:
46   case Decl::CXXConversion:
47   case Decl::Field:
48   case Decl::MSProperty:
49   case Decl::IndirectField:
50   case Decl::ObjCIvar:
51   case Decl::ObjCAtDefsField:
52   case Decl::ParmVar:
53   case Decl::ImplicitParam:
54   case Decl::ClassTemplate:
55   case Decl::FunctionTemplate:
56   case Decl::TypeAliasTemplate:
57   case Decl::TemplateTemplateParm:
58   case Decl::ObjCMethod:
59   case Decl::ObjCCategory:
60   case Decl::ObjCProtocol:
61   case Decl::ObjCInterface:
62   case Decl::ObjCCategoryImpl:
63   case Decl::ObjCImplementation:
64   case Decl::ObjCProperty:
65   case Decl::ObjCCompatibleAlias:
66   case Decl::AccessSpec:
67   case Decl::LinkageSpec:
68   case Decl::ObjCPropertyImpl:
69   case Decl::FileScopeAsm:
70   case Decl::Friend:
71   case Decl::FriendTemplate:
72   case Decl::Block:
73   case Decl::Captured:
74   case Decl::ClassScopeFunctionSpecialization:
75     llvm_unreachable("Declaration should not be in declstmts!");
76   case Decl::Function:  // void X();
77   case Decl::Record:    // struct/union/class X;
78   case Decl::Enum:      // enum X;
79   case Decl::EnumConstant: // enum ? { X = ? }
80   case Decl::CXXRecord: // struct/union/class X; [C++]
81   case Decl::Using:          // using X; [C++]
82   case Decl::UsingShadow:
83   case Decl::UsingDirective: // using namespace X; [C++]
84   case Decl::NamespaceAlias:
85   case Decl::StaticAssert: // static_assert(X, ""); [C++0x]
86   case Decl::Label:        // __label__ x;
87   case Decl::Import:
88   case Decl::OMPThreadPrivate:
89   case Decl::Empty:
90     // None of these decls require codegen support.
91     return;
92 
93   case Decl::Var: {
94     const VarDecl &VD = cast<VarDecl>(D);
95     assert(VD.isLocalVarDecl() &&
96            "Should not see file-scope variables inside a function!");
97     return EmitVarDecl(VD);
98   }
99 
100   case Decl::Typedef:      // typedef int X;
101   case Decl::TypeAlias: {  // using X = int; [C++0x]
102     const TypedefNameDecl &TD = cast<TypedefNameDecl>(D);
103     QualType Ty = TD.getUnderlyingType();
104 
105     if (Ty->isVariablyModifiedType())
106       EmitVariablyModifiedType(Ty);
107   }
108   }
109 }
110 
111 /// EmitVarDecl - This method handles emission of any variable declaration
112 /// inside a function, including static vars etc.
113 void CodeGenFunction::EmitVarDecl(const VarDecl &D) {
114   switch (D.getStorageClass()) {
115   case SC_None:
116   case SC_Auto:
117   case SC_Register:
118     return EmitAutoVarDecl(D);
119   case SC_Static: {
120     llvm::GlobalValue::LinkageTypes Linkage =
121       llvm::GlobalValue::InternalLinkage;
122 
123     // If the function definition has some sort of weak linkage, its
124     // static variables should also be weak so that they get properly
125     // uniqued.  We can't do this in C, though, because there's no
126     // standard way to agree on which variables are the same (i.e.
127     // there's no mangling).
128     if (getLangOpts().CPlusPlus)
129       if (llvm::GlobalValue::isWeakForLinker(CurFn->getLinkage()))
130         Linkage = CurFn->getLinkage();
131 
132     return EmitStaticVarDecl(D, Linkage);
133   }
134   case SC_Extern:
135   case SC_PrivateExtern:
136     // Don't emit it now, allow it to be emitted lazily on its first use.
137     return;
138   case SC_OpenCLWorkGroupLocal:
139     return CGM.getOpenCLRuntime().EmitWorkGroupLocalVarDecl(*this, D);
140   }
141 
142   llvm_unreachable("Unknown storage class");
143 }
144 
145 static std::string GetStaticDeclName(CodeGenFunction &CGF, const VarDecl &D,
146                                      const char *Separator) {
147   CodeGenModule &CGM = CGF.CGM;
148   if (CGF.getLangOpts().CPlusPlus) {
149     StringRef Name = CGM.getMangledName(&D);
150     return Name.str();
151   }
152 
153   std::string ContextName;
154   if (!CGF.CurFuncDecl) {
155     // Better be in a block declared in global scope.
156     const NamedDecl *ND = cast<NamedDecl>(&D);
157     const DeclContext *DC = ND->getDeclContext();
158     if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
159       MangleBuffer Name;
160       CGM.getBlockMangledName(GlobalDecl(), Name, BD);
161       ContextName = Name.getString();
162     }
163     else
164       llvm_unreachable("Unknown context for block static var decl");
165   } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CGF.CurFuncDecl)) {
166     StringRef Name = CGM.getMangledName(FD);
167     ContextName = Name.str();
168   } else if (isa<ObjCMethodDecl>(CGF.CurFuncDecl))
169     ContextName = CGF.CurFn->getName();
170   else
171     llvm_unreachable("Unknown context for static var decl");
172 
173   return ContextName + Separator + D.getNameAsString();
174 }
175 
176 llvm::GlobalVariable *
177 CodeGenFunction::CreateStaticVarDecl(const VarDecl &D,
178                                      const char *Separator,
179                                      llvm::GlobalValue::LinkageTypes Linkage) {
180   QualType Ty = D.getType();
181   assert(Ty->isConstantSizeType() && "VLAs can't be static");
182 
183   // Use the label if the variable is renamed with the asm-label extension.
184   std::string Name;
185   if (D.hasAttr<AsmLabelAttr>())
186     Name = CGM.getMangledName(&D);
187   else
188     Name = GetStaticDeclName(*this, D, Separator);
189 
190   llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
191   unsigned AddrSpace =
192    CGM.GetGlobalVarAddressSpace(&D, CGM.getContext().getTargetAddressSpace(Ty));
193   llvm::GlobalVariable *GV =
194     new llvm::GlobalVariable(CGM.getModule(), LTy,
195                              Ty.isConstant(getContext()), Linkage,
196                              CGM.EmitNullConstant(D.getType()), Name, 0,
197                              llvm::GlobalVariable::NotThreadLocal,
198                              AddrSpace);
199   GV->setAlignment(getContext().getDeclAlign(&D).getQuantity());
200   if (Linkage != llvm::GlobalValue::InternalLinkage)
201     GV->setVisibility(CurFn->getVisibility());
202 
203   if (D.getTLSKind())
204     CGM.setTLSMode(GV, D);
205 
206   return GV;
207 }
208 
209 /// hasNontrivialDestruction - Determine whether a type's destruction is
210 /// non-trivial. If so, and the variable uses static initialization, we must
211 /// register its destructor to run on exit.
212 static bool hasNontrivialDestruction(QualType T) {
213   CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
214   return RD && !RD->hasTrivialDestructor();
215 }
216 
217 /// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the
218 /// global variable that has already been created for it.  If the initializer
219 /// has a different type than GV does, this may free GV and return a different
220 /// one.  Otherwise it just returns GV.
221 llvm::GlobalVariable *
222 CodeGenFunction::AddInitializerToStaticVarDecl(const VarDecl &D,
223                                                llvm::GlobalVariable *GV) {
224   llvm::Constant *Init = CGM.EmitConstantInit(D, this);
225 
226   // If constant emission failed, then this should be a C++ static
227   // initializer.
228   if (!Init) {
229     if (!getLangOpts().CPlusPlus)
230       CGM.ErrorUnsupported(D.getInit(), "constant l-value expression");
231     else if (Builder.GetInsertBlock()) {
232       // Since we have a static initializer, this global variable can't
233       // be constant.
234       GV->setConstant(false);
235 
236       EmitCXXGuardedInit(D, GV, /*PerformInit*/true);
237     }
238     return GV;
239   }
240 
241   // The initializer may differ in type from the global. Rewrite
242   // the global to match the initializer.  (We have to do this
243   // because some types, like unions, can't be completely represented
244   // in the LLVM type system.)
245   if (GV->getType()->getElementType() != Init->getType()) {
246     llvm::GlobalVariable *OldGV = GV;
247 
248     GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
249                                   OldGV->isConstant(),
250                                   OldGV->getLinkage(), Init, "",
251                                   /*InsertBefore*/ OldGV,
252                                   OldGV->getThreadLocalMode(),
253                            CGM.getContext().getTargetAddressSpace(D.getType()));
254     GV->setVisibility(OldGV->getVisibility());
255 
256     // Steal the name of the old global
257     GV->takeName(OldGV);
258 
259     // Replace all uses of the old global with the new global
260     llvm::Constant *NewPtrForOldDecl =
261     llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
262     OldGV->replaceAllUsesWith(NewPtrForOldDecl);
263 
264     // Erase the old global, since it is no longer used.
265     OldGV->eraseFromParent();
266   }
267 
268   GV->setConstant(CGM.isTypeConstant(D.getType(), true));
269   GV->setInitializer(Init);
270 
271   if (hasNontrivialDestruction(D.getType())) {
272     // We have a constant initializer, but a nontrivial destructor. We still
273     // need to perform a guarded "initialization" in order to register the
274     // destructor.
275     EmitCXXGuardedInit(D, GV, /*PerformInit*/false);
276   }
277 
278   return GV;
279 }
280 
281 void CodeGenFunction::EmitStaticVarDecl(const VarDecl &D,
282                                       llvm::GlobalValue::LinkageTypes Linkage) {
283   llvm::Value *&DMEntry = LocalDeclMap[&D];
284   assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
285 
286   // Check to see if we already have a global variable for this
287   // declaration.  This can happen when double-emitting function
288   // bodies, e.g. with complete and base constructors.
289   llvm::Constant *addr =
290     CGM.getStaticLocalDeclAddress(&D);
291 
292   llvm::GlobalVariable *var;
293   if (addr) {
294     var = cast<llvm::GlobalVariable>(addr->stripPointerCasts());
295   } else {
296     addr = var = CreateStaticVarDecl(D, ".", Linkage);
297   }
298 
299   // Store into LocalDeclMap before generating initializer to handle
300   // circular references.
301   DMEntry = addr;
302   CGM.setStaticLocalDeclAddress(&D, addr);
303 
304   // We can't have a VLA here, but we can have a pointer to a VLA,
305   // even though that doesn't really make any sense.
306   // Make sure to evaluate VLA bounds now so that we have them for later.
307   if (D.getType()->isVariablyModifiedType())
308     EmitVariablyModifiedType(D.getType());
309 
310   // Save the type in case adding the initializer forces a type change.
311   llvm::Type *expectedType = addr->getType();
312 
313   // If this value has an initializer, emit it.
314   if (D.getInit())
315     var = AddInitializerToStaticVarDecl(D, var);
316 
317   var->setAlignment(getContext().getDeclAlign(&D).getQuantity());
318 
319   if (D.hasAttr<AnnotateAttr>())
320     CGM.AddGlobalAnnotations(&D, var);
321 
322   if (const SectionAttr *SA = D.getAttr<SectionAttr>())
323     var->setSection(SA->getName());
324 
325   if (D.hasAttr<UsedAttr>())
326     CGM.AddUsedGlobal(var);
327 
328   // We may have to cast the constant because of the initializer
329   // mismatch above.
330   //
331   // FIXME: It is really dangerous to store this in the map; if anyone
332   // RAUW's the GV uses of this constant will be invalid.
333   llvm::Constant *castedAddr = llvm::ConstantExpr::getBitCast(var, expectedType);
334   DMEntry = castedAddr;
335   CGM.setStaticLocalDeclAddress(&D, castedAddr);
336 
337   // Emit global variable debug descriptor for static vars.
338   CGDebugInfo *DI = getDebugInfo();
339   if (DI &&
340       CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo) {
341     DI->setLocation(D.getLocation());
342     DI->EmitGlobalVariable(var, &D);
343   }
344 }
345 
346 namespace {
347   struct DestroyObject : EHScopeStack::Cleanup {
348     DestroyObject(llvm::Value *addr, QualType type,
349                   CodeGenFunction::Destroyer *destroyer,
350                   bool useEHCleanupForArray)
351       : addr(addr), type(type), destroyer(destroyer),
352         useEHCleanupForArray(useEHCleanupForArray) {}
353 
354     llvm::Value *addr;
355     QualType type;
356     CodeGenFunction::Destroyer *destroyer;
357     bool useEHCleanupForArray;
358 
359     void Emit(CodeGenFunction &CGF, Flags flags) {
360       // Don't use an EH cleanup recursively from an EH cleanup.
361       bool useEHCleanupForArray =
362         flags.isForNormalCleanup() && this->useEHCleanupForArray;
363 
364       CGF.emitDestroy(addr, type, destroyer, useEHCleanupForArray);
365     }
366   };
367 
368   struct DestroyNRVOVariable : EHScopeStack::Cleanup {
369     DestroyNRVOVariable(llvm::Value *addr,
370                         const CXXDestructorDecl *Dtor,
371                         llvm::Value *NRVOFlag)
372       : Dtor(Dtor), NRVOFlag(NRVOFlag), Loc(addr) {}
373 
374     const CXXDestructorDecl *Dtor;
375     llvm::Value *NRVOFlag;
376     llvm::Value *Loc;
377 
378     void Emit(CodeGenFunction &CGF, Flags flags) {
379       // Along the exceptions path we always execute the dtor.
380       bool NRVO = flags.isForNormalCleanup() && NRVOFlag;
381 
382       llvm::BasicBlock *SkipDtorBB = 0;
383       if (NRVO) {
384         // If we exited via NRVO, we skip the destructor call.
385         llvm::BasicBlock *RunDtorBB = CGF.createBasicBlock("nrvo.unused");
386         SkipDtorBB = CGF.createBasicBlock("nrvo.skipdtor");
387         llvm::Value *DidNRVO = CGF.Builder.CreateLoad(NRVOFlag, "nrvo.val");
388         CGF.Builder.CreateCondBr(DidNRVO, SkipDtorBB, RunDtorBB);
389         CGF.EmitBlock(RunDtorBB);
390       }
391 
392       CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete,
393                                 /*ForVirtualBase=*/false,
394                                 /*Delegating=*/false,
395                                 Loc);
396 
397       if (NRVO) CGF.EmitBlock(SkipDtorBB);
398     }
399   };
400 
401   struct CallStackRestore : EHScopeStack::Cleanup {
402     llvm::Value *Stack;
403     CallStackRestore(llvm::Value *Stack) : Stack(Stack) {}
404     void Emit(CodeGenFunction &CGF, Flags flags) {
405       llvm::Value *V = CGF.Builder.CreateLoad(Stack);
406       llvm::Value *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stackrestore);
407       CGF.Builder.CreateCall(F, V);
408     }
409   };
410 
411   struct ExtendGCLifetime : EHScopeStack::Cleanup {
412     const VarDecl &Var;
413     ExtendGCLifetime(const VarDecl *var) : Var(*var) {}
414 
415     void Emit(CodeGenFunction &CGF, Flags flags) {
416       // Compute the address of the local variable, in case it's a
417       // byref or something.
418       DeclRefExpr DRE(const_cast<VarDecl*>(&Var), false,
419                       Var.getType(), VK_LValue, SourceLocation());
420       llvm::Value *value = CGF.EmitLoadOfScalar(CGF.EmitDeclRefLValue(&DRE));
421       CGF.EmitExtendGCLifetime(value);
422     }
423   };
424 
425   struct CallCleanupFunction : EHScopeStack::Cleanup {
426     llvm::Constant *CleanupFn;
427     const CGFunctionInfo &FnInfo;
428     const VarDecl &Var;
429 
430     CallCleanupFunction(llvm::Constant *CleanupFn, const CGFunctionInfo *Info,
431                         const VarDecl *Var)
432       : CleanupFn(CleanupFn), FnInfo(*Info), Var(*Var) {}
433 
434     void Emit(CodeGenFunction &CGF, Flags flags) {
435       DeclRefExpr DRE(const_cast<VarDecl*>(&Var), false,
436                       Var.getType(), VK_LValue, SourceLocation());
437       // Compute the address of the local variable, in case it's a byref
438       // or something.
439       llvm::Value *Addr = CGF.EmitDeclRefLValue(&DRE).getAddress();
440 
441       // In some cases, the type of the function argument will be different from
442       // the type of the pointer. An example of this is
443       // void f(void* arg);
444       // __attribute__((cleanup(f))) void *g;
445       //
446       // To fix this we insert a bitcast here.
447       QualType ArgTy = FnInfo.arg_begin()->type;
448       llvm::Value *Arg =
449         CGF.Builder.CreateBitCast(Addr, CGF.ConvertType(ArgTy));
450 
451       CallArgList Args;
452       Args.add(RValue::get(Arg),
453                CGF.getContext().getPointerType(Var.getType()));
454       CGF.EmitCall(FnInfo, CleanupFn, ReturnValueSlot(), Args);
455     }
456   };
457 
458   /// A cleanup to call @llvm.lifetime.end.
459   class CallLifetimeEnd : public EHScopeStack::Cleanup {
460     llvm::Value *Addr;
461     llvm::Value *Size;
462   public:
463     CallLifetimeEnd(llvm::Value *addr, llvm::Value *size)
464       : Addr(addr), Size(size) {}
465 
466     void Emit(CodeGenFunction &CGF, Flags flags) {
467       llvm::Value *castAddr = CGF.Builder.CreateBitCast(Addr, CGF.Int8PtrTy);
468       CGF.Builder.CreateCall2(CGF.CGM.getLLVMLifetimeEndFn(),
469                               Size, castAddr)
470         ->setDoesNotThrow();
471     }
472   };
473 }
474 
475 /// EmitAutoVarWithLifetime - Does the setup required for an automatic
476 /// variable with lifetime.
477 static void EmitAutoVarWithLifetime(CodeGenFunction &CGF, const VarDecl &var,
478                                     llvm::Value *addr,
479                                     Qualifiers::ObjCLifetime lifetime) {
480   switch (lifetime) {
481   case Qualifiers::OCL_None:
482     llvm_unreachable("present but none");
483 
484   case Qualifiers::OCL_ExplicitNone:
485     // nothing to do
486     break;
487 
488   case Qualifiers::OCL_Strong: {
489     CodeGenFunction::Destroyer *destroyer =
490       (var.hasAttr<ObjCPreciseLifetimeAttr>()
491        ? CodeGenFunction::destroyARCStrongPrecise
492        : CodeGenFunction::destroyARCStrongImprecise);
493 
494     CleanupKind cleanupKind = CGF.getARCCleanupKind();
495     CGF.pushDestroy(cleanupKind, addr, var.getType(), destroyer,
496                     cleanupKind & EHCleanup);
497     break;
498   }
499   case Qualifiers::OCL_Autoreleasing:
500     // nothing to do
501     break;
502 
503   case Qualifiers::OCL_Weak:
504     // __weak objects always get EH cleanups; otherwise, exceptions
505     // could cause really nasty crashes instead of mere leaks.
506     CGF.pushDestroy(NormalAndEHCleanup, addr, var.getType(),
507                     CodeGenFunction::destroyARCWeak,
508                     /*useEHCleanup*/ true);
509     break;
510   }
511 }
512 
513 static bool isAccessedBy(const VarDecl &var, const Stmt *s) {
514   if (const Expr *e = dyn_cast<Expr>(s)) {
515     // Skip the most common kinds of expressions that make
516     // hierarchy-walking expensive.
517     s = e = e->IgnoreParenCasts();
518 
519     if (const DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e))
520       return (ref->getDecl() == &var);
521     if (const BlockExpr *be = dyn_cast<BlockExpr>(e)) {
522       const BlockDecl *block = be->getBlockDecl();
523       for (BlockDecl::capture_const_iterator i = block->capture_begin(),
524            e = block->capture_end(); i != e; ++i) {
525         if (i->getVariable() == &var)
526           return true;
527       }
528     }
529   }
530 
531   for (Stmt::const_child_range children = s->children(); children; ++children)
532     // children might be null; as in missing decl or conditional of an if-stmt.
533     if ((*children) && isAccessedBy(var, *children))
534       return true;
535 
536   return false;
537 }
538 
539 static bool isAccessedBy(const ValueDecl *decl, const Expr *e) {
540   if (!decl) return false;
541   if (!isa<VarDecl>(decl)) return false;
542   const VarDecl *var = cast<VarDecl>(decl);
543   return isAccessedBy(*var, e);
544 }
545 
546 static void drillIntoBlockVariable(CodeGenFunction &CGF,
547                                    LValue &lvalue,
548                                    const VarDecl *var) {
549   lvalue.setAddress(CGF.BuildBlockByrefAddress(lvalue.getAddress(), var));
550 }
551 
552 void CodeGenFunction::EmitScalarInit(const Expr *init,
553                                      const ValueDecl *D,
554                                      LValue lvalue,
555                                      bool capturedByInit) {
556   Qualifiers::ObjCLifetime lifetime = lvalue.getObjCLifetime();
557   if (!lifetime) {
558     llvm::Value *value = EmitScalarExpr(init);
559     if (capturedByInit)
560       drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
561     EmitStoreThroughLValue(RValue::get(value), lvalue, true);
562     return;
563   }
564 
565   // If we're emitting a value with lifetime, we have to do the
566   // initialization *before* we leave the cleanup scopes.
567   if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(init)) {
568     enterFullExpression(ewc);
569     init = ewc->getSubExpr();
570   }
571   CodeGenFunction::RunCleanupsScope Scope(*this);
572 
573   // We have to maintain the illusion that the variable is
574   // zero-initialized.  If the variable might be accessed in its
575   // initializer, zero-initialize before running the initializer, then
576   // actually perform the initialization with an assign.
577   bool accessedByInit = false;
578   if (lifetime != Qualifiers::OCL_ExplicitNone)
579     accessedByInit = (capturedByInit || isAccessedBy(D, init));
580   if (accessedByInit) {
581     LValue tempLV = lvalue;
582     // Drill down to the __block object if necessary.
583     if (capturedByInit) {
584       // We can use a simple GEP for this because it can't have been
585       // moved yet.
586       tempLV.setAddress(Builder.CreateStructGEP(tempLV.getAddress(),
587                                    getByRefValueLLVMField(cast<VarDecl>(D))));
588     }
589 
590     llvm::PointerType *ty
591       = cast<llvm::PointerType>(tempLV.getAddress()->getType());
592     ty = cast<llvm::PointerType>(ty->getElementType());
593 
594     llvm::Value *zero = llvm::ConstantPointerNull::get(ty);
595 
596     // If __weak, we want to use a barrier under certain conditions.
597     if (lifetime == Qualifiers::OCL_Weak)
598       EmitARCInitWeak(tempLV.getAddress(), zero);
599 
600     // Otherwise just do a simple store.
601     else
602       EmitStoreOfScalar(zero, tempLV, /* isInitialization */ true);
603   }
604 
605   // Emit the initializer.
606   llvm::Value *value = 0;
607 
608   switch (lifetime) {
609   case Qualifiers::OCL_None:
610     llvm_unreachable("present but none");
611 
612   case Qualifiers::OCL_ExplicitNone:
613     // nothing to do
614     value = EmitScalarExpr(init);
615     break;
616 
617   case Qualifiers::OCL_Strong: {
618     value = EmitARCRetainScalarExpr(init);
619     break;
620   }
621 
622   case Qualifiers::OCL_Weak: {
623     // No way to optimize a producing initializer into this.  It's not
624     // worth optimizing for, because the value will immediately
625     // disappear in the common case.
626     value = EmitScalarExpr(init);
627 
628     if (capturedByInit) drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
629     if (accessedByInit)
630       EmitARCStoreWeak(lvalue.getAddress(), value, /*ignored*/ true);
631     else
632       EmitARCInitWeak(lvalue.getAddress(), value);
633     return;
634   }
635 
636   case Qualifiers::OCL_Autoreleasing:
637     value = EmitARCRetainAutoreleaseScalarExpr(init);
638     break;
639   }
640 
641   if (capturedByInit) drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
642 
643   // If the variable might have been accessed by its initializer, we
644   // might have to initialize with a barrier.  We have to do this for
645   // both __weak and __strong, but __weak got filtered out above.
646   if (accessedByInit && lifetime == Qualifiers::OCL_Strong) {
647     llvm::Value *oldValue = EmitLoadOfScalar(lvalue);
648     EmitStoreOfScalar(value, lvalue, /* isInitialization */ true);
649     EmitARCRelease(oldValue, ARCImpreciseLifetime);
650     return;
651   }
652 
653   EmitStoreOfScalar(value, lvalue, /* isInitialization */ true);
654 }
655 
656 /// EmitScalarInit - Initialize the given lvalue with the given object.
657 void CodeGenFunction::EmitScalarInit(llvm::Value *init, LValue lvalue) {
658   Qualifiers::ObjCLifetime lifetime = lvalue.getObjCLifetime();
659   if (!lifetime)
660     return EmitStoreThroughLValue(RValue::get(init), lvalue, true);
661 
662   switch (lifetime) {
663   case Qualifiers::OCL_None:
664     llvm_unreachable("present but none");
665 
666   case Qualifiers::OCL_ExplicitNone:
667     // nothing to do
668     break;
669 
670   case Qualifiers::OCL_Strong:
671     init = EmitARCRetain(lvalue.getType(), init);
672     break;
673 
674   case Qualifiers::OCL_Weak:
675     // Initialize and then skip the primitive store.
676     EmitARCInitWeak(lvalue.getAddress(), init);
677     return;
678 
679   case Qualifiers::OCL_Autoreleasing:
680     init = EmitARCRetainAutorelease(lvalue.getType(), init);
681     break;
682   }
683 
684   EmitStoreOfScalar(init, lvalue, /* isInitialization */ true);
685 }
686 
687 /// canEmitInitWithFewStoresAfterMemset - Decide whether we can emit the
688 /// non-zero parts of the specified initializer with equal or fewer than
689 /// NumStores scalar stores.
690 static bool canEmitInitWithFewStoresAfterMemset(llvm::Constant *Init,
691                                                 unsigned &NumStores) {
692   // Zero and Undef never requires any extra stores.
693   if (isa<llvm::ConstantAggregateZero>(Init) ||
694       isa<llvm::ConstantPointerNull>(Init) ||
695       isa<llvm::UndefValue>(Init))
696     return true;
697   if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) ||
698       isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) ||
699       isa<llvm::ConstantExpr>(Init))
700     return Init->isNullValue() || NumStores--;
701 
702   // See if we can emit each element.
703   if (isa<llvm::ConstantArray>(Init) || isa<llvm::ConstantStruct>(Init)) {
704     for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) {
705       llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i));
706       if (!canEmitInitWithFewStoresAfterMemset(Elt, NumStores))
707         return false;
708     }
709     return true;
710   }
711 
712   if (llvm::ConstantDataSequential *CDS =
713         dyn_cast<llvm::ConstantDataSequential>(Init)) {
714     for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
715       llvm::Constant *Elt = CDS->getElementAsConstant(i);
716       if (!canEmitInitWithFewStoresAfterMemset(Elt, NumStores))
717         return false;
718     }
719     return true;
720   }
721 
722   // Anything else is hard and scary.
723   return false;
724 }
725 
726 /// emitStoresForInitAfterMemset - For inits that
727 /// canEmitInitWithFewStoresAfterMemset returned true for, emit the scalar
728 /// stores that would be required.
729 static void emitStoresForInitAfterMemset(llvm::Constant *Init, llvm::Value *Loc,
730                                          bool isVolatile, CGBuilderTy &Builder) {
731   assert(!Init->isNullValue() && !isa<llvm::UndefValue>(Init) &&
732          "called emitStoresForInitAfterMemset for zero or undef value.");
733 
734   if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) ||
735       isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) ||
736       isa<llvm::ConstantExpr>(Init)) {
737     Builder.CreateStore(Init, Loc, isVolatile);
738     return;
739   }
740 
741   if (llvm::ConstantDataSequential *CDS =
742         dyn_cast<llvm::ConstantDataSequential>(Init)) {
743     for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
744       llvm::Constant *Elt = CDS->getElementAsConstant(i);
745 
746       // If necessary, get a pointer to the element and emit it.
747       if (!Elt->isNullValue() && !isa<llvm::UndefValue>(Elt))
748         emitStoresForInitAfterMemset(Elt, Builder.CreateConstGEP2_32(Loc, 0, i),
749                                      isVolatile, Builder);
750     }
751     return;
752   }
753 
754   assert((isa<llvm::ConstantStruct>(Init) || isa<llvm::ConstantArray>(Init)) &&
755          "Unknown value type!");
756 
757   for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) {
758     llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i));
759 
760     // If necessary, get a pointer to the element and emit it.
761     if (!Elt->isNullValue() && !isa<llvm::UndefValue>(Elt))
762       emitStoresForInitAfterMemset(Elt, Builder.CreateConstGEP2_32(Loc, 0, i),
763                                    isVolatile, Builder);
764   }
765 }
766 
767 
768 /// shouldUseMemSetPlusStoresToInitialize - Decide whether we should use memset
769 /// plus some stores to initialize a local variable instead of using a memcpy
770 /// from a constant global.  It is beneficial to use memset if the global is all
771 /// zeros, or mostly zeros and large.
772 static bool shouldUseMemSetPlusStoresToInitialize(llvm::Constant *Init,
773                                                   uint64_t GlobalSize) {
774   // If a global is all zeros, always use a memset.
775   if (isa<llvm::ConstantAggregateZero>(Init)) return true;
776 
777   // If a non-zero global is <= 32 bytes, always use a memcpy.  If it is large,
778   // do it if it will require 6 or fewer scalar stores.
779   // TODO: Should budget depends on the size?  Avoiding a large global warrants
780   // plopping in more stores.
781   unsigned StoreBudget = 6;
782   uint64_t SizeLimit = 32;
783 
784   return GlobalSize > SizeLimit &&
785          canEmitInitWithFewStoresAfterMemset(Init, StoreBudget);
786 }
787 
788 /// Should we use the LLVM lifetime intrinsics for the given local variable?
789 static bool shouldUseLifetimeMarkers(CodeGenFunction &CGF, const VarDecl &D,
790                                      unsigned Size) {
791   // Always emit lifetime markers in -fsanitize=use-after-scope mode.
792   if (CGF.getLangOpts().Sanitize.UseAfterScope)
793     return true;
794   // For now, only in optimized builds.
795   if (CGF.CGM.getCodeGenOpts().OptimizationLevel == 0)
796     return false;
797 
798   // Limit the size of marked objects to 32 bytes. We don't want to increase
799   // compile time by marking tiny objects.
800   unsigned SizeThreshold = 32;
801 
802   return Size > SizeThreshold;
803 }
804 
805 
806 /// EmitAutoVarDecl - Emit code and set up an entry in LocalDeclMap for a
807 /// variable declaration with auto, register, or no storage class specifier.
808 /// These turn into simple stack objects, or GlobalValues depending on target.
809 void CodeGenFunction::EmitAutoVarDecl(const VarDecl &D) {
810   AutoVarEmission emission = EmitAutoVarAlloca(D);
811   EmitAutoVarInit(emission);
812   EmitAutoVarCleanups(emission);
813 }
814 
815 /// EmitAutoVarAlloca - Emit the alloca and debug information for a
816 /// local variable.  Does not emit initalization or destruction.
817 CodeGenFunction::AutoVarEmission
818 CodeGenFunction::EmitAutoVarAlloca(const VarDecl &D) {
819   QualType Ty = D.getType();
820 
821   AutoVarEmission emission(D);
822 
823   bool isByRef = D.hasAttr<BlocksAttr>();
824   emission.IsByRef = isByRef;
825 
826   CharUnits alignment = getContext().getDeclAlign(&D);
827   emission.Alignment = alignment;
828 
829   // If the type is variably-modified, emit all the VLA sizes for it.
830   if (Ty->isVariablyModifiedType())
831     EmitVariablyModifiedType(Ty);
832 
833   llvm::Value *DeclPtr;
834   if (Ty->isConstantSizeType()) {
835     bool NRVO = getLangOpts().ElideConstructors &&
836       D.isNRVOVariable();
837 
838     // If this value is a POD array or struct with a statically
839     // determinable constant initializer, there are optimizations we can do.
840     //
841     // TODO: We should constant-evaluate the initializer of any variable,
842     // as long as it is initialized by a constant expression. Currently,
843     // isConstantInitializer produces wrong answers for structs with
844     // reference or bitfield members, and a few other cases, and checking
845     // for POD-ness protects us from some of these.
846     if (D.getInit() &&
847         (Ty->isArrayType() || Ty->isRecordType()) &&
848         (Ty.isPODType(getContext()) ||
849          getContext().getBaseElementType(Ty)->isObjCObjectPointerType()) &&
850         D.getInit()->isConstantInitializer(getContext(), false)) {
851 
852       // If the variable's a const type, and it's neither an NRVO
853       // candidate nor a __block variable and has no mutable members,
854       // emit it as a global instead.
855       if (CGM.getCodeGenOpts().MergeAllConstants && !NRVO && !isByRef &&
856           CGM.isTypeConstant(Ty, true)) {
857         EmitStaticVarDecl(D, llvm::GlobalValue::InternalLinkage);
858 
859         emission.Address = 0; // signal this condition to later callbacks
860         assert(emission.wasEmittedAsGlobal());
861         return emission;
862       }
863 
864       // Otherwise, tell the initialization code that we're in this case.
865       emission.IsConstantAggregate = true;
866     }
867 
868     // A normal fixed sized variable becomes an alloca in the entry block,
869     // unless it's an NRVO variable.
870     llvm::Type *LTy = ConvertTypeForMem(Ty);
871 
872     if (NRVO) {
873       // The named return value optimization: allocate this variable in the
874       // return slot, so that we can elide the copy when returning this
875       // variable (C++0x [class.copy]p34).
876       DeclPtr = ReturnValue;
877 
878       if (const RecordType *RecordTy = Ty->getAs<RecordType>()) {
879         if (!cast<CXXRecordDecl>(RecordTy->getDecl())->hasTrivialDestructor()) {
880           // Create a flag that is used to indicate when the NRVO was applied
881           // to this variable. Set it to zero to indicate that NRVO was not
882           // applied.
883           llvm::Value *Zero = Builder.getFalse();
884           llvm::Value *NRVOFlag = CreateTempAlloca(Zero->getType(), "nrvo");
885           EnsureInsertPoint();
886           Builder.CreateStore(Zero, NRVOFlag);
887 
888           // Record the NRVO flag for this variable.
889           NRVOFlags[&D] = NRVOFlag;
890           emission.NRVOFlag = NRVOFlag;
891         }
892       }
893     } else {
894       if (isByRef)
895         LTy = BuildByRefType(&D);
896 
897       llvm::AllocaInst *Alloc = CreateTempAlloca(LTy);
898       Alloc->setName(D.getName());
899 
900       CharUnits allocaAlignment = alignment;
901       if (isByRef)
902         allocaAlignment = std::max(allocaAlignment,
903             getContext().toCharUnitsFromBits(getTarget().getPointerAlign(0)));
904       Alloc->setAlignment(allocaAlignment.getQuantity());
905       DeclPtr = Alloc;
906 
907       // Emit a lifetime intrinsic if meaningful.  There's no point
908       // in doing this if we don't have a valid insertion point (?).
909       uint64_t size = CGM.getDataLayout().getTypeAllocSize(LTy);
910       if (HaveInsertPoint() && shouldUseLifetimeMarkers(*this, D, size)) {
911         llvm::Value *sizeV = llvm::ConstantInt::get(Int64Ty, size);
912 
913         emission.SizeForLifetimeMarkers = sizeV;
914         llvm::Value *castAddr = Builder.CreateBitCast(Alloc, Int8PtrTy);
915         Builder.CreateCall2(CGM.getLLVMLifetimeStartFn(), sizeV, castAddr)
916           ->setDoesNotThrow();
917       } else {
918         assert(!emission.useLifetimeMarkers());
919       }
920     }
921   } else {
922     EnsureInsertPoint();
923 
924     if (!DidCallStackSave) {
925       // Save the stack.
926       llvm::Value *Stack = CreateTempAlloca(Int8PtrTy, "saved_stack");
927 
928       llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave);
929       llvm::Value *V = Builder.CreateCall(F);
930 
931       Builder.CreateStore(V, Stack);
932 
933       DidCallStackSave = true;
934 
935       // Push a cleanup block and restore the stack there.
936       // FIXME: in general circumstances, this should be an EH cleanup.
937       EHStack.pushCleanup<CallStackRestore>(NormalCleanup, Stack);
938     }
939 
940     llvm::Value *elementCount;
941     QualType elementType;
942     llvm::tie(elementCount, elementType) = getVLASize(Ty);
943 
944     llvm::Type *llvmTy = ConvertTypeForMem(elementType);
945 
946     // Allocate memory for the array.
947     llvm::AllocaInst *vla = Builder.CreateAlloca(llvmTy, elementCount, "vla");
948     vla->setAlignment(alignment.getQuantity());
949 
950     DeclPtr = vla;
951   }
952 
953   llvm::Value *&DMEntry = LocalDeclMap[&D];
954   assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
955   DMEntry = DeclPtr;
956   emission.Address = DeclPtr;
957 
958   // Emit debug info for local var declaration.
959   if (HaveInsertPoint())
960     if (CGDebugInfo *DI = getDebugInfo()) {
961       if (CGM.getCodeGenOpts().getDebugInfo()
962             >= CodeGenOptions::LimitedDebugInfo) {
963         DI->setLocation(D.getLocation());
964         DI->EmitDeclareOfAutoVariable(&D, DeclPtr, Builder);
965       }
966     }
967 
968   if (D.hasAttr<AnnotateAttr>())
969       EmitVarAnnotations(&D, emission.Address);
970 
971   return emission;
972 }
973 
974 /// Determines whether the given __block variable is potentially
975 /// captured by the given expression.
976 static bool isCapturedBy(const VarDecl &var, const Expr *e) {
977   // Skip the most common kinds of expressions that make
978   // hierarchy-walking expensive.
979   e = e->IgnoreParenCasts();
980 
981   if (const BlockExpr *be = dyn_cast<BlockExpr>(e)) {
982     const BlockDecl *block = be->getBlockDecl();
983     for (BlockDecl::capture_const_iterator i = block->capture_begin(),
984            e = block->capture_end(); i != e; ++i) {
985       if (i->getVariable() == &var)
986         return true;
987     }
988 
989     // No need to walk into the subexpressions.
990     return false;
991   }
992 
993   if (const StmtExpr *SE = dyn_cast<StmtExpr>(e)) {
994     const CompoundStmt *CS = SE->getSubStmt();
995     for (CompoundStmt::const_body_iterator BI = CS->body_begin(),
996 	   BE = CS->body_end(); BI != BE; ++BI)
997       if (Expr *E = dyn_cast<Expr>((*BI))) {
998         if (isCapturedBy(var, E))
999             return true;
1000       }
1001       else if (DeclStmt *DS = dyn_cast<DeclStmt>((*BI))) {
1002           // special case declarations
1003           for (DeclStmt::decl_iterator I = DS->decl_begin(), E = DS->decl_end();
1004                I != E; ++I) {
1005               if (VarDecl *VD = dyn_cast<VarDecl>((*I))) {
1006                 Expr *Init = VD->getInit();
1007                 if (Init && isCapturedBy(var, Init))
1008                   return true;
1009               }
1010           }
1011       }
1012       else
1013         // FIXME. Make safe assumption assuming arbitrary statements cause capturing.
1014         // Later, provide code to poke into statements for capture analysis.
1015         return true;
1016     return false;
1017   }
1018 
1019   for (Stmt::const_child_range children = e->children(); children; ++children)
1020     if (isCapturedBy(var, cast<Expr>(*children)))
1021       return true;
1022 
1023   return false;
1024 }
1025 
1026 /// \brief Determine whether the given initializer is trivial in the sense
1027 /// that it requires no code to be generated.
1028 static bool isTrivialInitializer(const Expr *Init) {
1029   if (!Init)
1030     return true;
1031 
1032   if (const CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init))
1033     if (CXXConstructorDecl *Constructor = Construct->getConstructor())
1034       if (Constructor->isTrivial() &&
1035           Constructor->isDefaultConstructor() &&
1036           !Construct->requiresZeroInitialization())
1037         return true;
1038 
1039   return false;
1040 }
1041 void CodeGenFunction::EmitAutoVarInit(const AutoVarEmission &emission) {
1042   assert(emission.Variable && "emission was not valid!");
1043 
1044   // If this was emitted as a global constant, we're done.
1045   if (emission.wasEmittedAsGlobal()) return;
1046 
1047   const VarDecl &D = *emission.Variable;
1048   QualType type = D.getType();
1049 
1050   // If this local has an initializer, emit it now.
1051   const Expr *Init = D.getInit();
1052 
1053   // If we are at an unreachable point, we don't need to emit the initializer
1054   // unless it contains a label.
1055   if (!HaveInsertPoint()) {
1056     if (!Init || !ContainsLabel(Init)) return;
1057     EnsureInsertPoint();
1058   }
1059 
1060   // Initialize the structure of a __block variable.
1061   if (emission.IsByRef)
1062     emitByrefStructureInit(emission);
1063 
1064   if (isTrivialInitializer(Init))
1065     return;
1066 
1067   CharUnits alignment = emission.Alignment;
1068 
1069   // Check whether this is a byref variable that's potentially
1070   // captured and moved by its own initializer.  If so, we'll need to
1071   // emit the initializer first, then copy into the variable.
1072   bool capturedByInit = emission.IsByRef && isCapturedBy(D, Init);
1073 
1074   llvm::Value *Loc =
1075     capturedByInit ? emission.Address : emission.getObjectAddress(*this);
1076 
1077   llvm::Constant *constant = 0;
1078   if (emission.IsConstantAggregate) {
1079     assert(!capturedByInit && "constant init contains a capturing block?");
1080     constant = CGM.EmitConstantInit(D, this);
1081   }
1082 
1083   if (!constant) {
1084     LValue lv = MakeAddrLValue(Loc, type, alignment);
1085     lv.setNonGC(true);
1086     return EmitExprAsInit(Init, &D, lv, capturedByInit);
1087   }
1088 
1089   // If this is a simple aggregate initialization, we can optimize it
1090   // in various ways.
1091   bool isVolatile = type.isVolatileQualified();
1092 
1093   llvm::Value *SizeVal =
1094     llvm::ConstantInt::get(IntPtrTy,
1095                            getContext().getTypeSizeInChars(type).getQuantity());
1096 
1097   llvm::Type *BP = Int8PtrTy;
1098   if (Loc->getType() != BP)
1099     Loc = Builder.CreateBitCast(Loc, BP);
1100 
1101   // If the initializer is all or mostly zeros, codegen with memset then do
1102   // a few stores afterward.
1103   if (shouldUseMemSetPlusStoresToInitialize(constant,
1104                 CGM.getDataLayout().getTypeAllocSize(constant->getType()))) {
1105     Builder.CreateMemSet(Loc, llvm::ConstantInt::get(Int8Ty, 0), SizeVal,
1106                          alignment.getQuantity(), isVolatile);
1107     // Zero and undef don't require a stores.
1108     if (!constant->isNullValue() && !isa<llvm::UndefValue>(constant)) {
1109       Loc = Builder.CreateBitCast(Loc, constant->getType()->getPointerTo());
1110       emitStoresForInitAfterMemset(constant, Loc, isVolatile, Builder);
1111     }
1112   } else {
1113     // Otherwise, create a temporary global with the initializer then
1114     // memcpy from the global to the alloca.
1115     std::string Name = GetStaticDeclName(*this, D, ".");
1116     llvm::GlobalVariable *GV =
1117       new llvm::GlobalVariable(CGM.getModule(), constant->getType(), true,
1118                                llvm::GlobalValue::PrivateLinkage,
1119                                constant, Name);
1120     GV->setAlignment(alignment.getQuantity());
1121     GV->setUnnamedAddr(true);
1122 
1123     llvm::Value *SrcPtr = GV;
1124     if (SrcPtr->getType() != BP)
1125       SrcPtr = Builder.CreateBitCast(SrcPtr, BP);
1126 
1127     Builder.CreateMemCpy(Loc, SrcPtr, SizeVal, alignment.getQuantity(),
1128                          isVolatile);
1129   }
1130 }
1131 
1132 /// Emit an expression as an initializer for a variable at the given
1133 /// location.  The expression is not necessarily the normal
1134 /// initializer for the variable, and the address is not necessarily
1135 /// its normal location.
1136 ///
1137 /// \param init the initializing expression
1138 /// \param var the variable to act as if we're initializing
1139 /// \param loc the address to initialize; its type is a pointer
1140 ///   to the LLVM mapping of the variable's type
1141 /// \param alignment the alignment of the address
1142 /// \param capturedByInit true if the variable is a __block variable
1143 ///   whose address is potentially changed by the initializer
1144 void CodeGenFunction::EmitExprAsInit(const Expr *init,
1145                                      const ValueDecl *D,
1146                                      LValue lvalue,
1147                                      bool capturedByInit) {
1148   QualType type = D->getType();
1149 
1150   if (type->isReferenceType()) {
1151     RValue rvalue = EmitReferenceBindingToExpr(init, D);
1152     if (capturedByInit)
1153       drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
1154     EmitStoreThroughLValue(rvalue, lvalue, true);
1155     return;
1156   }
1157   switch (getEvaluationKind(type)) {
1158   case TEK_Scalar:
1159     EmitScalarInit(init, D, lvalue, capturedByInit);
1160     return;
1161   case TEK_Complex: {
1162     ComplexPairTy complex = EmitComplexExpr(init);
1163     if (capturedByInit)
1164       drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
1165     EmitStoreOfComplex(complex, lvalue, /*init*/ true);
1166     return;
1167   }
1168   case TEK_Aggregate:
1169     if (type->isAtomicType()) {
1170       EmitAtomicInit(const_cast<Expr*>(init), lvalue);
1171     } else {
1172       // TODO: how can we delay here if D is captured by its initializer?
1173       EmitAggExpr(init, AggValueSlot::forLValue(lvalue,
1174                                               AggValueSlot::IsDestructed,
1175                                          AggValueSlot::DoesNotNeedGCBarriers,
1176                                               AggValueSlot::IsNotAliased));
1177     }
1178     MaybeEmitStdInitializerListCleanup(lvalue.getAddress(), init);
1179     return;
1180   }
1181   llvm_unreachable("bad evaluation kind");
1182 }
1183 
1184 /// Enter a destroy cleanup for the given local variable.
1185 void CodeGenFunction::emitAutoVarTypeCleanup(
1186                             const CodeGenFunction::AutoVarEmission &emission,
1187                             QualType::DestructionKind dtorKind) {
1188   assert(dtorKind != QualType::DK_none);
1189 
1190   // Note that for __block variables, we want to destroy the
1191   // original stack object, not the possibly forwarded object.
1192   llvm::Value *addr = emission.getObjectAddress(*this);
1193 
1194   const VarDecl *var = emission.Variable;
1195   QualType type = var->getType();
1196 
1197   CleanupKind cleanupKind = NormalAndEHCleanup;
1198   CodeGenFunction::Destroyer *destroyer = 0;
1199 
1200   switch (dtorKind) {
1201   case QualType::DK_none:
1202     llvm_unreachable("no cleanup for trivially-destructible variable");
1203 
1204   case QualType::DK_cxx_destructor:
1205     // If there's an NRVO flag on the emission, we need a different
1206     // cleanup.
1207     if (emission.NRVOFlag) {
1208       assert(!type->isArrayType());
1209       CXXDestructorDecl *dtor = type->getAsCXXRecordDecl()->getDestructor();
1210       EHStack.pushCleanup<DestroyNRVOVariable>(cleanupKind, addr, dtor,
1211                                                emission.NRVOFlag);
1212       return;
1213     }
1214     break;
1215 
1216   case QualType::DK_objc_strong_lifetime:
1217     // Suppress cleanups for pseudo-strong variables.
1218     if (var->isARCPseudoStrong()) return;
1219 
1220     // Otherwise, consider whether to use an EH cleanup or not.
1221     cleanupKind = getARCCleanupKind();
1222 
1223     // Use the imprecise destroyer by default.
1224     if (!var->hasAttr<ObjCPreciseLifetimeAttr>())
1225       destroyer = CodeGenFunction::destroyARCStrongImprecise;
1226     break;
1227 
1228   case QualType::DK_objc_weak_lifetime:
1229     break;
1230   }
1231 
1232   // If we haven't chosen a more specific destroyer, use the default.
1233   if (!destroyer) destroyer = getDestroyer(dtorKind);
1234 
1235   // Use an EH cleanup in array destructors iff the destructor itself
1236   // is being pushed as an EH cleanup.
1237   bool useEHCleanup = (cleanupKind & EHCleanup);
1238   EHStack.pushCleanup<DestroyObject>(cleanupKind, addr, type, destroyer,
1239                                      useEHCleanup);
1240 }
1241 
1242 void CodeGenFunction::EmitAutoVarCleanups(const AutoVarEmission &emission) {
1243   assert(emission.Variable && "emission was not valid!");
1244 
1245   // If this was emitted as a global constant, we're done.
1246   if (emission.wasEmittedAsGlobal()) return;
1247 
1248   // If we don't have an insertion point, we're done.  Sema prevents
1249   // us from jumping into any of these scopes anyway.
1250   if (!HaveInsertPoint()) return;
1251 
1252   const VarDecl &D = *emission.Variable;
1253 
1254   // Make sure we call @llvm.lifetime.end.  This needs to happen
1255   // *last*, so the cleanup needs to be pushed *first*.
1256   if (emission.useLifetimeMarkers()) {
1257     EHStack.pushCleanup<CallLifetimeEnd>(NormalCleanup,
1258                                          emission.getAllocatedAddress(),
1259                                          emission.getSizeForLifetimeMarkers());
1260   }
1261 
1262   // Check the type for a cleanup.
1263   if (QualType::DestructionKind dtorKind = D.getType().isDestructedType())
1264     emitAutoVarTypeCleanup(emission, dtorKind);
1265 
1266   // In GC mode, honor objc_precise_lifetime.
1267   if (getLangOpts().getGC() != LangOptions::NonGC &&
1268       D.hasAttr<ObjCPreciseLifetimeAttr>()) {
1269     EHStack.pushCleanup<ExtendGCLifetime>(NormalCleanup, &D);
1270   }
1271 
1272   // Handle the cleanup attribute.
1273   if (const CleanupAttr *CA = D.getAttr<CleanupAttr>()) {
1274     const FunctionDecl *FD = CA->getFunctionDecl();
1275 
1276     llvm::Constant *F = CGM.GetAddrOfFunction(FD);
1277     assert(F && "Could not find function!");
1278 
1279     const CGFunctionInfo &Info = CGM.getTypes().arrangeFunctionDeclaration(FD);
1280     EHStack.pushCleanup<CallCleanupFunction>(NormalAndEHCleanup, F, &Info, &D);
1281   }
1282 
1283   // If this is a block variable, call _Block_object_destroy
1284   // (on the unforwarded address).
1285   if (emission.IsByRef)
1286     enterByrefCleanup(emission);
1287 }
1288 
1289 CodeGenFunction::Destroyer *
1290 CodeGenFunction::getDestroyer(QualType::DestructionKind kind) {
1291   switch (kind) {
1292   case QualType::DK_none: llvm_unreachable("no destroyer for trivial dtor");
1293   case QualType::DK_cxx_destructor:
1294     return destroyCXXObject;
1295   case QualType::DK_objc_strong_lifetime:
1296     return destroyARCStrongPrecise;
1297   case QualType::DK_objc_weak_lifetime:
1298     return destroyARCWeak;
1299   }
1300   llvm_unreachable("Unknown DestructionKind");
1301 }
1302 
1303 /// pushEHDestroy - Push the standard destructor for the given type as
1304 /// an EH-only cleanup.
1305 void CodeGenFunction::pushEHDestroy(QualType::DestructionKind dtorKind,
1306                                   llvm::Value *addr, QualType type) {
1307   assert(dtorKind && "cannot push destructor for trivial type");
1308   assert(needsEHCleanup(dtorKind));
1309 
1310   pushDestroy(EHCleanup, addr, type, getDestroyer(dtorKind), true);
1311 }
1312 
1313 /// pushDestroy - Push the standard destructor for the given type as
1314 /// at least a normal cleanup.
1315 void CodeGenFunction::pushDestroy(QualType::DestructionKind dtorKind,
1316                                   llvm::Value *addr, QualType type) {
1317   assert(dtorKind && "cannot push destructor for trivial type");
1318 
1319   CleanupKind cleanupKind = getCleanupKind(dtorKind);
1320   pushDestroy(cleanupKind, addr, type, getDestroyer(dtorKind),
1321               cleanupKind & EHCleanup);
1322 }
1323 
1324 void CodeGenFunction::pushDestroy(CleanupKind cleanupKind, llvm::Value *addr,
1325                                   QualType type, Destroyer *destroyer,
1326                                   bool useEHCleanupForArray) {
1327   pushFullExprCleanup<DestroyObject>(cleanupKind, addr, type,
1328                                      destroyer, useEHCleanupForArray);
1329 }
1330 
1331 /// emitDestroy - Immediately perform the destruction of the given
1332 /// object.
1333 ///
1334 /// \param addr - the address of the object; a type*
1335 /// \param type - the type of the object; if an array type, all
1336 ///   objects are destroyed in reverse order
1337 /// \param destroyer - the function to call to destroy individual
1338 ///   elements
1339 /// \param useEHCleanupForArray - whether an EH cleanup should be
1340 ///   used when destroying array elements, in case one of the
1341 ///   destructions throws an exception
1342 void CodeGenFunction::emitDestroy(llvm::Value *addr, QualType type,
1343                                   Destroyer *destroyer,
1344                                   bool useEHCleanupForArray) {
1345   const ArrayType *arrayType = getContext().getAsArrayType(type);
1346   if (!arrayType)
1347     return destroyer(*this, addr, type);
1348 
1349   llvm::Value *begin = addr;
1350   llvm::Value *length = emitArrayLength(arrayType, type, begin);
1351 
1352   // Normally we have to check whether the array is zero-length.
1353   bool checkZeroLength = true;
1354 
1355   // But if the array length is constant, we can suppress that.
1356   if (llvm::ConstantInt *constLength = dyn_cast<llvm::ConstantInt>(length)) {
1357     // ...and if it's constant zero, we can just skip the entire thing.
1358     if (constLength->isZero()) return;
1359     checkZeroLength = false;
1360   }
1361 
1362   llvm::Value *end = Builder.CreateInBoundsGEP(begin, length);
1363   emitArrayDestroy(begin, end, type, destroyer,
1364                    checkZeroLength, useEHCleanupForArray);
1365 }
1366 
1367 /// emitArrayDestroy - Destroys all the elements of the given array,
1368 /// beginning from last to first.  The array cannot be zero-length.
1369 ///
1370 /// \param begin - a type* denoting the first element of the array
1371 /// \param end - a type* denoting one past the end of the array
1372 /// \param type - the element type of the array
1373 /// \param destroyer - the function to call to destroy elements
1374 /// \param useEHCleanup - whether to push an EH cleanup to destroy
1375 ///   the remaining elements in case the destruction of a single
1376 ///   element throws
1377 void CodeGenFunction::emitArrayDestroy(llvm::Value *begin,
1378                                        llvm::Value *end,
1379                                        QualType type,
1380                                        Destroyer *destroyer,
1381                                        bool checkZeroLength,
1382                                        bool useEHCleanup) {
1383   assert(!type->isArrayType());
1384 
1385   // The basic structure here is a do-while loop, because we don't
1386   // need to check for the zero-element case.
1387   llvm::BasicBlock *bodyBB = createBasicBlock("arraydestroy.body");
1388   llvm::BasicBlock *doneBB = createBasicBlock("arraydestroy.done");
1389 
1390   if (checkZeroLength) {
1391     llvm::Value *isEmpty = Builder.CreateICmpEQ(begin, end,
1392                                                 "arraydestroy.isempty");
1393     Builder.CreateCondBr(isEmpty, doneBB, bodyBB);
1394   }
1395 
1396   // Enter the loop body, making that address the current address.
1397   llvm::BasicBlock *entryBB = Builder.GetInsertBlock();
1398   EmitBlock(bodyBB);
1399   llvm::PHINode *elementPast =
1400     Builder.CreatePHI(begin->getType(), 2, "arraydestroy.elementPast");
1401   elementPast->addIncoming(end, entryBB);
1402 
1403   // Shift the address back by one element.
1404   llvm::Value *negativeOne = llvm::ConstantInt::get(SizeTy, -1, true);
1405   llvm::Value *element = Builder.CreateInBoundsGEP(elementPast, negativeOne,
1406                                                    "arraydestroy.element");
1407 
1408   if (useEHCleanup)
1409     pushRegularPartialArrayCleanup(begin, element, type, destroyer);
1410 
1411   // Perform the actual destruction there.
1412   destroyer(*this, element, type);
1413 
1414   if (useEHCleanup)
1415     PopCleanupBlock();
1416 
1417   // Check whether we've reached the end.
1418   llvm::Value *done = Builder.CreateICmpEQ(element, begin, "arraydestroy.done");
1419   Builder.CreateCondBr(done, doneBB, bodyBB);
1420   elementPast->addIncoming(element, Builder.GetInsertBlock());
1421 
1422   // Done.
1423   EmitBlock(doneBB);
1424 }
1425 
1426 /// Perform partial array destruction as if in an EH cleanup.  Unlike
1427 /// emitArrayDestroy, the element type here may still be an array type.
1428 static void emitPartialArrayDestroy(CodeGenFunction &CGF,
1429                                     llvm::Value *begin, llvm::Value *end,
1430                                     QualType type,
1431                                     CodeGenFunction::Destroyer *destroyer) {
1432   // If the element type is itself an array, drill down.
1433   unsigned arrayDepth = 0;
1434   while (const ArrayType *arrayType = CGF.getContext().getAsArrayType(type)) {
1435     // VLAs don't require a GEP index to walk into.
1436     if (!isa<VariableArrayType>(arrayType))
1437       arrayDepth++;
1438     type = arrayType->getElementType();
1439   }
1440 
1441   if (arrayDepth) {
1442     llvm::Value *zero = llvm::ConstantInt::get(CGF.SizeTy, arrayDepth+1);
1443 
1444     SmallVector<llvm::Value*,4> gepIndices(arrayDepth, zero);
1445     begin = CGF.Builder.CreateInBoundsGEP(begin, gepIndices, "pad.arraybegin");
1446     end = CGF.Builder.CreateInBoundsGEP(end, gepIndices, "pad.arrayend");
1447   }
1448 
1449   // Destroy the array.  We don't ever need an EH cleanup because we
1450   // assume that we're in an EH cleanup ourselves, so a throwing
1451   // destructor causes an immediate terminate.
1452   CGF.emitArrayDestroy(begin, end, type, destroyer,
1453                        /*checkZeroLength*/ true, /*useEHCleanup*/ false);
1454 }
1455 
1456 namespace {
1457   /// RegularPartialArrayDestroy - a cleanup which performs a partial
1458   /// array destroy where the end pointer is regularly determined and
1459   /// does not need to be loaded from a local.
1460   class RegularPartialArrayDestroy : public EHScopeStack::Cleanup {
1461     llvm::Value *ArrayBegin;
1462     llvm::Value *ArrayEnd;
1463     QualType ElementType;
1464     CodeGenFunction::Destroyer *Destroyer;
1465   public:
1466     RegularPartialArrayDestroy(llvm::Value *arrayBegin, llvm::Value *arrayEnd,
1467                                QualType elementType,
1468                                CodeGenFunction::Destroyer *destroyer)
1469       : ArrayBegin(arrayBegin), ArrayEnd(arrayEnd),
1470         ElementType(elementType), Destroyer(destroyer) {}
1471 
1472     void Emit(CodeGenFunction &CGF, Flags flags) {
1473       emitPartialArrayDestroy(CGF, ArrayBegin, ArrayEnd,
1474                               ElementType, Destroyer);
1475     }
1476   };
1477 
1478   /// IrregularPartialArrayDestroy - a cleanup which performs a
1479   /// partial array destroy where the end pointer is irregularly
1480   /// determined and must be loaded from a local.
1481   class IrregularPartialArrayDestroy : public EHScopeStack::Cleanup {
1482     llvm::Value *ArrayBegin;
1483     llvm::Value *ArrayEndPointer;
1484     QualType ElementType;
1485     CodeGenFunction::Destroyer *Destroyer;
1486   public:
1487     IrregularPartialArrayDestroy(llvm::Value *arrayBegin,
1488                                  llvm::Value *arrayEndPointer,
1489                                  QualType elementType,
1490                                  CodeGenFunction::Destroyer *destroyer)
1491       : ArrayBegin(arrayBegin), ArrayEndPointer(arrayEndPointer),
1492         ElementType(elementType), Destroyer(destroyer) {}
1493 
1494     void Emit(CodeGenFunction &CGF, Flags flags) {
1495       llvm::Value *arrayEnd = CGF.Builder.CreateLoad(ArrayEndPointer);
1496       emitPartialArrayDestroy(CGF, ArrayBegin, arrayEnd,
1497                               ElementType, Destroyer);
1498     }
1499   };
1500 }
1501 
1502 /// pushIrregularPartialArrayCleanup - Push an EH cleanup to destroy
1503 /// already-constructed elements of the given array.  The cleanup
1504 /// may be popped with DeactivateCleanupBlock or PopCleanupBlock.
1505 ///
1506 /// \param elementType - the immediate element type of the array;
1507 ///   possibly still an array type
1508 void CodeGenFunction::pushIrregularPartialArrayCleanup(llvm::Value *arrayBegin,
1509                                                  llvm::Value *arrayEndPointer,
1510                                                        QualType elementType,
1511                                                        Destroyer *destroyer) {
1512   pushFullExprCleanup<IrregularPartialArrayDestroy>(EHCleanup,
1513                                                     arrayBegin, arrayEndPointer,
1514                                                     elementType, destroyer);
1515 }
1516 
1517 /// pushRegularPartialArrayCleanup - Push an EH cleanup to destroy
1518 /// already-constructed elements of the given array.  The cleanup
1519 /// may be popped with DeactivateCleanupBlock or PopCleanupBlock.
1520 ///
1521 /// \param elementType - the immediate element type of the array;
1522 ///   possibly still an array type
1523 void CodeGenFunction::pushRegularPartialArrayCleanup(llvm::Value *arrayBegin,
1524                                                      llvm::Value *arrayEnd,
1525                                                      QualType elementType,
1526                                                      Destroyer *destroyer) {
1527   pushFullExprCleanup<RegularPartialArrayDestroy>(EHCleanup,
1528                                                   arrayBegin, arrayEnd,
1529                                                   elementType, destroyer);
1530 }
1531 
1532 /// Lazily declare the @llvm.lifetime.start intrinsic.
1533 llvm::Constant *CodeGenModule::getLLVMLifetimeStartFn() {
1534   if (LifetimeStartFn) return LifetimeStartFn;
1535   LifetimeStartFn = llvm::Intrinsic::getDeclaration(&getModule(),
1536                                             llvm::Intrinsic::lifetime_start);
1537   return LifetimeStartFn;
1538 }
1539 
1540 /// Lazily declare the @llvm.lifetime.end intrinsic.
1541 llvm::Constant *CodeGenModule::getLLVMLifetimeEndFn() {
1542   if (LifetimeEndFn) return LifetimeEndFn;
1543   LifetimeEndFn = llvm::Intrinsic::getDeclaration(&getModule(),
1544                                               llvm::Intrinsic::lifetime_end);
1545   return LifetimeEndFn;
1546 }
1547 
1548 namespace {
1549   /// A cleanup to perform a release of an object at the end of a
1550   /// function.  This is used to balance out the incoming +1 of a
1551   /// ns_consumed argument when we can't reasonably do that just by
1552   /// not doing the initial retain for a __block argument.
1553   struct ConsumeARCParameter : EHScopeStack::Cleanup {
1554     ConsumeARCParameter(llvm::Value *param,
1555                         ARCPreciseLifetime_t precise)
1556       : Param(param), Precise(precise) {}
1557 
1558     llvm::Value *Param;
1559     ARCPreciseLifetime_t Precise;
1560 
1561     void Emit(CodeGenFunction &CGF, Flags flags) {
1562       CGF.EmitARCRelease(Param, Precise);
1563     }
1564   };
1565 }
1566 
1567 /// Emit an alloca (or GlobalValue depending on target)
1568 /// for the specified parameter and set up LocalDeclMap.
1569 void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg,
1570                                    unsigned ArgNo) {
1571   // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl?
1572   assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) &&
1573          "Invalid argument to EmitParmDecl");
1574 
1575   Arg->setName(D.getName());
1576 
1577   QualType Ty = D.getType();
1578 
1579   // Use better IR generation for certain implicit parameters.
1580   if (isa<ImplicitParamDecl>(D)) {
1581     // The only implicit argument a block has is its literal.
1582     if (BlockInfo) {
1583       LocalDeclMap[&D] = Arg;
1584       llvm::Value *LocalAddr = 0;
1585       if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
1586         // Allocate a stack slot to let the debug info survive the RA.
1587         llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertTypeForMem(Ty),
1588                                                    D.getName() + ".addr");
1589         Alloc->setAlignment(getContext().getDeclAlign(&D).getQuantity());
1590         LValue lv = MakeAddrLValue(Alloc, Ty, getContext().getDeclAlign(&D));
1591         EmitStoreOfScalar(Arg, lv, /* isInitialization */ true);
1592         LocalAddr = Builder.CreateLoad(Alloc);
1593       }
1594 
1595       if (CGDebugInfo *DI = getDebugInfo()) {
1596         if (CGM.getCodeGenOpts().getDebugInfo()
1597               >= CodeGenOptions::LimitedDebugInfo) {
1598           DI->setLocation(D.getLocation());
1599           DI->EmitDeclareOfBlockLiteralArgVariable(*BlockInfo, Arg, LocalAddr, Builder);
1600         }
1601       }
1602 
1603       return;
1604     }
1605   }
1606 
1607   llvm::Value *DeclPtr;
1608   // If this is an aggregate or variable sized value, reuse the input pointer.
1609   if (!Ty->isConstantSizeType() ||
1610       !CodeGenFunction::hasScalarEvaluationKind(Ty)) {
1611     DeclPtr = Arg;
1612   } else {
1613     // Otherwise, create a temporary to hold the value.
1614     llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertTypeForMem(Ty),
1615                                                D.getName() + ".addr");
1616     CharUnits Align = getContext().getDeclAlign(&D);
1617     Alloc->setAlignment(Align.getQuantity());
1618     DeclPtr = Alloc;
1619 
1620     bool doStore = true;
1621 
1622     Qualifiers qs = Ty.getQualifiers();
1623     LValue lv = MakeAddrLValue(DeclPtr, Ty, Align);
1624     if (Qualifiers::ObjCLifetime lt = qs.getObjCLifetime()) {
1625       // We honor __attribute__((ns_consumed)) for types with lifetime.
1626       // For __strong, it's handled by just skipping the initial retain;
1627       // otherwise we have to balance out the initial +1 with an extra
1628       // cleanup to do the release at the end of the function.
1629       bool isConsumed = D.hasAttr<NSConsumedAttr>();
1630 
1631       // 'self' is always formally __strong, but if this is not an
1632       // init method then we don't want to retain it.
1633       if (D.isARCPseudoStrong()) {
1634         const ObjCMethodDecl *method = cast<ObjCMethodDecl>(CurCodeDecl);
1635         assert(&D == method->getSelfDecl());
1636         assert(lt == Qualifiers::OCL_Strong);
1637         assert(qs.hasConst());
1638         assert(method->getMethodFamily() != OMF_init);
1639         (void) method;
1640         lt = Qualifiers::OCL_ExplicitNone;
1641       }
1642 
1643       if (lt == Qualifiers::OCL_Strong) {
1644         if (!isConsumed) {
1645           if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
1646             // use objc_storeStrong(&dest, value) for retaining the
1647             // object. But first, store a null into 'dest' because
1648             // objc_storeStrong attempts to release its old value.
1649             llvm::Value * Null = CGM.EmitNullConstant(D.getType());
1650             EmitStoreOfScalar(Null, lv, /* isInitialization */ true);
1651             EmitARCStoreStrongCall(lv.getAddress(), Arg, true);
1652             doStore = false;
1653           }
1654           else
1655           // Don't use objc_retainBlock for block pointers, because we
1656           // don't want to Block_copy something just because we got it
1657           // as a parameter.
1658             Arg = EmitARCRetainNonBlock(Arg);
1659         }
1660       } else {
1661         // Push the cleanup for a consumed parameter.
1662         if (isConsumed) {
1663           ARCPreciseLifetime_t precise = (D.hasAttr<ObjCPreciseLifetimeAttr>()
1664                                 ? ARCPreciseLifetime : ARCImpreciseLifetime);
1665           EHStack.pushCleanup<ConsumeARCParameter>(getARCCleanupKind(), Arg,
1666                                                    precise);
1667         }
1668 
1669         if (lt == Qualifiers::OCL_Weak) {
1670           EmitARCInitWeak(DeclPtr, Arg);
1671           doStore = false; // The weak init is a store, no need to do two.
1672         }
1673       }
1674 
1675       // Enter the cleanup scope.
1676       EmitAutoVarWithLifetime(*this, D, DeclPtr, lt);
1677     }
1678 
1679     // Store the initial value into the alloca.
1680     if (doStore)
1681       EmitStoreOfScalar(Arg, lv, /* isInitialization */ true);
1682   }
1683 
1684   llvm::Value *&DMEntry = LocalDeclMap[&D];
1685   assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
1686   DMEntry = DeclPtr;
1687 
1688   // Emit debug info for param declaration.
1689   if (CGDebugInfo *DI = getDebugInfo()) {
1690     if (CGM.getCodeGenOpts().getDebugInfo()
1691           >= CodeGenOptions::LimitedDebugInfo) {
1692       DI->EmitDeclareOfArgVariable(&D, DeclPtr, ArgNo, Builder);
1693     }
1694   }
1695 
1696   if (D.hasAttr<AnnotateAttr>())
1697       EmitVarAnnotations(&D, DeclPtr);
1698 }
1699