blob: 792865668bc95bd96e5f3badc9af19dae60843ec [file] [log] [blame]
Chris Lattner84915fa2007-06-02 04:16:21 +00001//===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner84915fa2007-06-02 04:16:21 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Decl nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
Sanjiv Gupta18de6242008-05-30 10:30:31 +000014#include "CGDebugInfo.h"
Chris Lattner84915fa2007-06-02 04:16:21 +000015#include "CodeGenFunction.h"
Anders Carlssonf94cd1f2007-10-17 00:52:43 +000016#include "CodeGenModule.h"
John McCallad7c5c12011-02-08 08:22:06 +000017#include "CGBlocks.h"
Daniel Dunbarad319a72008-08-11 05:00:27 +000018#include "clang/AST/ASTContext.h"
Ken Dyck8c89d592009-12-22 14:23:30 +000019#include "clang/AST/CharUnits.h"
Daniel Dunbar6e8aa532008-08-11 05:35:13 +000020#include "clang/AST/Decl.h"
Anders Carlsson4c03d982008-08-25 01:38:19 +000021#include "clang/AST/DeclObjC.h"
Nate Begemanfaae0812008-04-19 04:17:09 +000022#include "clang/Basic/SourceManager.h"
Chris Lattnerb781dc792008-05-08 05:58:21 +000023#include "clang/Basic/TargetInfo.h"
Chandler Carruth85098242010-06-15 23:19:56 +000024#include "clang/Frontend/CodeGenOptions.h"
Anders Carlssonf94cd1f2007-10-17 00:52:43 +000025#include "llvm/GlobalVariable.h"
Anders Carlsson30032882008-12-12 07:38:43 +000026#include "llvm/Intrinsics.h"
Mike Stump97d01d52009-03-04 03:23:46 +000027#include "llvm/Target/TargetData.h"
Chris Lattner53621a52007-06-13 20:44:40 +000028#include "llvm/Type.h"
Chris Lattner84915fa2007-06-02 04:16:21 +000029using namespace clang;
30using namespace CodeGen;
31
32
Chris Lattner1ad38f82007-06-09 01:20:56 +000033void CodeGenFunction::EmitDecl(const Decl &D) {
Chris Lattner1ad38f82007-06-09 01:20:56 +000034 switch (D.getKind()) {
Douglas Gregor19043f02010-04-23 02:02:43 +000035 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:
Alexis Hunted053252010-05-30 07:21:58 +000042 case Decl::NonTypeTemplateParm:
Douglas Gregor19043f02010-04-23 02:02:43 +000043 case Decl::CXXMethod:
44 case Decl::CXXConstructor:
45 case Decl::CXXDestructor:
46 case Decl::CXXConversion:
47 case Decl::Field:
Francois Pichetdf946c32010-11-21 06:49:41 +000048 case Decl::IndirectField:
Douglas Gregor19043f02010-04-23 02:02:43 +000049 case Decl::ObjCIvar:
50 case Decl::ObjCAtDefsField:
Chris Lattnerba9dddb2007-10-08 21:37:32 +000051 case Decl::ParmVar:
Douglas Gregor19043f02010-04-23 02:02:43 +000052 case Decl::ImplicitParam:
53 case Decl::ClassTemplate:
54 case Decl::FunctionTemplate:
55 case Decl::TemplateTemplateParm:
56 case Decl::ObjCMethod:
57 case Decl::ObjCCategory:
58 case Decl::ObjCProtocol:
59 case Decl::ObjCInterface:
60 case Decl::ObjCCategoryImpl:
61 case Decl::ObjCImplementation:
62 case Decl::ObjCProperty:
63 case Decl::ObjCCompatibleAlias:
Abramo Bagnarad7340582010-06-05 05:09:32 +000064 case Decl::AccessSpec:
Douglas Gregor19043f02010-04-23 02:02:43 +000065 case Decl::LinkageSpec:
66 case Decl::ObjCPropertyImpl:
67 case Decl::ObjCClass:
68 case Decl::ObjCForwardProtocol:
69 case Decl::FileScopeAsm:
70 case Decl::Friend:
71 case Decl::FriendTemplate:
72 case Decl::Block:
Douglas Gregor19043f02010-04-23 02:02:43 +000073 assert(0 && "Declaration not should not be in declstmts!");
Chris Lattner84915fa2007-06-02 04:16:21 +000074 case Decl::Function: // void X();
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +000075 case Decl::Record: // struct/union/class X;
Chris Lattner84915fa2007-06-02 04:16:21 +000076 case Decl::Enum: // enum X;
Mike Stump11289f42009-09-09 15:08:12 +000077 case Decl::EnumConstant: // enum ? { X = ? }
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +000078 case Decl::CXXRecord: // struct/union/class X; [C++]
Daniel Dunbar785406b2009-11-23 00:07:06 +000079 case Decl::Using: // using X; [C++]
80 case Decl::UsingShadow:
81 case Decl::UsingDirective: // using namespace X; [C++]
Douglas Gregor19043f02010-04-23 02:02:43 +000082 case Decl::NamespaceAlias:
Anders Carlssonce2cd012009-12-03 17:26:31 +000083 case Decl::StaticAssert: // static_assert(X, ""); [C++0x]
Chris Lattner43e7f312011-02-18 02:08:43 +000084 case Decl::Label: // __label__ x;
Chris Lattner84915fa2007-06-02 04:16:21 +000085 // None of these decls require codegen support.
86 return;
Mike Stump11289f42009-09-09 15:08:12 +000087
Daniel Dunbara7998072008-08-29 17:28:43 +000088 case Decl::Var: {
89 const VarDecl &VD = cast<VarDecl>(D);
John McCall1c9c3fd2010-10-15 04:57:14 +000090 assert(VD.isLocalVarDecl() &&
Daniel Dunbara7998072008-08-29 17:28:43 +000091 "Should not see file-scope variables inside a function!");
John McCall1c9c3fd2010-10-15 04:57:14 +000092 return EmitVarDecl(VD);
Chris Lattner84915fa2007-06-02 04:16:21 +000093 }
Mike Stump11289f42009-09-09 15:08:12 +000094
Anders Carlsson5d985f52008-12-20 21:51:53 +000095 case Decl::Typedef: { // typedef int X;
96 const TypedefDecl &TD = cast<TypedefDecl>(D);
97 QualType Ty = TD.getUnderlyingType();
Mike Stump11289f42009-09-09 15:08:12 +000098
Anders Carlsson5d985f52008-12-20 21:51:53 +000099 if (Ty->isVariablyModifiedType())
100 EmitVLASize(Ty);
101 }
Daniel Dunbara7998072008-08-29 17:28:43 +0000102 }
Chris Lattner84915fa2007-06-02 04:16:21 +0000103}
Chris Lattner03df1222007-06-02 04:53:11 +0000104
John McCall1c9c3fd2010-10-15 04:57:14 +0000105/// EmitVarDecl - This method handles emission of any variable declaration
Chris Lattner03df1222007-06-02 04:53:11 +0000106/// inside a function, including static vars etc.
John McCall1c9c3fd2010-10-15 04:57:14 +0000107void CodeGenFunction::EmitVarDecl(const VarDecl &D) {
Chris Lattner03df1222007-06-02 04:53:11 +0000108 switch (D.getStorageClass()) {
John McCall8e7d6562010-08-26 03:08:43 +0000109 case SC_None:
110 case SC_Auto:
111 case SC_Register:
John McCall1c9c3fd2010-10-15 04:57:14 +0000112 return EmitAutoVarDecl(D);
John McCall8e7d6562010-08-26 03:08:43 +0000113 case SC_Static: {
Anders Carlssoncee2d2f2010-02-07 02:03:08 +0000114 llvm::GlobalValue::LinkageTypes Linkage =
115 llvm::GlobalValue::InternalLinkage;
116
John McCall7cb02202010-05-25 04:30:21 +0000117 // If the function definition has some sort of weak linkage, its
118 // static variables should also be weak so that they get properly
119 // uniqued. We can't do this in C, though, because there's no
120 // standard way to agree on which variables are the same (i.e.
121 // there's no mangling).
122 if (getContext().getLangOptions().CPlusPlus)
123 if (llvm::GlobalValue::isWeakForLinker(CurFn->getLinkage()))
124 Linkage = CurFn->getLinkage();
Anders Carlssoncee2d2f2010-02-07 02:03:08 +0000125
John McCall1c9c3fd2010-10-15 04:57:14 +0000126 return EmitStaticVarDecl(D, Linkage);
Anders Carlssoncee2d2f2010-02-07 02:03:08 +0000127 }
John McCall8e7d6562010-08-26 03:08:43 +0000128 case SC_Extern:
129 case SC_PrivateExtern:
Lauro Ramos Venanciobada8d42008-02-16 22:30:38 +0000130 // Don't emit it now, allow it to be emitted lazily on its first use.
131 return;
Chris Lattner03df1222007-06-02 04:53:11 +0000132 }
Daniel Dunbar0ca16602009-04-14 02:25:56 +0000133
134 assert(0 && "Unknown storage class");
Chris Lattner03df1222007-06-02 04:53:11 +0000135}
136
Chris Lattnere99c1102009-12-05 08:22:11 +0000137static std::string GetStaticDeclName(CodeGenFunction &CGF, const VarDecl &D,
138 const char *Separator) {
139 CodeGenModule &CGM = CGF.CGM;
John McCall7ec50432010-03-19 23:29:14 +0000140 if (CGF.getContext().getLangOptions().CPlusPlus) {
Anders Carlssonea836bc2010-06-22 16:16:50 +0000141 llvm::StringRef Name = CGM.getMangledName(&D);
142 return Name.str();
John McCall7ec50432010-03-19 23:29:14 +0000143 }
Chris Lattnere99c1102009-12-05 08:22:11 +0000144
145 std::string ContextName;
Fariborz Jahanian3a4ea9a2010-11-30 23:07:14 +0000146 if (!CGF.CurFuncDecl) {
147 // Better be in a block declared in global scope.
148 const NamedDecl *ND = cast<NamedDecl>(&D);
149 const DeclContext *DC = ND->getDeclContext();
150 if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
151 MangleBuffer Name;
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000152 CGM.getBlockMangledName(GlobalDecl(), Name, BD);
Fariborz Jahanian3a4ea9a2010-11-30 23:07:14 +0000153 ContextName = Name.getString();
154 }
155 else
156 assert(0 && "Unknown context for block static var decl");
157 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CGF.CurFuncDecl)) {
Anders Carlssonea836bc2010-06-22 16:16:50 +0000158 llvm::StringRef Name = CGM.getMangledName(FD);
159 ContextName = Name.str();
John McCall7ec50432010-03-19 23:29:14 +0000160 } else if (isa<ObjCMethodDecl>(CGF.CurFuncDecl))
Chris Lattnere99c1102009-12-05 08:22:11 +0000161 ContextName = CGF.CurFn->getName();
162 else
Fariborz Jahanian3a4ea9a2010-11-30 23:07:14 +0000163 assert(0 && "Unknown context for static var decl");
Chris Lattnere99c1102009-12-05 08:22:11 +0000164
165 return ContextName + Separator + D.getNameAsString();
166}
167
Daniel Dunbar22a87f92009-02-25 19:24:29 +0000168llvm::GlobalVariable *
John McCall1c9c3fd2010-10-15 04:57:14 +0000169CodeGenFunction::CreateStaticVarDecl(const VarDecl &D,
170 const char *Separator,
171 llvm::GlobalValue::LinkageTypes Linkage) {
Chris Lattnerfc4379f2008-04-06 23:10:54 +0000172 QualType Ty = D.getType();
Eli Friedmana682d392008-02-15 12:20:59 +0000173 assert(Ty->isConstantSizeType() && "VLAs can't be static");
Nate Begemanfaae0812008-04-19 04:17:09 +0000174
Chris Lattnere99c1102009-12-05 08:22:11 +0000175 std::string Name = GetStaticDeclName(*this, D, Separator);
Nate Begemanfaae0812008-04-19 04:17:09 +0000176
Daniel Dunbar22a87f92009-02-25 19:24:29 +0000177 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
Anders Carlssone33eed52009-09-26 18:16:06 +0000178 llvm::GlobalVariable *GV =
179 new llvm::GlobalVariable(CGM.getModule(), LTy,
180 Ty.isConstant(getContext()), Linkage,
181 CGM.EmitNullConstant(D.getType()), Name, 0,
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000182 D.isThreadSpecified(),
183 CGM.getContext().getTargetAddressSpace(Ty));
Ken Dyck160146e2010-01-27 17:10:57 +0000184 GV->setAlignment(getContext().getDeclAlign(&D).getQuantity());
John McCall8e7cb6d2010-11-02 21:04:24 +0000185 if (Linkage != llvm::GlobalValue::InternalLinkage)
186 GV->setVisibility(CurFn->getVisibility());
Anders Carlssone33eed52009-09-26 18:16:06 +0000187 return GV;
Daniel Dunbar22a87f92009-02-25 19:24:29 +0000188}
189
John McCall1c9c3fd2010-10-15 04:57:14 +0000190/// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the
Chris Lattnere99c1102009-12-05 08:22:11 +0000191/// global variable that has already been created for it. If the initializer
192/// has a different type than GV does, this may free GV and return a different
193/// one. Otherwise it just returns GV.
194llvm::GlobalVariable *
John McCall1c9c3fd2010-10-15 04:57:14 +0000195CodeGenFunction::AddInitializerToStaticVarDecl(const VarDecl &D,
196 llvm::GlobalVariable *GV) {
Chris Lattnere99c1102009-12-05 08:22:11 +0000197 llvm::Constant *Init = CGM.EmitConstantExpr(D.getInit(), D.getType(), this);
John McCall70013b62010-07-15 23:40:35 +0000198
Chris Lattnere99c1102009-12-05 08:22:11 +0000199 // If constant emission failed, then this should be a C++ static
200 // initializer.
201 if (!Init) {
202 if (!getContext().getLangOptions().CPlusPlus)
203 CGM.ErrorUnsupported(D.getInit(), "constant l-value expression");
John McCall68ff0372010-09-08 01:44:27 +0000204 else if (Builder.GetInsertBlock()) {
Anders Carlsson20bbbd42010-01-26 04:02:23 +0000205 // Since we have a static initializer, this global variable can't
206 // be constant.
207 GV->setConstant(false);
John McCall68ff0372010-09-08 01:44:27 +0000208
John McCallcdf7ef52010-11-06 09:44:32 +0000209 EmitCXXGuardedInit(D, GV);
Anders Carlsson20bbbd42010-01-26 04:02:23 +0000210 }
Chris Lattnere99c1102009-12-05 08:22:11 +0000211 return GV;
212 }
John McCall70013b62010-07-15 23:40:35 +0000213
Chris Lattnere99c1102009-12-05 08:22:11 +0000214 // The initializer may differ in type from the global. Rewrite
215 // the global to match the initializer. (We have to do this
216 // because some types, like unions, can't be completely represented
217 // in the LLVM type system.)
John McCall56f57582010-09-03 18:58:50 +0000218 if (GV->getType()->getElementType() != Init->getType()) {
Chris Lattnere99c1102009-12-05 08:22:11 +0000219 llvm::GlobalVariable *OldGV = GV;
220
221 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
222 OldGV->isConstant(),
223 OldGV->getLinkage(), Init, "",
John McCall8e7cb6d2010-11-02 21:04:24 +0000224 /*InsertBefore*/ OldGV,
225 D.isThreadSpecified(),
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000226 CGM.getContext().getTargetAddressSpace(D.getType()));
John McCall8e7cb6d2010-11-02 21:04:24 +0000227 GV->setVisibility(OldGV->getVisibility());
Chris Lattnere99c1102009-12-05 08:22:11 +0000228
229 // Steal the name of the old global
230 GV->takeName(OldGV);
231
232 // Replace all uses of the old global with the new global
233 llvm::Constant *NewPtrForOldDecl =
234 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
235 OldGV->replaceAllUsesWith(NewPtrForOldDecl);
236
237 // Erase the old global, since it is no longer used.
238 OldGV->eraseFromParent();
239 }
240
241 GV->setInitializer(Init);
242 return GV;
243}
244
John McCall1c9c3fd2010-10-15 04:57:14 +0000245void CodeGenFunction::EmitStaticVarDecl(const VarDecl &D,
Anders Carlssoncee2d2f2010-02-07 02:03:08 +0000246 llvm::GlobalValue::LinkageTypes Linkage) {
Daniel Dunbara374e602009-02-25 19:45:19 +0000247 llvm::Value *&DMEntry = LocalDeclMap[&D];
248 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
Mike Stump11289f42009-09-09 15:08:12 +0000249
John McCall1c9c3fd2010-10-15 04:57:14 +0000250 llvm::GlobalVariable *GV = CreateStaticVarDecl(D, ".", Linkage);
Daniel Dunbara374e602009-02-25 19:45:19 +0000251
Daniel Dunbar1cdbc542009-02-25 20:08:33 +0000252 // Store into LocalDeclMap before generating initializer to handle
253 // circular references.
254 DMEntry = GV;
255
John McCall4a39ab82010-05-04 20:45:42 +0000256 // We can't have a VLA here, but we can have a pointer to a VLA,
257 // even though that doesn't really make any sense.
Eli Friedmanbc633be2009-04-20 03:54:15 +0000258 // Make sure to evaluate VLA bounds now so that we have them for later.
259 if (D.getType()->isVariablyModifiedType())
260 EmitVLASize(D.getType());
Fariborz Jahanian366a9482010-09-07 23:26:17 +0000261
262 // Local static block variables must be treated as globals as they may be
263 // referenced in their RHS initializer block-literal expresion.
264 CGM.setStaticLocalDeclAddress(&D, GV);
Eli Friedmanbc633be2009-04-20 03:54:15 +0000265
Chris Lattnere99c1102009-12-05 08:22:11 +0000266 // If this value has an initializer, emit it.
267 if (D.getInit())
John McCall1c9c3fd2010-10-15 04:57:14 +0000268 GV = AddInitializerToStaticVarDecl(D, GV);
Nate Begemanfaae0812008-04-19 04:17:09 +0000269
Chris Lattner4d941092010-03-10 23:59:59 +0000270 GV->setAlignment(getContext().getDeclAlign(&D).getQuantity());
271
Daniel Dunbar53bf7412009-02-12 23:32:54 +0000272 // FIXME: Merge attribute handling.
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000273 if (const AnnotateAttr *AA = D.getAttr<AnnotateAttr>()) {
Nate Begemanfaae0812008-04-19 04:17:09 +0000274 SourceManager &SM = CGM.getContext().getSourceManager();
275 llvm::Constant *Ann =
Mike Stump11289f42009-09-09 15:08:12 +0000276 CGM.EmitAnnotateAttr(GV, AA,
Chris Lattner8a425862009-01-16 07:36:28 +0000277 SM.getInstantiationLineNumber(D.getLocation()));
Nate Begemanfaae0812008-04-19 04:17:09 +0000278 CGM.AddAnnotation(Ann);
279 }
280
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000281 if (const SectionAttr *SA = D.getAttr<SectionAttr>())
Daniel Dunbar53bf7412009-02-12 23:32:54 +0000282 GV->setSection(SA->getName());
Mike Stump11289f42009-09-09 15:08:12 +0000283
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000284 if (D.hasAttr<UsedAttr>())
Daniel Dunbar128a1382009-02-13 22:08:43 +0000285 CGM.AddUsedGlobal(GV);
286
Daniel Dunbar1cdbc542009-02-25 20:08:33 +0000287 // We may have to cast the constant because of the initializer
288 // mismatch above.
289 //
290 // FIXME: It is really dangerous to store this in the map; if anyone
291 // RAUW's the GV uses of this constant will be invalid.
Eli Friedmanc98a7ad2008-06-08 01:23:18 +0000292 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(D.getType());
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000293 const llvm::Type *LPtrTy =
294 LTy->getPointerTo(CGM.getContext().getTargetAddressSpace(D.getType()));
Owen Andersonade90fd2009-07-29 18:54:39 +0000295 DMEntry = llvm::ConstantExpr::getBitCast(GV, LPtrTy);
Sanjiv Gupta158143a2008-06-05 08:59:10 +0000296
297 // Emit global variable debug descriptor for static vars.
Anders Carlsson63784f42009-02-13 08:11:52 +0000298 CGDebugInfo *DI = getDebugInfo();
Mike Stumpf5a5c4f2009-02-20 00:19:45 +0000299 if (DI) {
Daniel Dunbarb9fd9022008-10-17 16:15:48 +0000300 DI->setLocation(D.getLocation());
Sanjiv Gupta158143a2008-06-05 08:59:10 +0000301 DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(GV), &D);
302 }
Anders Carlssonf94cd1f2007-10-17 00:52:43 +0000303}
Mike Stump11289f42009-09-09 15:08:12 +0000304
Anders Carlsson0168f4b2009-09-12 02:14:24 +0000305unsigned CodeGenFunction::getByRefValueLLVMField(const ValueDecl *VD) const {
306 assert(ByRefValueInfo.count(VD) && "Did not find value!");
307
308 return ByRefValueInfo.find(VD)->second.second;
309}
310
Fariborz Jahanian2f2fa722011-01-26 23:08:27 +0000311llvm::Value *CodeGenFunction::BuildBlockByrefAddress(llvm::Value *BaseAddr,
312 const VarDecl *V) {
313 llvm::Value *Loc = Builder.CreateStructGEP(BaseAddr, 1, "forwarding");
314 Loc = Builder.CreateLoad(Loc);
315 Loc = Builder.CreateStructGEP(Loc, getByRefValueLLVMField(V),
316 V->getNameAsString());
317 return Loc;
318}
319
Mike Stump97d01d52009-03-04 03:23:46 +0000320/// BuildByRefType - This routine changes a __block variable declared as T x
321/// into:
322///
323/// struct {
324/// void *__isa;
325/// void *__forwarding;
326/// int32_t __flags;
327/// int32_t __size;
Mike Stump2114d7c2009-09-22 02:12:52 +0000328/// void *__copy_helper; // only if needed
329/// void *__destroy_helper; // only if needed
330/// char padding[X]; // only if needed
Mike Stump97d01d52009-03-04 03:23:46 +0000331/// T x;
332/// } x
333///
John McCall351762c2011-02-07 10:33:21 +0000334const llvm::Type *CodeGenFunction::BuildByRefType(const VarDecl *D) {
Anders Carlsson0168f4b2009-09-12 02:14:24 +0000335 std::pair<const llvm::Type *, unsigned> &Info = ByRefValueInfo[D];
336 if (Info.first)
337 return Info.first;
338
Anders Carlsson71d1d922009-09-09 02:51:03 +0000339 QualType Ty = D->getType();
Mike Stump11289f42009-09-09 15:08:12 +0000340
Anders Carlssonf8e94f22009-09-12 02:44:18 +0000341 std::vector<const llvm::Type *> Types;
Anders Carlsson10f2c102009-09-10 01:32:12 +0000342
John McCallad7c5c12011-02-08 08:22:06 +0000343 llvm::PATypeHolder ByRefTypeHolder = llvm::OpaqueType::get(getLLVMContext());
Anders Carlsson10f2c102009-09-10 01:32:12 +0000344
Anders Carlssonf8e94f22009-09-12 02:44:18 +0000345 // void *__isa;
346 Types.push_back(Int8PtrTy);
347
348 // void *__forwarding;
349 Types.push_back(llvm::PointerType::getUnqual(ByRefTypeHolder));
350
351 // int32_t __flags;
Chris Lattner5e016ae2010-06-27 07:15:29 +0000352 Types.push_back(Int32Ty);
Anders Carlssonf8e94f22009-09-12 02:44:18 +0000353
354 // int32_t __size;
Chris Lattner5e016ae2010-06-27 07:15:29 +0000355 Types.push_back(Int32Ty);
Anders Carlssonf8e94f22009-09-12 02:44:18 +0000356
John McCall351762c2011-02-07 10:33:21 +0000357 bool HasCopyAndDispose = getContext().BlockRequiresCopying(Ty);
Anders Carlssonf8e94f22009-09-12 02:44:18 +0000358 if (HasCopyAndDispose) {
359 /// void *__copy_helper;
360 Types.push_back(Int8PtrTy);
361
362 /// void *__destroy_helper;
363 Types.push_back(Int8PtrTy);
Mike Stump97d01d52009-03-04 03:23:46 +0000364 }
Anders Carlssonf8e94f22009-09-12 02:44:18 +0000365
366 bool Packed = false;
Ken Dyck160146e2010-01-27 17:10:57 +0000367 CharUnits Align = getContext().getDeclAlign(D);
Ken Dyck9a648692011-01-18 02:01:14 +0000368 if (Align > getContext().toCharUnitsFromBits(Target.getPointerAlign(0))) {
Anders Carlssonf8e94f22009-09-12 02:44:18 +0000369 // We have to insert padding.
370
371 // The struct above has 2 32-bit integers.
372 unsigned CurrentOffsetInBytes = 4 * 2;
373
374 // And either 2 or 4 pointers.
375 CurrentOffsetInBytes += (HasCopyAndDispose ? 4 : 2) *
376 CGM.getTargetData().getTypeAllocSize(Int8PtrTy);
377
378 // Align the offset.
379 unsigned AlignedOffsetInBytes =
Ken Dyck160146e2010-01-27 17:10:57 +0000380 llvm::RoundUpToAlignment(CurrentOffsetInBytes, Align.getQuantity());
Anders Carlssonf8e94f22009-09-12 02:44:18 +0000381
382 unsigned NumPaddingBytes = AlignedOffsetInBytes - CurrentOffsetInBytes;
Anders Carlssonccbabc92009-09-13 17:55:13 +0000383 if (NumPaddingBytes > 0) {
John McCallad7c5c12011-02-08 08:22:06 +0000384 const llvm::Type *Ty = llvm::Type::getInt8Ty(getLLVMContext());
Mike Stump70197d52009-10-21 00:42:55 +0000385 // FIXME: We need a sema error for alignment larger than the minimum of
386 // the maximal stack alignmint and the alignment of malloc on the system.
Anders Carlssonccbabc92009-09-13 17:55:13 +0000387 if (NumPaddingBytes > 1)
388 Ty = llvm::ArrayType::get(Ty, NumPaddingBytes);
Anders Carlssonf8e94f22009-09-12 02:44:18 +0000389
Anders Carlssonccbabc92009-09-13 17:55:13 +0000390 Types.push_back(Ty);
Anders Carlssonf8e94f22009-09-12 02:44:18 +0000391
Anders Carlssonccbabc92009-09-13 17:55:13 +0000392 // We want a packed struct.
393 Packed = true;
394 }
Anders Carlssonf8e94f22009-09-12 02:44:18 +0000395 }
396
397 // T x;
Fariborz Jahanian087206d2010-09-03 23:07:53 +0000398 Types.push_back(ConvertTypeForMem(Ty));
Anders Carlsson0168f4b2009-09-12 02:14:24 +0000399
John McCallad7c5c12011-02-08 08:22:06 +0000400 const llvm::Type *T = llvm::StructType::get(getLLVMContext(), Types, Packed);
Anders Carlsson10f2c102009-09-10 01:32:12 +0000401
402 cast<llvm::OpaqueType>(ByRefTypeHolder.get())->refineAbstractTypeTo(T);
403 CGM.getModule().addTypeName("struct.__block_byref_" + D->getNameAsString(),
404 ByRefTypeHolder.get());
405
Anders Carlsson0168f4b2009-09-12 02:14:24 +0000406 Info.first = ByRefTypeHolder.get();
Anders Carlssonf8e94f22009-09-12 02:44:18 +0000407
408 Info.second = Types.size() - 1;
Anders Carlsson0168f4b2009-09-12 02:14:24 +0000409
410 return Info.first;
Mike Stump97d01d52009-03-04 03:23:46 +0000411}
412
John McCall2b7fc382010-07-13 20:32:21 +0000413namespace {
John McCallcda666c2010-07-21 07:22:38 +0000414 struct CallArrayDtor : EHScopeStack::Cleanup {
John McCall2b7fc382010-07-13 20:32:21 +0000415 CallArrayDtor(const CXXDestructorDecl *Dtor,
416 const ConstantArrayType *Type,
417 llvm::Value *Loc)
418 : Dtor(Dtor), Type(Type), Loc(Loc) {}
419
420 const CXXDestructorDecl *Dtor;
421 const ConstantArrayType *Type;
422 llvm::Value *Loc;
423
424 void Emit(CodeGenFunction &CGF, bool IsForEH) {
425 QualType BaseElementTy = CGF.getContext().getBaseElementType(Type);
426 const llvm::Type *BasePtr = CGF.ConvertType(BaseElementTy);
427 BasePtr = llvm::PointerType::getUnqual(BasePtr);
428 llvm::Value *BaseAddrPtr = CGF.Builder.CreateBitCast(Loc, BasePtr);
429 CGF.EmitCXXAggrDestructorCall(Dtor, Type, BaseAddrPtr);
430 }
431 };
432
John McCallcda666c2010-07-21 07:22:38 +0000433 struct CallVarDtor : EHScopeStack::Cleanup {
John McCall2b7fc382010-07-13 20:32:21 +0000434 CallVarDtor(const CXXDestructorDecl *Dtor,
435 llvm::Value *NRVOFlag,
436 llvm::Value *Loc)
437 : Dtor(Dtor), NRVOFlag(NRVOFlag), Loc(Loc) {}
438
439 const CXXDestructorDecl *Dtor;
440 llvm::Value *NRVOFlag;
441 llvm::Value *Loc;
442
443 void Emit(CodeGenFunction &CGF, bool IsForEH) {
444 // Along the exceptions path we always execute the dtor.
445 bool NRVO = !IsForEH && NRVOFlag;
446
447 llvm::BasicBlock *SkipDtorBB = 0;
448 if (NRVO) {
449 // If we exited via NRVO, we skip the destructor call.
450 llvm::BasicBlock *RunDtorBB = CGF.createBasicBlock("nrvo.unused");
451 SkipDtorBB = CGF.createBasicBlock("nrvo.skipdtor");
452 llvm::Value *DidNRVO = CGF.Builder.CreateLoad(NRVOFlag, "nrvo.val");
453 CGF.Builder.CreateCondBr(DidNRVO, SkipDtorBB, RunDtorBB);
454 CGF.EmitBlock(RunDtorBB);
455 }
456
457 CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete,
458 /*ForVirtualBase=*/false, Loc);
459
460 if (NRVO) CGF.EmitBlock(SkipDtorBB);
461 }
462 };
463}
464
John McCalla464ff92010-07-21 06:13:08 +0000465namespace {
John McCallcda666c2010-07-21 07:22:38 +0000466 struct CallStackRestore : EHScopeStack::Cleanup {
John McCalla464ff92010-07-21 06:13:08 +0000467 llvm::Value *Stack;
468 CallStackRestore(llvm::Value *Stack) : Stack(Stack) {}
469 void Emit(CodeGenFunction &CGF, bool IsForEH) {
470 llvm::Value *V = CGF.Builder.CreateLoad(Stack, "tmp");
471 llvm::Value *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stackrestore);
472 CGF.Builder.CreateCall(F, V);
473 }
474 };
475
John McCallcda666c2010-07-21 07:22:38 +0000476 struct CallCleanupFunction : EHScopeStack::Cleanup {
John McCalla464ff92010-07-21 06:13:08 +0000477 llvm::Constant *CleanupFn;
478 const CGFunctionInfo &FnInfo;
John McCalla464ff92010-07-21 06:13:08 +0000479 const VarDecl &Var;
480
481 CallCleanupFunction(llvm::Constant *CleanupFn, const CGFunctionInfo *Info,
John McCallc533cb72011-02-22 06:44:22 +0000482 const VarDecl *Var)
483 : CleanupFn(CleanupFn), FnInfo(*Info), Var(*Var) {}
John McCalla464ff92010-07-21 06:13:08 +0000484
485 void Emit(CodeGenFunction &CGF, bool IsForEH) {
John McCallc533cb72011-02-22 06:44:22 +0000486 DeclRefExpr DRE(const_cast<VarDecl*>(&Var), Var.getType(), VK_LValue,
487 SourceLocation());
488 // Compute the address of the local variable, in case it's a byref
489 // or something.
490 llvm::Value *Addr = CGF.EmitDeclRefLValue(&DRE).getAddress();
491
John McCalla464ff92010-07-21 06:13:08 +0000492 // In some cases, the type of the function argument will be different from
493 // the type of the pointer. An example of this is
494 // void f(void* arg);
495 // __attribute__((cleanup(f))) void *g;
496 //
497 // To fix this we insert a bitcast here.
498 QualType ArgTy = FnInfo.arg_begin()->type;
499 llvm::Value *Arg =
500 CGF.Builder.CreateBitCast(Addr, CGF.ConvertType(ArgTy));
501
502 CallArgList Args;
503 Args.push_back(std::make_pair(RValue::get(Arg),
504 CGF.getContext().getPointerType(Var.getType())));
505 CGF.EmitCall(FnInfo, CleanupFn, ReturnValueSlot(), Args);
506 }
507 };
508
John McCallcda666c2010-07-21 07:22:38 +0000509 struct CallBlockRelease : EHScopeStack::Cleanup {
John McCalla464ff92010-07-21 06:13:08 +0000510 llvm::Value *Addr;
511 CallBlockRelease(llvm::Value *Addr) : Addr(Addr) {}
512
513 void Emit(CodeGenFunction &CGF, bool IsForEH) {
John McCalla9a2e8a2011-02-18 02:58:31 +0000514 CGF.BuildBlockRelease(Addr, BLOCK_FIELD_IS_BYREF);
John McCalla464ff92010-07-21 06:13:08 +0000515 }
516 };
517}
518
Chris Lattnerb85025f2010-12-01 02:05:19 +0000519
520/// canEmitInitWithFewStoresAfterMemset - Decide whether we can emit the
521/// non-zero parts of the specified initializer with equal or fewer than
522/// NumStores scalar stores.
523static bool canEmitInitWithFewStoresAfterMemset(llvm::Constant *Init,
524 unsigned &NumStores) {
Chris Lattnere6af8862010-12-02 01:58:41 +0000525 // Zero and Undef never requires any extra stores.
526 if (isa<llvm::ConstantAggregateZero>(Init) ||
527 isa<llvm::ConstantPointerNull>(Init) ||
528 isa<llvm::UndefValue>(Init))
529 return true;
530 if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) ||
531 isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) ||
532 isa<llvm::ConstantExpr>(Init))
533 return Init->isNullValue() || NumStores--;
534
535 // See if we can emit each element.
536 if (isa<llvm::ConstantArray>(Init) || isa<llvm::ConstantStruct>(Init)) {
537 for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) {
538 llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i));
539 if (!canEmitInitWithFewStoresAfterMemset(Elt, NumStores))
540 return false;
541 }
542 return true;
543 }
Chris Lattnerb85025f2010-12-01 02:05:19 +0000544
545 // Anything else is hard and scary.
546 return false;
547}
548
549/// emitStoresForInitAfterMemset - For inits that
550/// canEmitInitWithFewStoresAfterMemset returned true for, emit the scalar
551/// stores that would be required.
552static void emitStoresForInitAfterMemset(llvm::Constant *Init, llvm::Value *Loc,
John McCallc533cb72011-02-22 06:44:22 +0000553 bool isVolatile, CGBuilderTy &Builder) {
Chris Lattnerb85025f2010-12-01 02:05:19 +0000554 // Zero doesn't require any stores.
Chris Lattnere6af8862010-12-02 01:58:41 +0000555 if (isa<llvm::ConstantAggregateZero>(Init) ||
556 isa<llvm::ConstantPointerNull>(Init) ||
557 isa<llvm::UndefValue>(Init))
558 return;
Chris Lattnerb85025f2010-12-01 02:05:19 +0000559
Chris Lattnere6af8862010-12-02 01:58:41 +0000560 if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) ||
561 isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) ||
562 isa<llvm::ConstantExpr>(Init)) {
563 if (!Init->isNullValue())
John McCallc533cb72011-02-22 06:44:22 +0000564 Builder.CreateStore(Init, Loc, isVolatile);
Chris Lattnere6af8862010-12-02 01:58:41 +0000565 return;
566 }
Chris Lattnerb85025f2010-12-01 02:05:19 +0000567
Chris Lattnere6af8862010-12-02 01:58:41 +0000568 assert((isa<llvm::ConstantStruct>(Init) || isa<llvm::ConstantArray>(Init)) &&
569 "Unknown value type!");
570
571 for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) {
572 llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i));
573 if (Elt->isNullValue()) continue;
574
575 // Otherwise, get a pointer to the element and emit it.
576 emitStoresForInitAfterMemset(Elt, Builder.CreateConstGEP2_32(Loc, 0, i),
John McCallc533cb72011-02-22 06:44:22 +0000577 isVolatile, Builder);
Chris Lattnere6af8862010-12-02 01:58:41 +0000578 }
Chris Lattnerb85025f2010-12-01 02:05:19 +0000579}
580
581
582/// shouldUseMemSetPlusStoresToInitialize - Decide whether we should use memset
583/// plus some stores to initialize a local variable instead of using a memcpy
584/// from a constant global. It is beneficial to use memset if the global is all
585/// zeros, or mostly zeros and large.
586static bool shouldUseMemSetPlusStoresToInitialize(llvm::Constant *Init,
587 uint64_t GlobalSize) {
588 // If a global is all zeros, always use a memset.
589 if (isa<llvm::ConstantAggregateZero>(Init)) return true;
590
591
592 // If a non-zero global is <= 32 bytes, always use a memcpy. If it is large,
593 // do it if it will require 6 or fewer scalar stores.
594 // TODO: Should budget depends on the size? Avoiding a large global warrants
595 // plopping in more stores.
596 unsigned StoreBudget = 6;
597 uint64_t SizeLimit = 32;
598
599 return GlobalSize > SizeLimit &&
600 canEmitInitWithFewStoresAfterMemset(Init, StoreBudget);
601}
602
603
Nick Lewycky8a7d90b2010-12-30 20:21:55 +0000604/// EmitAutoVarDecl - Emit code and set up an entry in LocalDeclMap for a
Chris Lattner03df1222007-06-02 04:53:11 +0000605/// variable declaration with auto, register, or no storage class specifier.
Chris Lattnerb781dc792008-05-08 05:58:21 +0000606/// These turn into simple stack objects, or GlobalValues depending on target.
John McCallc533cb72011-02-22 06:44:22 +0000607void CodeGenFunction::EmitAutoVarDecl(const VarDecl &D) {
608 AutoVarEmission emission = EmitAutoVarAlloca(D);
609 EmitAutoVarInit(emission);
610 EmitAutoVarCleanups(emission);
611}
Chris Lattner03df1222007-06-02 04:53:11 +0000612
John McCallc533cb72011-02-22 06:44:22 +0000613/// EmitAutoVarAlloca - Emit the alloca and debug information for a
614/// local variable. Does not emit initalization or destruction.
615CodeGenFunction::AutoVarEmission
616CodeGenFunction::EmitAutoVarAlloca(const VarDecl &D) {
617 QualType Ty = D.getType();
618
619 AutoVarEmission emission(D);
620
621 bool isByRef = D.hasAttr<BlocksAttr>();
622 emission.IsByRef = isByRef;
623
624 CharUnits alignment = getContext().getDeclAlign(&D);
625 emission.Alignment = alignment;
626
Chris Lattner03df1222007-06-02 04:53:11 +0000627 llvm::Value *DeclPtr;
Eli Friedmana682d392008-02-15 12:20:59 +0000628 if (Ty->isConstantSizeType()) {
Chris Lattnerb781dc792008-05-08 05:58:21 +0000629 if (!Target.useGlobalsForAutomaticVariables()) {
John McCallc533cb72011-02-22 06:44:22 +0000630 bool NRVO = getContext().getLangOptions().ElideConstructors &&
631 D.isNRVOVariable();
632
633 // If this value is a POD array or struct with a statically
634 // determinable constant initializer, there are optimizations we
635 // can do.
636 // TODO: we can potentially constant-evaluate non-POD structs and
637 // arrays as long as the initialization is trivial (e.g. if they
638 // have a non-trivial destructor, but not a non-trivial constructor).
639 if (D.getInit() &&
640 (Ty->isArrayType() || Ty->isRecordType()) && Ty->isPODType() &&
John McCall8b0f4ff2010-08-02 21:13:48 +0000641 D.getInit()->isConstantInitializer(getContext(), false)) {
John McCallc533cb72011-02-22 06:44:22 +0000642
643 // If the variable's a const type, and it's neither an NRVO
644 // candidate nor a __block variable, emit it as a global instead.
645 if (CGM.getCodeGenOpts().MergeAllConstants && Ty.isConstQualified() &&
646 !NRVO && !isByRef) {
Douglas Gregor19da4e42011-03-06 23:28:21 +0000647 EmitStaticVarDecl(D, llvm::GlobalValue::InternalLinkage);
John McCallc533cb72011-02-22 06:44:22 +0000648
649 emission.Address = 0; // signal this condition to later callbacks
650 assert(emission.wasEmittedAsGlobal());
651 return emission;
Tanya Lattnerf9d41df2009-11-04 01:18:09 +0000652 }
John McCallc533cb72011-02-22 06:44:22 +0000653
654 // Otherwise, tell the initialization code that we're in this case.
655 emission.IsConstantAggregate = true;
Tanya Lattnerf9d41df2009-11-04 01:18:09 +0000656 }
657
Douglas Gregor290c93e2010-05-15 06:46:45 +0000658 // A normal fixed sized variable becomes an alloca in the entry block,
659 // unless it's an NRVO variable.
Eli Friedman3efa41a2009-03-04 04:25:14 +0000660 const llvm::Type *LTy = ConvertTypeForMem(Ty);
Douglas Gregor290c93e2010-05-15 06:46:45 +0000661
662 if (NRVO) {
663 // The named return value optimization: allocate this variable in the
664 // return slot, so that we can elide the copy when returning this
665 // variable (C++0x [class.copy]p34).
666 DeclPtr = ReturnValue;
Douglas Gregor9154b5d2010-05-17 15:52:46 +0000667
668 if (const RecordType *RecordTy = Ty->getAs<RecordType>()) {
669 if (!cast<CXXRecordDecl>(RecordTy->getDecl())->hasTrivialDestructor()) {
670 // Create a flag that is used to indicate when the NRVO was applied
671 // to this variable. Set it to zero to indicate that NRVO was not
672 // applied.
Chris Lattner46a7ad72010-12-01 01:47:15 +0000673 llvm::Value *Zero = Builder.getFalse();
John McCallc533cb72011-02-22 06:44:22 +0000674 llvm::Value *NRVOFlag = CreateTempAlloca(Zero->getType(), "nrvo");
Nick Lewycky8d222622011-02-16 23:59:08 +0000675 EnsureInsertPoint();
Douglas Gregor9154b5d2010-05-17 15:52:46 +0000676 Builder.CreateStore(Zero, NRVOFlag);
677
678 // Record the NRVO flag for this variable.
679 NRVOFlags[&D] = NRVOFlag;
John McCallc533cb72011-02-22 06:44:22 +0000680 emission.NRVOFlag = NRVOFlag;
Douglas Gregor9154b5d2010-05-17 15:52:46 +0000681 }
682 }
Douglas Gregor290c93e2010-05-15 06:46:45 +0000683 } else {
684 if (isByRef)
685 LTy = BuildByRefType(&D);
686
687 llvm::AllocaInst *Alloc = CreateTempAlloca(LTy);
688 Alloc->setName(D.getNameAsString());
Mike Stump11289f42009-09-09 15:08:12 +0000689
John McCallc533cb72011-02-22 06:44:22 +0000690 CharUnits allocaAlignment = alignment;
Douglas Gregor290c93e2010-05-15 06:46:45 +0000691 if (isByRef)
John McCallc533cb72011-02-22 06:44:22 +0000692 allocaAlignment = std::max(allocaAlignment,
Ken Dyck9a648692011-01-18 02:01:14 +0000693 getContext().toCharUnitsFromBits(Target.getPointerAlign(0)));
John McCallc533cb72011-02-22 06:44:22 +0000694 Alloc->setAlignment(allocaAlignment.getQuantity());
Douglas Gregor290c93e2010-05-15 06:46:45 +0000695 DeclPtr = Alloc;
696 }
Chris Lattnerb781dc792008-05-08 05:58:21 +0000697 } else {
698 // Targets that don't support recursion emit locals as globals.
699 const char *Class =
John McCall8e7d6562010-08-26 03:08:43 +0000700 D.getStorageClass() == SC_Register ? ".reg." : ".auto.";
John McCall1c9c3fd2010-10-15 04:57:14 +0000701 DeclPtr = CreateStaticVarDecl(D, Class,
702 llvm::GlobalValue::InternalLinkage);
Chris Lattnerb781dc792008-05-08 05:58:21 +0000703 }
Mike Stump11289f42009-09-09 15:08:12 +0000704
Daniel Dunbarb6adc432009-07-19 06:58:07 +0000705 // FIXME: Can this happen?
Anders Carlsson8a01b792008-12-20 20:46:34 +0000706 if (Ty->isVariablyModifiedType())
707 EmitVLASize(Ty);
Chris Lattner03df1222007-06-02 04:53:11 +0000708 } else {
Daniel Dunbarb6adc432009-07-19 06:58:07 +0000709 EnsureInsertPoint();
710
Anders Carlsson15949b32009-02-09 20:41:50 +0000711 if (!DidCallStackSave) {
Anders Carlsson30032882008-12-12 07:38:43 +0000712 // Save the stack.
John McCallad7c5c12011-02-08 08:22:06 +0000713 llvm::Value *Stack = CreateTempAlloca(Int8PtrTy, "saved_stack");
Mike Stump11289f42009-09-09 15:08:12 +0000714
Anders Carlsson30032882008-12-12 07:38:43 +0000715 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave);
716 llvm::Value *V = Builder.CreateCall(F);
Mike Stump11289f42009-09-09 15:08:12 +0000717
Anders Carlsson30032882008-12-12 07:38:43 +0000718 Builder.CreateStore(V, Stack);
Anders Carlsson15949b32009-02-09 20:41:50 +0000719
720 DidCallStackSave = true;
Mike Stump11289f42009-09-09 15:08:12 +0000721
John McCalla464ff92010-07-21 06:13:08 +0000722 // Push a cleanup block and restore the stack there.
John McCalle4df6c82011-01-28 08:37:24 +0000723 // FIXME: in general circumstances, this should be an EH cleanup.
John McCallcda666c2010-07-21 07:22:38 +0000724 EHStack.pushCleanup<CallStackRestore>(NormalCleanup, Stack);
Anders Carlsson30032882008-12-12 07:38:43 +0000725 }
Mike Stump11289f42009-09-09 15:08:12 +0000726
Anders Carlsson30032882008-12-12 07:38:43 +0000727 // Get the element type.
Mike Stump11289f42009-09-09 15:08:12 +0000728 const llvm::Type *LElemTy = ConvertTypeForMem(Ty);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000729 const llvm::Type *LElemPtrTy =
730 LElemTy->getPointerTo(CGM.getContext().getTargetAddressSpace(Ty));
Anders Carlsson30032882008-12-12 07:38:43 +0000731
Anders Carlsson8a01b792008-12-20 20:46:34 +0000732 llvm::Value *VLASize = EmitVLASize(Ty);
Anders Carlsson30032882008-12-12 07:38:43 +0000733
734 // Allocate memory for the array.
Anders Carlssone33eed52009-09-26 18:16:06 +0000735 llvm::AllocaInst *VLA =
John McCallad7c5c12011-02-08 08:22:06 +0000736 Builder.CreateAlloca(llvm::Type::getInt8Ty(getLLVMContext()), VLASize, "vla");
John McCallc533cb72011-02-22 06:44:22 +0000737 VLA->setAlignment(alignment.getQuantity());
Anders Carlssone33eed52009-09-26 18:16:06 +0000738
Eli Friedmanf4084702008-12-20 23:11:59 +0000739 DeclPtr = Builder.CreateBitCast(VLA, LElemPtrTy, "tmp");
Chris Lattner03df1222007-06-02 04:53:11 +0000740 }
Eli Friedmanf4084702008-12-20 23:11:59 +0000741
Chris Lattner03df1222007-06-02 04:53:11 +0000742 llvm::Value *&DMEntry = LocalDeclMap[&D];
743 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
744 DMEntry = DeclPtr;
John McCallc533cb72011-02-22 06:44:22 +0000745 emission.Address = DeclPtr;
Sanjiv Gupta18de6242008-05-30 10:30:31 +0000746
747 // Emit debug info for local var declaration.
Anders Carlsson63784f42009-02-13 08:11:52 +0000748 if (CGDebugInfo *DI = getDebugInfo()) {
Daniel Dunbar669521c2009-07-19 07:03:11 +0000749 assert(HaveInsertPoint() && "Unexpected unreachable point!");
Mike Stump11289f42009-09-09 15:08:12 +0000750
Daniel Dunbarb9fd9022008-10-17 16:15:48 +0000751 DI->setLocation(D.getLocation());
Sanjiv Gupta47e29612009-05-22 13:54:25 +0000752 if (Target.useGlobalsForAutomaticVariables()) {
753 DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(DeclPtr), &D);
Mike Stump97d01d52009-03-04 03:23:46 +0000754 } else
755 DI->EmitDeclareOfAutoVariable(&D, DeclPtr, Builder);
Sanjiv Gupta18de6242008-05-30 10:30:31 +0000756 }
757
John McCallc533cb72011-02-22 06:44:22 +0000758 return emission;
759}
760
761/// Determines whether the given __block variable is potentially
762/// captured by the given expression.
763static bool isCapturedBy(const VarDecl &var, const Expr *e) {
764 // Skip the most common kinds of expressions that make
765 // hierarchy-walking expensive.
766 e = e->IgnoreParenCasts();
767
768 if (const BlockExpr *be = dyn_cast<BlockExpr>(e)) {
769 const BlockDecl *block = be->getBlockDecl();
770 for (BlockDecl::capture_const_iterator i = block->capture_begin(),
771 e = block->capture_end(); i != e; ++i) {
772 if (i->getVariable() == &var)
773 return true;
774 }
775
776 // No need to walk into the subexpressions.
777 return false;
778 }
779
780 for (Stmt::const_child_range children = e->children(); children; ++children)
781 if (isCapturedBy(var, cast<Expr>(*children)))
782 return true;
783
784 return false;
785}
786
787void CodeGenFunction::EmitAutoVarInit(const AutoVarEmission &emission) {
John McCall9e2e22f2011-02-22 07:16:58 +0000788 assert(emission.Variable && "emission was not valid!");
789
John McCallc533cb72011-02-22 06:44:22 +0000790 // If this was emitted as a global constant, we're done.
791 if (emission.wasEmittedAsGlobal()) return;
792
John McCall9e2e22f2011-02-22 07:16:58 +0000793 const VarDecl &D = *emission.Variable;
John McCallc533cb72011-02-22 06:44:22 +0000794 QualType type = D.getType();
795
Chris Lattner07eb7332007-07-12 00:39:48 +0000796 // If this local has an initializer, emit it now.
Daniel Dunbarb6adc432009-07-19 06:58:07 +0000797 const Expr *Init = D.getInit();
798
799 // If we are at an unreachable point, we don't need to emit the initializer
800 // unless it contains a label.
801 if (!HaveInsertPoint()) {
John McCallc533cb72011-02-22 06:44:22 +0000802 if (!Init || !ContainsLabel(Init)) return;
803 EnsureInsertPoint();
Daniel Dunbarb6adc432009-07-19 06:58:07 +0000804 }
805
John McCallc533cb72011-02-22 06:44:22 +0000806 CharUnits alignment = emission.Alignment;
807
808 if (emission.IsByRef) {
Mike Stump97d01d52009-03-04 03:23:46 +0000809 llvm::Value *V;
John McCallad7c5c12011-02-08 08:22:06 +0000810
811 BlockFieldFlags fieldFlags;
812 bool fieldNeedsCopyDispose = false;
Mike Stump97d01d52009-03-04 03:23:46 +0000813
John McCallc533cb72011-02-22 06:44:22 +0000814 if (type->isBlockPointerType()) {
John McCallad7c5c12011-02-08 08:22:06 +0000815 fieldFlags |= BLOCK_FIELD_IS_BLOCK;
816 fieldNeedsCopyDispose = true;
John McCallc533cb72011-02-22 06:44:22 +0000817 } else if (getContext().isObjCNSObjectType(type) ||
818 type->isObjCObjectPointerType()) {
John McCallad7c5c12011-02-08 08:22:06 +0000819 fieldFlags |= BLOCK_FIELD_IS_OBJECT;
820 fieldNeedsCopyDispose = true;
821 } else if (getLangOptions().CPlusPlus) {
822 if (getContext().getBlockVarCopyInits(&D))
823 fieldNeedsCopyDispose = true;
John McCallc533cb72011-02-22 06:44:22 +0000824 else if (const CXXRecordDecl *record = type->getAsCXXRecordDecl())
John McCallad7c5c12011-02-08 08:22:06 +0000825 fieldNeedsCopyDispose = !record->hasTrivialDestructor();
Mike Stump97d01d52009-03-04 03:23:46 +0000826 }
Mike Stump90d8daf2009-03-07 06:04:31 +0000827
John McCallc533cb72011-02-22 06:44:22 +0000828 llvm::Value *addr = emission.Address;
829
Mike Stump90d8daf2009-03-07 06:04:31 +0000830 // FIXME: Someone double check this.
John McCallc533cb72011-02-22 06:44:22 +0000831 if (type.isObjCGCWeak())
John McCallad7c5c12011-02-08 08:22:06 +0000832 fieldFlags |= BLOCK_FIELD_IS_WEAK;
Mike Stump97d01d52009-03-04 03:23:46 +0000833
John McCallc533cb72011-02-22 06:44:22 +0000834 // Initialize the 'isa', which is just 0 or 1.
Mike Stump97d01d52009-03-04 03:23:46 +0000835 int isa = 0;
John McCallad7c5c12011-02-08 08:22:06 +0000836 if (fieldFlags & BLOCK_FIELD_IS_WEAK)
Mike Stump97d01d52009-03-04 03:23:46 +0000837 isa = 1;
John McCallad7c5c12011-02-08 08:22:06 +0000838 V = Builder.CreateIntToPtr(Builder.getInt32(isa), Int8PtrTy, "isa");
John McCallc533cb72011-02-22 06:44:22 +0000839 Builder.CreateStore(V, Builder.CreateStructGEP(addr, 0, "byref.isa"));
Mike Stump97d01d52009-03-04 03:23:46 +0000840
John McCallc533cb72011-02-22 06:44:22 +0000841 // Store the address of the variable into its own forwarding pointer.
842 Builder.CreateStore(addr,
843 Builder.CreateStructGEP(addr, 1, "byref.forwarding"));
Mike Stump97d01d52009-03-04 03:23:46 +0000844
John McCalla9a2e8a2011-02-18 02:58:31 +0000845 // Blocks ABI:
846 // c) the flags field is set to either 0 if no helper functions are
847 // needed or BLOCK_HAS_COPY_DISPOSE if they are,
848 BlockFlags flags;
849 if (fieldNeedsCopyDispose) flags |= BLOCK_HAS_COPY_DISPOSE;
850 Builder.CreateStore(llvm::ConstantInt::get(IntTy, flags.getBitMask()),
John McCallc533cb72011-02-22 06:44:22 +0000851 Builder.CreateStructGEP(addr, 2, "byref.flags"));
Mike Stump97d01d52009-03-04 03:23:46 +0000852
Mike Stump4446dcf2009-03-05 08:32:30 +0000853 const llvm::Type *V1;
John McCallc533cb72011-02-22 06:44:22 +0000854 V1 = cast<llvm::PointerType>(addr->getType())->getElementType();
John McCalla9a2e8a2011-02-18 02:58:31 +0000855 V = llvm::ConstantInt::get(IntTy, CGM.GetTargetTypeStoreSize(V1).getQuantity());
John McCallc533cb72011-02-22 06:44:22 +0000856 Builder.CreateStore(V, Builder.CreateStructGEP(addr, 3, "byref.size"));
Mike Stump97d01d52009-03-04 03:23:46 +0000857
John McCallad7c5c12011-02-08 08:22:06 +0000858 if (fieldNeedsCopyDispose) {
John McCallc533cb72011-02-22 06:44:22 +0000859 llvm::Value *copy_helper = Builder.CreateStructGEP(addr, 4);
860 Builder.CreateStore(CGM.BuildbyrefCopyHelper(addr->getType(), fieldFlags,
861 alignment.getQuantity(), &D),
Mike Stumpf89230d2009-03-06 06:12:24 +0000862 copy_helper);
Mike Stump97d01d52009-03-04 03:23:46 +0000863
John McCallc533cb72011-02-22 06:44:22 +0000864 llvm::Value *destroy_helper = Builder.CreateStructGEP(addr, 5);
865 Builder.CreateStore(CGM.BuildbyrefDestroyHelper(addr->getType(),
John McCallad7c5c12011-02-08 08:22:06 +0000866 fieldFlags,
John McCallc533cb72011-02-22 06:44:22 +0000867 alignment.getQuantity(),
868 &D),
Mike Stumpfbe25dd2009-03-06 04:53:30 +0000869 destroy_helper);
Chris Lattner9de95272007-08-26 05:13:54 +0000870 }
Chris Lattner2da04b32007-08-24 05:35:26 +0000871 }
Anders Carlssoncadb9a62009-02-07 23:51:38 +0000872
John McCallc533cb72011-02-22 06:44:22 +0000873 if (!Init) return;
Mon P Wangcc2ab0c2010-04-04 03:10:52 +0000874
John McCallc533cb72011-02-22 06:44:22 +0000875 // Check whether this is a byref variable that's potentially
876 // captured and moved by its own initializer. If so, we'll need to
877 // emit the initializer first, then copy into the variable.
878 bool capturedByInit = emission.IsByRef && isCapturedBy(D, Init);
879
880 llvm::Value *Loc =
881 capturedByInit ? emission.Address : emission.getObjectAddress(*this);
882
John McCall91ca10f2011-03-08 09:11:50 +0000883 if (!emission.IsConstantAggregate)
884 return EmitExprAsInit(Init, &D, Loc, alignment, capturedByInit);
885
John McCallc533cb72011-02-22 06:44:22 +0000886 // If this is a simple aggregate initialization, we can optimize it
887 // in various ways.
John McCall91ca10f2011-03-08 09:11:50 +0000888 assert(!capturedByInit && "constant init contains a capturing block?");
John McCallc533cb72011-02-22 06:44:22 +0000889
John McCall91ca10f2011-03-08 09:11:50 +0000890 bool isVolatile = type.isVolatileQualified();
John McCallc533cb72011-02-22 06:44:22 +0000891
John McCall91ca10f2011-03-08 09:11:50 +0000892 llvm::Constant *constant = CGM.EmitConstantExpr(D.getInit(), type, this);
893 assert(constant != 0 && "Wasn't a simple constant init?");
John McCallc533cb72011-02-22 06:44:22 +0000894
John McCall91ca10f2011-03-08 09:11:50 +0000895 llvm::Value *SizeVal =
896 llvm::ConstantInt::get(IntPtrTy,
897 getContext().getTypeSizeInChars(type).getQuantity());
John McCallc533cb72011-02-22 06:44:22 +0000898
John McCall91ca10f2011-03-08 09:11:50 +0000899 const llvm::Type *BP = Int8PtrTy;
900 if (Loc->getType() != BP)
901 Loc = Builder.CreateBitCast(Loc, BP, "tmp");
Mon P Wangcc2ab0c2010-04-04 03:10:52 +0000902
John McCall91ca10f2011-03-08 09:11:50 +0000903 // If the initializer is all or mostly zeros, codegen with memset then do
904 // a few stores afterward.
905 if (shouldUseMemSetPlusStoresToInitialize(constant,
906 CGM.getTargetData().getTypeAllocSize(constant->getType()))) {
907 Builder.CreateMemSet(Loc, llvm::ConstantInt::get(Int8Ty, 0), SizeVal,
908 alignment.getQuantity(), isVolatile);
909 if (!constant->isNullValue()) {
910 Loc = Builder.CreateBitCast(Loc, constant->getType()->getPointerTo());
911 emitStoresForInitAfterMemset(constant, Loc, isVolatile, Builder);
Fariborz Jahanianc6140732010-03-12 21:40:43 +0000912 }
John McCall91ca10f2011-03-08 09:11:50 +0000913 } else {
914 // Otherwise, create a temporary global with the initializer then
915 // memcpy from the global to the alloca.
916 std::string Name = GetStaticDeclName(*this, D, ".");
917 llvm::GlobalVariable *GV =
918 new llvm::GlobalVariable(CGM.getModule(), constant->getType(), true,
919 llvm::GlobalValue::InternalLinkage,
920 constant, Name, 0, false, 0);
921 GV->setAlignment(alignment.getQuantity());
922
923 llvm::Value *SrcPtr = GV;
924 if (SrcPtr->getType() != BP)
925 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
926
927 Builder.CreateMemCpy(Loc, SrcPtr, SizeVal, alignment.getQuantity(),
928 isVolatile);
929 }
930}
931
932/// Emit an expression as an initializer for a variable at the given
933/// location. The expression is not necessarily the normal
934/// initializer for the variable, and the address is not necessarily
935/// its normal location.
936///
937/// \param init the initializing expression
938/// \param var the variable to act as if we're initializing
939/// \param loc the address to initialize; its type is a pointer
940/// to the LLVM mapping of the variable's type
941/// \param alignment the alignment of the address
942/// \param capturedByInit true if the variable is a __block variable
943/// whose address is potentially changed by the initializer
944void CodeGenFunction::EmitExprAsInit(const Expr *init,
945 const VarDecl *var,
946 llvm::Value *loc,
947 CharUnits alignment,
948 bool capturedByInit) {
949 QualType type = var->getType();
950 bool isVolatile = type.isVolatileQualified();
951
952 if (type->isReferenceType()) {
953 RValue RV = EmitReferenceBindingToExpr(init, var);
954 if (capturedByInit) loc = BuildBlockByrefAddress(loc, var);
955 EmitStoreOfScalar(RV.getScalarVal(), loc, false,
956 alignment.getQuantity(), type);
John McCallc533cb72011-02-22 06:44:22 +0000957 } else if (!hasAggregateLLVMType(type)) {
John McCall91ca10f2011-03-08 09:11:50 +0000958 llvm::Value *V = EmitScalarExpr(init);
959 if (capturedByInit) loc = BuildBlockByrefAddress(loc, var);
960 EmitStoreOfScalar(V, loc, isVolatile, alignment.getQuantity(), type);
John McCallc533cb72011-02-22 06:44:22 +0000961 } else if (type->isAnyComplexType()) {
John McCall91ca10f2011-03-08 09:11:50 +0000962 ComplexPairTy complex = EmitComplexExpr(init);
963 if (capturedByInit) loc = BuildBlockByrefAddress(loc, var);
964 StoreComplexToAddr(complex, loc, isVolatile);
John McCallc533cb72011-02-22 06:44:22 +0000965 } else {
966 // TODO: how can we delay here if D is captured by its initializer?
John McCall91ca10f2011-03-08 09:11:50 +0000967 EmitAggExpr(init, AggValueSlot::forAddr(loc, isVolatile, true, false));
Fariborz Jahanianc6140732010-03-12 21:40:43 +0000968 }
John McCallc533cb72011-02-22 06:44:22 +0000969}
John McCallbd309292010-07-06 01:34:17 +0000970
John McCallc533cb72011-02-22 06:44:22 +0000971void CodeGenFunction::EmitAutoVarCleanups(const AutoVarEmission &emission) {
John McCall9e2e22f2011-02-22 07:16:58 +0000972 assert(emission.Variable && "emission was not valid!");
973
John McCallc533cb72011-02-22 06:44:22 +0000974 // If this was emitted as a global constant, we're done.
975 if (emission.wasEmittedAsGlobal()) return;
976
John McCall9e2e22f2011-02-22 07:16:58 +0000977 const VarDecl &D = *emission.Variable;
John McCallc533cb72011-02-22 06:44:22 +0000978
979 // Handle C++ destruction of variables.
980 if (getLangOptions().CPlusPlus) {
981 QualType type = D.getType();
982 QualType baseType = getContext().getBaseElementType(type);
983 if (const RecordType *RT = baseType->getAs<RecordType>()) {
984 CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RT->getDecl());
Douglas Gregor17012562010-05-15 16:39:56 +0000985 if (!ClassDecl->hasTrivialDestructor()) {
Douglas Gregor9154b5d2010-05-17 15:52:46 +0000986 // Note: We suppress the destructor call when the corresponding NRVO
987 // flag has been set.
John McCallc533cb72011-02-22 06:44:22 +0000988
989 // Note that for __block variables, we want to destroy the
990 // original stack object, not the possible forwarded object.
991 llvm::Value *Loc = emission.getObjectAddress(*this);
Douglas Gregor290c93e2010-05-15 06:46:45 +0000992
Douglas Gregorbac74902010-07-01 14:13:13 +0000993 const CXXDestructorDecl *D = ClassDecl->getDestructor();
Fariborz Jahanian18c06232009-08-03 20:20:07 +0000994 assert(D && "EmitLocalBlockVarDecl - destructor is nul");
Fariborz Jahanianc9076fe2009-10-29 16:22:54 +0000995
John McCallc533cb72011-02-22 06:44:22 +0000996 if (type != baseType) {
997 const ConstantArrayType *Array =
998 getContext().getAsConstantArrayType(type);
999 assert(Array && "types changed without array?");
John McCallcda666c2010-07-21 07:22:38 +00001000 EHStack.pushCleanup<CallArrayDtor>(NormalAndEHCleanup,
1001 D, Array, Loc);
Fariborz Jahanian09cc10f2009-11-04 17:57:40 +00001002 } else {
John McCallcda666c2010-07-21 07:22:38 +00001003 EHStack.pushCleanup<CallVarDtor>(NormalAndEHCleanup,
John McCallc533cb72011-02-22 06:44:22 +00001004 D, emission.NRVOFlag, Loc);
Fariborz Jahanian09cc10f2009-11-04 17:57:40 +00001005 }
Fariborz Jahanian18c06232009-08-03 20:20:07 +00001006 }
John McCallc533cb72011-02-22 06:44:22 +00001007 }
Fariborz Jahanian18c06232009-08-03 20:20:07 +00001008 }
Mike Stump11289f42009-09-09 15:08:12 +00001009
John McCallc533cb72011-02-22 06:44:22 +00001010 // Handle the cleanup attribute.
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001011 if (const CleanupAttr *CA = D.getAttr<CleanupAttr>()) {
Anders Carlssoncadb9a62009-02-07 23:51:38 +00001012 const FunctionDecl *FD = CA->getFunctionDecl();
Mike Stump11289f42009-09-09 15:08:12 +00001013
John McCallc533cb72011-02-22 06:44:22 +00001014 llvm::Constant *F = CGM.GetAddrOfFunction(FD);
Anders Carlssoncadb9a62009-02-07 23:51:38 +00001015 assert(F && "Could not find function!");
Mike Stump11289f42009-09-09 15:08:12 +00001016
Anders Carlsson6f5c1562009-04-26 00:34:20 +00001017 const CGFunctionInfo &Info = CGM.getTypes().getFunctionInfo(FD);
John McCallc533cb72011-02-22 06:44:22 +00001018 EHStack.pushCleanup<CallCleanupFunction>(NormalAndEHCleanup, F, &Info, &D);
Anders Carlssoncadb9a62009-02-07 23:51:38 +00001019 }
Mike Stump626aecc2009-03-05 01:23:13 +00001020
John McCallc533cb72011-02-22 06:44:22 +00001021 // If this is a block variable, call _Block_object_destroy
1022 // (on the unforwarded address).
1023 if (emission.IsByRef &&
1024 CGM.getLangOptions().getGCMode() != LangOptions::GCOnly)
1025 EHStack.pushCleanup<CallBlockRelease>(NormalAndEHCleanup, emission.Address);
Chris Lattner03df1222007-06-02 04:53:11 +00001026}
Chris Lattner53621a52007-06-13 20:44:40 +00001027
Mike Stump11289f42009-09-09 15:08:12 +00001028/// Emit an alloca (or GlobalValue depending on target)
Chris Lattnerb781dc792008-05-08 05:58:21 +00001029/// for the specified parameter and set up LocalDeclMap.
Devang Patel68a15252011-03-03 20:13:15 +00001030void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg,
1031 unsigned ArgNo) {
Daniel Dunbara94ecd22008-08-16 03:19:19 +00001032 // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl?
Sanjiv Guptad7959242008-10-31 09:52:39 +00001033 assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) &&
Daniel Dunbara94ecd22008-08-16 03:19:19 +00001034 "Invalid argument to EmitParmDecl");
John McCall147d0212011-02-22 22:38:33 +00001035
1036 Arg->setName(D.getName());
1037
1038 // Use better IR generation for certain implicit parameters.
1039 if (isa<ImplicitParamDecl>(D)) {
1040 // The only implicit argument a block has is its literal.
1041 if (BlockInfo) {
1042 LocalDeclMap[&D] = Arg;
1043
1044 if (CGDebugInfo *DI = getDebugInfo()) {
1045 DI->setLocation(D.getLocation());
1046 DI->EmitDeclareOfBlockLiteralArgVariable(*BlockInfo, Arg, Builder);
1047 }
1048
1049 return;
1050 }
1051 }
1052
Chris Lattnerfc4379f2008-04-06 23:10:54 +00001053 QualType Ty = D.getType();
Mike Stump11289f42009-09-09 15:08:12 +00001054
Chris Lattner53621a52007-06-13 20:44:40 +00001055 llvm::Value *DeclPtr;
Daniel Dunbar3d33fab2010-02-08 22:53:07 +00001056 // If this is an aggregate or variable sized value, reuse the input pointer.
1057 if (!Ty->isConstantSizeType() ||
1058 CodeGenFunction::hasAggregateLLVMType(Ty)) {
Chris Lattner53621a52007-06-13 20:44:40 +00001059 DeclPtr = Arg;
Chris Lattner53621a52007-06-13 20:44:40 +00001060 } else {
Daniel Dunbar3d33fab2010-02-08 22:53:07 +00001061 // Otherwise, create a temporary to hold the value.
Daniel Dunbara7566f12010-02-09 02:48:28 +00001062 DeclPtr = CreateMemTemp(Ty, D.getName() + ".addr");
Mike Stump11289f42009-09-09 15:08:12 +00001063
Daniel Dunbar3d33fab2010-02-08 22:53:07 +00001064 // Store the initial value into the alloca.
John McCalle3dc1702011-02-15 09:22:45 +00001065 EmitStoreOfScalar(Arg, DeclPtr, Ty.isVolatileQualified(),
1066 getContext().getDeclAlign(&D).getQuantity(), Ty,
1067 CGM.getTBAAInfo(Ty));
Chris Lattner53621a52007-06-13 20:44:40 +00001068 }
1069
1070 llvm::Value *&DMEntry = LocalDeclMap[&D];
1071 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
1072 DMEntry = DeclPtr;
Sanjiv Gupta18de6242008-05-30 10:30:31 +00001073
1074 // Emit debug info for param declaration.
Devang Patel3028a432009-10-09 22:06:15 +00001075 if (CGDebugInfo *DI = getDebugInfo()) {
1076 DI->setLocation(D.getLocation());
Devang Patel68a15252011-03-03 20:13:15 +00001077 DI->EmitDeclareOfArgVariable(&D, DeclPtr, ArgNo, Builder);
Devang Patel3028a432009-10-09 22:06:15 +00001078 }
Chris Lattner53621a52007-06-13 20:44:40 +00001079}