blob: 23b4ff26a2e18f13bb4f0902dc507ae5d476c06a [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Decl nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
Sanjiv Guptacc9b1632008-05-30 10:30:31 +000014#include "CGDebugInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000015#include "CodeGenFunction.h"
Anders Carlsson1a86b332007-10-17 00:52:43 +000016#include "CodeGenModule.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000017#include "clang/AST/ASTContext.h"
Ken Dyckbdc601b2009-12-22 14:23:30 +000018#include "clang/AST/CharUnits.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000019#include "clang/AST/Decl.h"
Anders Carlsson19567ee2008-08-25 01:38:19 +000020#include "clang/AST/DeclObjC.h"
Nate Begeman8bd4afe2008-04-19 04:17:09 +000021#include "clang/Basic/SourceManager.h"
Chris Lattner2621fd12008-05-08 05:58:21 +000022#include "clang/Basic/TargetInfo.h"
Chandler Carruth2811ccf2009-11-12 17:24:48 +000023#include "clang/CodeGen/CodeGenOptions.h"
Anders Carlsson1a86b332007-10-17 00:52:43 +000024#include "llvm/GlobalVariable.h"
Anders Carlsson5d463152008-12-12 07:38:43 +000025#include "llvm/Intrinsics.h"
Mike Stumpdab514f2009-03-04 03:23:46 +000026#include "llvm/Target/TargetData.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000027#include "llvm/Type.h"
28using namespace clang;
29using namespace CodeGen;
30
31
32void CodeGenFunction::EmitDecl(const Decl &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +000033 switch (D.getKind()) {
Daniel Dunbarfa133a12009-11-23 00:07:06 +000034 default:
35 CGM.ErrorUnsupported(&D, "decl");
36 return;
Chris Lattneraa9fc462007-10-08 21:37:32 +000037 case Decl::ParmVar:
Reid Spencer5f016e22007-07-11 17:01:13 +000038 assert(0 && "Parmdecls should not be in declstmts!");
Reid Spencer5f016e22007-07-11 17:01:13 +000039 case Decl::Function: // void X();
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +000040 case Decl::Record: // struct/union/class X;
Reid Spencer5f016e22007-07-11 17:01:13 +000041 case Decl::Enum: // enum X;
Mike Stump1eb44332009-09-09 15:08:12 +000042 case Decl::EnumConstant: // enum ? { X = ? }
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +000043 case Decl::CXXRecord: // struct/union/class X; [C++]
Daniel Dunbarfa133a12009-11-23 00:07:06 +000044 case Decl::Using: // using X; [C++]
45 case Decl::UsingShadow:
46 case Decl::UsingDirective: // using namespace X; [C++]
Anders Carlsson7b0ca3f2009-12-03 17:26:31 +000047 case Decl::StaticAssert: // static_assert(X, ""); [C++0x]
Reid Spencer5f016e22007-07-11 17:01:13 +000048 // None of these decls require codegen support.
49 return;
Mike Stump1eb44332009-09-09 15:08:12 +000050
Daniel Dunbar662174c82008-08-29 17:28:43 +000051 case Decl::Var: {
52 const VarDecl &VD = cast<VarDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +000053 assert(VD.isBlockVarDecl() &&
Daniel Dunbar662174c82008-08-29 17:28:43 +000054 "Should not see file-scope variables inside a function!");
55 return EmitBlockVarDecl(VD);
Reid Spencer5f016e22007-07-11 17:01:13 +000056 }
Mike Stump1eb44332009-09-09 15:08:12 +000057
Anders Carlssonfcdbb932008-12-20 21:51:53 +000058 case Decl::Typedef: { // typedef int X;
59 const TypedefDecl &TD = cast<TypedefDecl>(D);
60 QualType Ty = TD.getUnderlyingType();
Mike Stump1eb44332009-09-09 15:08:12 +000061
Anders Carlssonfcdbb932008-12-20 21:51:53 +000062 if (Ty->isVariablyModifiedType())
63 EmitVLASize(Ty);
64 }
Daniel Dunbar662174c82008-08-29 17:28:43 +000065 }
Reid Spencer5f016e22007-07-11 17:01:13 +000066}
67
68/// EmitBlockVarDecl - This method handles emission of any variable declaration
69/// inside a function, including static vars etc.
Steve Naroff248a7532008-04-15 22:42:06 +000070void CodeGenFunction::EmitBlockVarDecl(const VarDecl &D) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +000071 if (D.hasAttr<AsmLabelAttr>())
Fariborz Jahanianba8639d2009-03-30 20:32:06 +000072 CGM.ErrorUnsupported(&D, "__asm__");
Mike Stump1eb44332009-09-09 15:08:12 +000073
Reid Spencer5f016e22007-07-11 17:01:13 +000074 switch (D.getStorageClass()) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +000075 case VarDecl::None:
76 case VarDecl::Auto:
77 case VarDecl::Register:
78 return EmitLocalBlockVarDecl(D);
Reid Spencer5f016e22007-07-11 17:01:13 +000079 case VarDecl::Static:
Anders Carlsson1a86b332007-10-17 00:52:43 +000080 return EmitStaticBlockVarDecl(D);
Reid Spencer5f016e22007-07-11 17:01:13 +000081 case VarDecl::Extern:
Daniel Dunbar5466c7b2009-04-14 02:25:56 +000082 case VarDecl::PrivateExtern:
Lauro Ramos Venanciofea90b82008-02-16 22:30:38 +000083 // Don't emit it now, allow it to be emitted lazily on its first use.
84 return;
Reid Spencer5f016e22007-07-11 17:01:13 +000085 }
Daniel Dunbar5466c7b2009-04-14 02:25:56 +000086
87 assert(0 && "Unknown storage class");
Reid Spencer5f016e22007-07-11 17:01:13 +000088}
89
Chris Lattner761acc12009-12-05 08:22:11 +000090static std::string GetStaticDeclName(CodeGenFunction &CGF, const VarDecl &D,
91 const char *Separator) {
92 CodeGenModule &CGM = CGF.CGM;
93 if (CGF.getContext().getLangOptions().CPlusPlus)
94 return CGM.getMangledName(&D);
95
96 std::string ContextName;
97 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CGF.CurFuncDecl))
98 ContextName = CGM.getMangledName(FD);
99 else if (isa<ObjCMethodDecl>(CGF.CurFuncDecl))
100 ContextName = CGF.CurFn->getName();
101 else
102 // FIXME: What about in a block??
103 assert(0 && "Unknown context for block var decl");
104
105 return ContextName + Separator + D.getNameAsString();
106}
107
Daniel Dunbar0096acf2009-02-25 19:24:29 +0000108llvm::GlobalVariable *
109CodeGenFunction::CreateStaticBlockVarDecl(const VarDecl &D,
110 const char *Separator,
Chris Lattner761acc12009-12-05 08:22:11 +0000111 llvm::GlobalValue::LinkageTypes Linkage) {
Chris Lattner8bcfc5b2008-04-06 23:10:54 +0000112 QualType Ty = D.getType();
Eli Friedman3c2b3172008-02-15 12:20:59 +0000113 assert(Ty->isConstantSizeType() && "VLAs can't be static");
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000114
Chris Lattner761acc12009-12-05 08:22:11 +0000115 std::string Name = GetStaticDeclName(*this, D, Separator);
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000116
Daniel Dunbar0096acf2009-02-25 19:24:29 +0000117 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
Anders Carlsson41f8a132009-09-26 18:16:06 +0000118 llvm::GlobalVariable *GV =
119 new llvm::GlobalVariable(CGM.getModule(), LTy,
120 Ty.isConstant(getContext()), Linkage,
121 CGM.EmitNullConstant(D.getType()), Name, 0,
122 D.isThreadSpecified(), Ty.getAddressSpace());
123 GV->setAlignment(getContext().getDeclAlignInBytes(&D));
124 return GV;
Daniel Dunbar0096acf2009-02-25 19:24:29 +0000125}
126
Chris Lattner761acc12009-12-05 08:22:11 +0000127/// AddInitializerToGlobalBlockVarDecl - Add the initializer for 'D' to the
128/// global variable that has already been created for it. If the initializer
129/// has a different type than GV does, this may free GV and return a different
130/// one. Otherwise it just returns GV.
131llvm::GlobalVariable *
132CodeGenFunction::AddInitializerToGlobalBlockVarDecl(const VarDecl &D,
133 llvm::GlobalVariable *GV) {
134 llvm::Constant *Init = CGM.EmitConstantExpr(D.getInit(), D.getType(), this);
135
136 // If constant emission failed, then this should be a C++ static
137 // initializer.
138 if (!Init) {
139 if (!getContext().getLangOptions().CPlusPlus)
140 CGM.ErrorUnsupported(D.getInit(), "constant l-value expression");
Anders Carlsson071c8102010-01-26 04:02:23 +0000141 else {
142 // Since we have a static initializer, this global variable can't
143 // be constant.
144 GV->setConstant(false);
145
Chris Lattner761acc12009-12-05 08:22:11 +0000146 EmitStaticCXXBlockVarDeclInit(D, GV);
Anders Carlsson071c8102010-01-26 04:02:23 +0000147 }
Chris Lattner761acc12009-12-05 08:22:11 +0000148 return GV;
149 }
150
151 // The initializer may differ in type from the global. Rewrite
152 // the global to match the initializer. (We have to do this
153 // because some types, like unions, can't be completely represented
154 // in the LLVM type system.)
155 if (GV->getType() != Init->getType()) {
156 llvm::GlobalVariable *OldGV = GV;
157
158 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
159 OldGV->isConstant(),
160 OldGV->getLinkage(), Init, "",
161 0, D.isThreadSpecified(),
162 D.getType().getAddressSpace());
163
164 // Steal the name of the old global
165 GV->takeName(OldGV);
166
167 // Replace all uses of the old global with the new global
168 llvm::Constant *NewPtrForOldDecl =
169 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
170 OldGV->replaceAllUsesWith(NewPtrForOldDecl);
171
172 // Erase the old global, since it is no longer used.
173 OldGV->eraseFromParent();
174 }
175
176 GV->setInitializer(Init);
177 return GV;
178}
179
Mike Stump1eb44332009-09-09 15:08:12 +0000180void CodeGenFunction::EmitStaticBlockVarDecl(const VarDecl &D) {
Daniel Dunbara985b312009-02-25 19:45:19 +0000181 llvm::Value *&DMEntry = LocalDeclMap[&D];
182 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
Mike Stump1eb44332009-09-09 15:08:12 +0000183
184 llvm::GlobalVariable *GV =
Daniel Dunbara985b312009-02-25 19:45:19 +0000185 CreateStaticBlockVarDecl(D, ".", llvm::GlobalValue::InternalLinkage);
186
Daniel Dunbare5731f82009-02-25 20:08:33 +0000187 // Store into LocalDeclMap before generating initializer to handle
188 // circular references.
189 DMEntry = GV;
190
Eli Friedmanc62aad82009-04-20 03:54:15 +0000191 // Make sure to evaluate VLA bounds now so that we have them for later.
Daniel Dunbard286f052009-07-19 06:58:07 +0000192 //
193 // FIXME: Can this happen?
Eli Friedmanc62aad82009-04-20 03:54:15 +0000194 if (D.getType()->isVariablyModifiedType())
195 EmitVLASize(D.getType());
196
Chris Lattner761acc12009-12-05 08:22:11 +0000197 // If this value has an initializer, emit it.
198 if (D.getInit())
199 GV = AddInitializerToGlobalBlockVarDecl(D, GV);
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000200
Daniel Dunbar30510ab2009-02-12 23:32:54 +0000201 // FIXME: Merge attribute handling.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000202 if (const AnnotateAttr *AA = D.getAttr<AnnotateAttr>()) {
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000203 SourceManager &SM = CGM.getContext().getSourceManager();
204 llvm::Constant *Ann =
Mike Stump1eb44332009-09-09 15:08:12 +0000205 CGM.EmitAnnotateAttr(GV, AA,
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000206 SM.getInstantiationLineNumber(D.getLocation()));
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000207 CGM.AddAnnotation(Ann);
208 }
209
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000210 if (const SectionAttr *SA = D.getAttr<SectionAttr>())
Daniel Dunbar30510ab2009-02-12 23:32:54 +0000211 GV->setSection(SA->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000213 if (D.hasAttr<UsedAttr>())
Daniel Dunbar5c61d972009-02-13 22:08:43 +0000214 CGM.AddUsedGlobal(GV);
215
Daniel Dunbare5731f82009-02-25 20:08:33 +0000216 // We may have to cast the constant because of the initializer
217 // mismatch above.
218 //
219 // FIXME: It is really dangerous to store this in the map; if anyone
220 // RAUW's the GV uses of this constant will be invalid.
Eli Friedman26590522008-06-08 01:23:18 +0000221 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(D.getType());
222 const llvm::Type *LPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000223 llvm::PointerType::get(LTy, D.getType().getAddressSpace());
Owen Anderson3c4972d2009-07-29 18:54:39 +0000224 DMEntry = llvm::ConstantExpr::getBitCast(GV, LPtrTy);
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000225
226 // Emit global variable debug descriptor for static vars.
Anders Carlssone896d982009-02-13 08:11:52 +0000227 CGDebugInfo *DI = getDebugInfo();
Mike Stump4451bd92009-02-20 00:19:45 +0000228 if (DI) {
Daniel Dunbar66031a52008-10-17 16:15:48 +0000229 DI->setLocation(D.getLocation());
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000230 DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(GV), &D);
231 }
Anders Carlsson1a86b332007-10-17 00:52:43 +0000232}
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000234unsigned CodeGenFunction::getByRefValueLLVMField(const ValueDecl *VD) const {
235 assert(ByRefValueInfo.count(VD) && "Did not find value!");
236
237 return ByRefValueInfo.find(VD)->second.second;
238}
239
Mike Stumpdab514f2009-03-04 03:23:46 +0000240/// BuildByRefType - This routine changes a __block variable declared as T x
241/// into:
242///
243/// struct {
244/// void *__isa;
245/// void *__forwarding;
246/// int32_t __flags;
247/// int32_t __size;
Mike Stump39605b42009-09-22 02:12:52 +0000248/// void *__copy_helper; // only if needed
249/// void *__destroy_helper; // only if needed
250/// char padding[X]; // only if needed
Mike Stumpdab514f2009-03-04 03:23:46 +0000251/// T x;
252/// } x
253///
Anders Carlsson9ad55132009-09-09 02:51:03 +0000254const llvm::Type *CodeGenFunction::BuildByRefType(const ValueDecl *D) {
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000255 std::pair<const llvm::Type *, unsigned> &Info = ByRefValueInfo[D];
256 if (Info.first)
257 return Info.first;
258
Anders Carlsson9ad55132009-09-09 02:51:03 +0000259 QualType Ty = D->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Anders Carlsson18be84c2009-09-12 02:44:18 +0000261 std::vector<const llvm::Type *> Types;
Anders Carlsson36043862009-09-10 01:32:12 +0000262
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000263 const llvm::PointerType *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Anders Carlsson18be84c2009-09-12 02:44:18 +0000264
Anders Carlsson36043862009-09-10 01:32:12 +0000265 llvm::PATypeHolder ByRefTypeHolder = llvm::OpaqueType::get(VMContext);
266
Anders Carlsson18be84c2009-09-12 02:44:18 +0000267 // void *__isa;
268 Types.push_back(Int8PtrTy);
269
270 // void *__forwarding;
271 Types.push_back(llvm::PointerType::getUnqual(ByRefTypeHolder));
272
273 // int32_t __flags;
274 Types.push_back(llvm::Type::getInt32Ty(VMContext));
275
276 // int32_t __size;
277 Types.push_back(llvm::Type::getInt32Ty(VMContext));
278
279 bool HasCopyAndDispose = BlockRequiresCopying(Ty);
280 if (HasCopyAndDispose) {
281 /// void *__copy_helper;
282 Types.push_back(Int8PtrTy);
283
284 /// void *__destroy_helper;
285 Types.push_back(Int8PtrTy);
Mike Stumpdab514f2009-03-04 03:23:46 +0000286 }
Anders Carlsson18be84c2009-09-12 02:44:18 +0000287
288 bool Packed = false;
289 unsigned Align = getContext().getDeclAlignInBytes(D);
290 if (Align > Target.getPointerAlign(0) / 8) {
291 // We have to insert padding.
292
293 // The struct above has 2 32-bit integers.
294 unsigned CurrentOffsetInBytes = 4 * 2;
295
296 // And either 2 or 4 pointers.
297 CurrentOffsetInBytes += (HasCopyAndDispose ? 4 : 2) *
298 CGM.getTargetData().getTypeAllocSize(Int8PtrTy);
299
300 // Align the offset.
301 unsigned AlignedOffsetInBytes =
302 llvm::RoundUpToAlignment(CurrentOffsetInBytes, Align);
303
304 unsigned NumPaddingBytes = AlignedOffsetInBytes - CurrentOffsetInBytes;
Anders Carlssone0c88222009-09-13 17:55:13 +0000305 if (NumPaddingBytes > 0) {
306 const llvm::Type *Ty = llvm::Type::getInt8Ty(VMContext);
Mike Stump04c688a2009-10-21 00:42:55 +0000307 // FIXME: We need a sema error for alignment larger than the minimum of
308 // the maximal stack alignmint and the alignment of malloc on the system.
Anders Carlssone0c88222009-09-13 17:55:13 +0000309 if (NumPaddingBytes > 1)
310 Ty = llvm::ArrayType::get(Ty, NumPaddingBytes);
Anders Carlsson18be84c2009-09-12 02:44:18 +0000311
Anders Carlssone0c88222009-09-13 17:55:13 +0000312 Types.push_back(Ty);
Anders Carlsson18be84c2009-09-12 02:44:18 +0000313
Anders Carlssone0c88222009-09-13 17:55:13 +0000314 // We want a packed struct.
315 Packed = true;
316 }
Anders Carlsson18be84c2009-09-12 02:44:18 +0000317 }
318
319 // T x;
320 Types.push_back(ConvertType(Ty));
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000321
Anders Carlsson18be84c2009-09-12 02:44:18 +0000322 const llvm::Type *T = llvm::StructType::get(VMContext, Types, Packed);
Anders Carlsson36043862009-09-10 01:32:12 +0000323
324 cast<llvm::OpaqueType>(ByRefTypeHolder.get())->refineAbstractTypeTo(T);
325 CGM.getModule().addTypeName("struct.__block_byref_" + D->getNameAsString(),
326 ByRefTypeHolder.get());
327
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000328 Info.first = ByRefTypeHolder.get();
Anders Carlsson18be84c2009-09-12 02:44:18 +0000329
330 Info.second = Types.size() - 1;
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000331
332 return Info.first;
Mike Stumpdab514f2009-03-04 03:23:46 +0000333}
334
Reid Spencer5f016e22007-07-11 17:01:13 +0000335/// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a
336/// variable declaration with auto, register, or no storage class specifier.
Chris Lattner2621fd12008-05-08 05:58:21 +0000337/// These turn into simple stack objects, or GlobalValues depending on target.
Steve Naroff248a7532008-04-15 22:42:06 +0000338void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) {
Chris Lattner8bcfc5b2008-04-06 23:10:54 +0000339 QualType Ty = D.getType();
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000340 bool isByRef = D.hasAttr<BlocksAttr>();
Mike Stump797b6322009-03-05 01:23:13 +0000341 bool needsDispose = false;
Mike Stump3899a7f2009-06-05 23:26:36 +0000342 unsigned Align = 0;
Chris Lattner761acc12009-12-05 08:22:11 +0000343 bool IsSimpleConstantInitializer = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000344
345 llvm::Value *DeclPtr;
Eli Friedman3c2b3172008-02-15 12:20:59 +0000346 if (Ty->isConstantSizeType()) {
Chris Lattner2621fd12008-05-08 05:58:21 +0000347 if (!Target.useGlobalsForAutomaticVariables()) {
Tanya Lattner59876c22009-11-04 01:18:09 +0000348
Chris Lattnerff933b72009-12-05 06:49:57 +0000349 // If this value is an array or struct, is POD, and if the initializer is
350 // a staticly determinable constant, try to optimize it.
Chris Lattner761acc12009-12-05 08:22:11 +0000351 if (D.getInit() && !isByRef &&
Chris Lattnerff933b72009-12-05 06:49:57 +0000352 (Ty->isArrayType() || Ty->isRecordType()) &&
353 Ty->isPODType() &&
354 D.getInit()->isConstantInitializer(getContext())) {
Chris Lattnerff933b72009-12-05 06:49:57 +0000355 // If this variable is marked 'const', emit the value as a global.
356 if (CGM.getCodeGenOpts().MergeAllConstants &&
357 Ty.isConstant(getContext())) {
Tanya Lattner59876c22009-11-04 01:18:09 +0000358 EmitStaticBlockVarDecl(D);
359 return;
360 }
Chris Lattner761acc12009-12-05 08:22:11 +0000361
362 IsSimpleConstantInitializer = true;
Tanya Lattner59876c22009-11-04 01:18:09 +0000363 }
364
Chris Lattner2621fd12008-05-08 05:58:21 +0000365 // A normal fixed sized variable becomes an alloca in the entry block.
Eli Friedmana3460ac2009-03-04 04:25:14 +0000366 const llvm::Type *LTy = ConvertTypeForMem(Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000367 if (isByRef)
Anders Carlsson9ad55132009-09-09 02:51:03 +0000368 LTy = BuildByRefType(&D);
Chris Lattnerf1466842009-03-22 00:24:14 +0000369 llvm::AllocaInst *Alloc = CreateTempAlloca(LTy);
Benjamin Kramer155fd792009-12-08 14:04:35 +0000370 Alloc->setName(D.getNameAsString());
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Chris Lattner761acc12009-12-05 08:22:11 +0000372 Align = getContext().getDeclAlignInBytes(&D);
Mike Stumpdab514f2009-03-04 03:23:46 +0000373 if (isByRef)
Mike Stump3899a7f2009-06-05 23:26:36 +0000374 Align = std::max(Align, unsigned(Target.getPointerAlign(0) / 8));
375 Alloc->setAlignment(Align);
Eli Friedman77eedd62008-05-31 21:20:41 +0000376 DeclPtr = Alloc;
Chris Lattner2621fd12008-05-08 05:58:21 +0000377 } else {
378 // Targets that don't support recursion emit locals as globals.
379 const char *Class =
380 D.getStorageClass() == VarDecl::Register ? ".reg." : ".auto.";
Mike Stump1eb44332009-09-09 15:08:12 +0000381 DeclPtr = CreateStaticBlockVarDecl(D, Class,
Daniel Dunbara985b312009-02-25 19:45:19 +0000382 llvm::GlobalValue
383 ::InternalLinkage);
Chris Lattner2621fd12008-05-08 05:58:21 +0000384 }
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Daniel Dunbard286f052009-07-19 06:58:07 +0000386 // FIXME: Can this happen?
Anders Carlsson60d35412008-12-20 20:46:34 +0000387 if (Ty->isVariablyModifiedType())
388 EmitVLASize(Ty);
Reid Spencer5f016e22007-07-11 17:01:13 +0000389 } else {
Daniel Dunbard286f052009-07-19 06:58:07 +0000390 EnsureInsertPoint();
391
Anders Carlsson5ecb1b92009-02-09 20:41:50 +0000392 if (!DidCallStackSave) {
Anders Carlsson5d463152008-12-12 07:38:43 +0000393 // Save the stack.
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000394 const llvm::Type *LTy = llvm::Type::getInt8PtrTy(VMContext);
Anders Carlsson5d463152008-12-12 07:38:43 +0000395 llvm::Value *Stack = CreateTempAlloca(LTy, "saved_stack");
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Anders Carlsson5d463152008-12-12 07:38:43 +0000397 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave);
398 llvm::Value *V = Builder.CreateCall(F);
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Anders Carlsson5d463152008-12-12 07:38:43 +0000400 Builder.CreateStore(V, Stack);
Anders Carlsson5ecb1b92009-02-09 20:41:50 +0000401
402 DidCallStackSave = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Anders Carlsson5ecb1b92009-02-09 20:41:50 +0000404 {
405 // Push a cleanup block and restore the stack there.
Douglas Gregord5782d52009-11-24 16:21:10 +0000406 DelayedCleanupBlock scope(*this);
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Anders Carlsson5ecb1b92009-02-09 20:41:50 +0000408 V = Builder.CreateLoad(Stack, "tmp");
409 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stackrestore);
410 Builder.CreateCall(F, V);
411 }
Anders Carlsson5d463152008-12-12 07:38:43 +0000412 }
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Anders Carlsson5d463152008-12-12 07:38:43 +0000414 // Get the element type.
Mike Stump1eb44332009-09-09 15:08:12 +0000415 const llvm::Type *LElemTy = ConvertTypeForMem(Ty);
Anders Carlsson5d463152008-12-12 07:38:43 +0000416 const llvm::Type *LElemPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000417 llvm::PointerType::get(LElemTy, D.getType().getAddressSpace());
Anders Carlsson5d463152008-12-12 07:38:43 +0000418
Anders Carlsson60d35412008-12-20 20:46:34 +0000419 llvm::Value *VLASize = EmitVLASize(Ty);
Anders Carlsson5d463152008-12-12 07:38:43 +0000420
Anders Carlsson96f21472009-02-05 19:43:10 +0000421 // Downcast the VLA size expression
Owen Anderson0032b272009-08-13 21:57:51 +0000422 VLASize = Builder.CreateIntCast(VLASize, llvm::Type::getInt32Ty(VMContext),
423 false, "tmp");
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Anders Carlsson5d463152008-12-12 07:38:43 +0000425 // Allocate memory for the array.
Anders Carlsson41f8a132009-09-26 18:16:06 +0000426 llvm::AllocaInst *VLA =
427 Builder.CreateAlloca(llvm::Type::getInt8Ty(VMContext), VLASize, "vla");
428 VLA->setAlignment(getContext().getDeclAlignInBytes(&D));
429
Eli Friedman8f39f5e2008-12-20 23:11:59 +0000430 DeclPtr = Builder.CreateBitCast(VLA, LElemPtrTy, "tmp");
Reid Spencer5f016e22007-07-11 17:01:13 +0000431 }
Eli Friedman8f39f5e2008-12-20 23:11:59 +0000432
Reid Spencer5f016e22007-07-11 17:01:13 +0000433 llvm::Value *&DMEntry = LocalDeclMap[&D];
434 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
435 DMEntry = DeclPtr;
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000436
437 // Emit debug info for local var declaration.
Anders Carlssone896d982009-02-13 08:11:52 +0000438 if (CGDebugInfo *DI = getDebugInfo()) {
Daniel Dunbar25b6ebf2009-07-19 07:03:11 +0000439 assert(HaveInsertPoint() && "Unexpected unreachable point!");
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Daniel Dunbar66031a52008-10-17 16:15:48 +0000441 DI->setLocation(D.getLocation());
Sanjiv Gupta4381d4b2009-05-22 13:54:25 +0000442 if (Target.useGlobalsForAutomaticVariables()) {
443 DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(DeclPtr), &D);
Mike Stumpdab514f2009-03-04 03:23:46 +0000444 } else
445 DI->EmitDeclareOfAutoVariable(&D, DeclPtr, Builder);
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000446 }
447
Chris Lattner19785962007-07-12 00:39:48 +0000448 // If this local has an initializer, emit it now.
Daniel Dunbard286f052009-07-19 06:58:07 +0000449 const Expr *Init = D.getInit();
450
451 // If we are at an unreachable point, we don't need to emit the initializer
452 // unless it contains a label.
453 if (!HaveInsertPoint()) {
454 if (!ContainsLabel(Init))
455 Init = 0;
456 else
457 EnsureInsertPoint();
458 }
459
460 if (Init) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000461 llvm::Value *Loc = DeclPtr;
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000462 if (isByRef)
463 Loc = Builder.CreateStructGEP(DeclPtr, getByRefValueLLVMField(&D),
464 D.getNameAsString());
465
Chris Lattner761acc12009-12-05 08:22:11 +0000466 bool isVolatile =
467 getContext().getCanonicalType(D.getType()).isVolatileQualified();
468
469 // If the initializer was a simple constant initializer, we can optimize it
470 // in various ways.
471 if (IsSimpleConstantInitializer) {
472 llvm::Constant *Init = CGM.EmitConstantExpr(D.getInit(),D.getType(),this);
473 assert(Init != 0 && "Wasn't a simple constant init?");
474
475 llvm::Value *AlignVal =
476 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Align);
477 const llvm::Type *IntPtr =
478 llvm::IntegerType::get(VMContext, LLVMPointerWidth);
479 llvm::Value *SizeVal =
Ken Dyck4273f702009-12-18 15:55:54 +0000480 llvm::ConstantInt::get(IntPtr,
Ken Dyck199c3d62010-01-11 17:06:35 +0000481 getContext().getTypeSizeInChars(Ty).getQuantity());
Chris Lattner761acc12009-12-05 08:22:11 +0000482
483 const llvm::Type *BP = llvm::Type::getInt8PtrTy(VMContext);
484 if (Loc->getType() != BP)
485 Loc = Builder.CreateBitCast(Loc, BP, "tmp");
486
487 // If the initializer is all zeros, codegen with memset.
488 if (isa<llvm::ConstantAggregateZero>(Init)) {
489 llvm::Value *Zero =
490 llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 0);
491 Builder.CreateCall4(CGM.getMemSetFn(), Loc, Zero, SizeVal, AlignVal);
492 } else {
493 // Otherwise, create a temporary global with the initializer then
494 // memcpy from the global to the alloca.
495 std::string Name = GetStaticDeclName(*this, D, ".");
496 llvm::GlobalVariable *GV =
497 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), true,
498 llvm::GlobalValue::InternalLinkage,
499 Init, Name, 0, false, 0);
500 GV->setAlignment(Align);
501
502 llvm::Value *SrcPtr = GV;
503 if (SrcPtr->getType() != BP)
504 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
505
506 Builder.CreateCall4(CGM.getMemCpyFn(), Loc, SrcPtr, SizeVal, AlignVal);
507 }
508 } else if (Ty->isReferenceType()) {
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000509 RValue RV = EmitReferenceBindingToExpr(Init, Ty, /*IsInitializer=*/true);
510 EmitStoreOfScalar(RV.getScalarVal(), Loc, false, Ty);
Eli Friedman4a187842009-05-27 05:39:06 +0000511 } else if (!hasAggregateLLVMType(Init->getType())) {
Chris Lattnerd31beb12007-08-26 07:29:23 +0000512 llvm::Value *V = EmitScalarExpr(Init);
Mike Stumpdf317bf2009-11-03 23:25:48 +0000513 EmitStoreOfScalar(V, Loc, isVolatile, D.getType());
Chris Lattner9b2dc282008-04-04 16:54:41 +0000514 } else if (Init->getType()->isAnyComplexType()) {
Mike Stumpdf317bf2009-11-03 23:25:48 +0000515 EmitComplexExprIntoAddr(Init, Loc, isVolatile);
Chris Lattner9a19edf2007-08-26 05:13:54 +0000516 } else {
Mike Stumpdf317bf2009-11-03 23:25:48 +0000517 EmitAggExpr(Init, Loc, isVolatile);
Mike Stumpdab514f2009-03-04 03:23:46 +0000518 }
519 }
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Mike Stumpdab514f2009-03-04 03:23:46 +0000521 if (isByRef) {
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000522 const llvm::PointerType *PtrToInt8Ty = llvm::Type::getInt8PtrTy(VMContext);
Mike Stumpdab514f2009-03-04 03:23:46 +0000523
Daniel Dunbard286f052009-07-19 06:58:07 +0000524 EnsureInsertPoint();
Mike Stumpdab514f2009-03-04 03:23:46 +0000525 llvm::Value *isa_field = Builder.CreateStructGEP(DeclPtr, 0);
526 llvm::Value *forwarding_field = Builder.CreateStructGEP(DeclPtr, 1);
527 llvm::Value *flags_field = Builder.CreateStructGEP(DeclPtr, 2);
528 llvm::Value *size_field = Builder.CreateStructGEP(DeclPtr, 3);
529 llvm::Value *V;
530 int flag = 0;
531 int flags = 0;
532
Mike Stumpf4bc3122009-03-07 06:04:31 +0000533 needsDispose = true;
Mike Stump00470a12009-03-05 08:32:30 +0000534
Mike Stumpdab514f2009-03-04 03:23:46 +0000535 if (Ty->isBlockPointerType()) {
536 flag |= BLOCK_FIELD_IS_BLOCK;
537 flags |= BLOCK_HAS_COPY_DISPOSE;
538 } else if (BlockRequiresCopying(Ty)) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000539 flag |= BLOCK_FIELD_IS_OBJECT;
Mike Stump1851b682009-03-06 04:53:30 +0000540 flags |= BLOCK_HAS_COPY_DISPOSE;
Mike Stumpdab514f2009-03-04 03:23:46 +0000541 }
Mike Stumpf4bc3122009-03-07 06:04:31 +0000542
543 // FIXME: Someone double check this.
544 if (Ty.isObjCGCWeak())
545 flag |= BLOCK_FIELD_IS_WEAK;
Mike Stumpdab514f2009-03-04 03:23:46 +0000546
547 int isa = 0;
548 if (flag&BLOCK_FIELD_IS_WEAK)
549 isa = 1;
Owen Anderson0032b272009-08-13 21:57:51 +0000550 V = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), isa);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000551 V = Builder.CreateIntToPtr(V, PtrToInt8Ty, "isa");
Mike Stumpdab514f2009-03-04 03:23:46 +0000552 Builder.CreateStore(V, isa_field);
553
Anders Carlsson36043862009-09-10 01:32:12 +0000554 Builder.CreateStore(DeclPtr, forwarding_field);
Mike Stumpdab514f2009-03-04 03:23:46 +0000555
Owen Anderson0032b272009-08-13 21:57:51 +0000556 V = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), flags);
Mike Stumpdab514f2009-03-04 03:23:46 +0000557 Builder.CreateStore(V, flags_field);
558
Mike Stump00470a12009-03-05 08:32:30 +0000559 const llvm::Type *V1;
560 V1 = cast<llvm::PointerType>(DeclPtr->getType())->getElementType();
Owen Anderson0032b272009-08-13 21:57:51 +0000561 V = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
Mike Stump00470a12009-03-05 08:32:30 +0000562 (CGM.getTargetData().getTypeStoreSizeInBits(V1)
563 / 8));
Mike Stumpdab514f2009-03-04 03:23:46 +0000564 Builder.CreateStore(V, size_field);
565
566 if (flags & BLOCK_HAS_COPY_DISPOSE) {
Mike Stump00470a12009-03-05 08:32:30 +0000567 BlockHasCopyDispose = true;
Mike Stumpdab514f2009-03-04 03:23:46 +0000568 llvm::Value *copy_helper = Builder.CreateStructGEP(DeclPtr, 4);
Mike Stump3899a7f2009-06-05 23:26:36 +0000569 Builder.CreateStore(BuildbyrefCopyHelper(DeclPtr->getType(), flag, Align),
Mike Stumpee094222009-03-06 06:12:24 +0000570 copy_helper);
Mike Stumpdab514f2009-03-04 03:23:46 +0000571
Mike Stump1851b682009-03-06 04:53:30 +0000572 llvm::Value *destroy_helper = Builder.CreateStructGEP(DeclPtr, 5);
Mike Stump3899a7f2009-06-05 23:26:36 +0000573 Builder.CreateStore(BuildbyrefDestroyHelper(DeclPtr->getType(), flag,
574 Align),
Mike Stump1851b682009-03-06 04:53:30 +0000575 destroy_helper);
Chris Lattner9a19edf2007-08-26 05:13:54 +0000576 }
Chris Lattner7f02f722007-08-24 05:35:26 +0000577 }
Anders Carlsson69c68b72009-02-07 23:51:38 +0000578
Fariborz Jahanian6ca0b322009-08-03 20:20:07 +0000579 // Handle CXX destruction of variables.
Fariborz Jahanian667f36a2009-08-03 20:51:29 +0000580 QualType DtorTy(Ty);
Douglas Gregor89c49f02009-11-09 22:08:55 +0000581 while (const ArrayType *Array = getContext().getAsArrayType(DtorTy))
Fariborz Jahanian6fba7462009-10-29 16:22:54 +0000582 DtorTy = getContext().getBaseElementType(Array);
Fariborz Jahanian667f36a2009-08-03 20:51:29 +0000583 if (const RecordType *RT = DtorTy->getAs<RecordType>())
Fariborz Jahanian6ca0b322009-08-03 20:20:07 +0000584 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
585 if (!ClassDecl->hasTrivialDestructor()) {
586 const CXXDestructorDecl *D = ClassDecl->getDestructor(getContext());
587 assert(D && "EmitLocalBlockVarDecl - destructor is nul");
Fariborz Jahanian6fba7462009-10-29 16:22:54 +0000588
Fariborz Jahanian6fba7462009-10-29 16:22:54 +0000589 if (const ConstantArrayType *Array =
590 getContext().getAsConstantArrayType(Ty)) {
Mike Stumpd88ea562009-12-09 03:35:49 +0000591 {
592 DelayedCleanupBlock Scope(*this);
593 QualType BaseElementTy = getContext().getBaseElementType(Array);
594 const llvm::Type *BasePtr = ConvertType(BaseElementTy);
595 BasePtr = llvm::PointerType::getUnqual(BasePtr);
596 llvm::Value *BaseAddrPtr =
597 Builder.CreateBitCast(DeclPtr, BasePtr);
598 EmitCXXAggrDestructorCall(D, Array, BaseAddrPtr);
Fariborz Jahanian77996212009-11-04 17:57:40 +0000599
Mike Stumpd88ea562009-12-09 03:35:49 +0000600 // Make sure to jump to the exit block.
601 EmitBranch(Scope.getCleanupExitBlock());
602 }
603 if (Exceptions) {
604 EHCleanupBlock Cleanup(*this);
605 QualType BaseElementTy = getContext().getBaseElementType(Array);
606 const llvm::Type *BasePtr = ConvertType(BaseElementTy);
607 BasePtr = llvm::PointerType::getUnqual(BasePtr);
608 llvm::Value *BaseAddrPtr =
609 Builder.CreateBitCast(DeclPtr, BasePtr);
610 EmitCXXAggrDestructorCall(D, Array, BaseAddrPtr);
611 }
Fariborz Jahanian77996212009-11-04 17:57:40 +0000612 } else {
Mike Stumpd88ea562009-12-09 03:35:49 +0000613 {
614 DelayedCleanupBlock Scope(*this);
615 EmitCXXDestructorCall(D, Dtor_Complete, DeclPtr);
Mike Stump73020302009-12-02 23:28:08 +0000616
Mike Stumpd88ea562009-12-09 03:35:49 +0000617 // Make sure to jump to the exit block.
618 EmitBranch(Scope.getCleanupExitBlock());
619 }
620 if (Exceptions) {
621 EHCleanupBlock Cleanup(*this);
622 EmitCXXDestructorCall(D, Dtor_Complete, DeclPtr);
623 }
Fariborz Jahanian77996212009-11-04 17:57:40 +0000624 }
Fariborz Jahanian6ca0b322009-08-03 20:20:07 +0000625 }
626 }
Mike Stump1eb44332009-09-09 15:08:12 +0000627
Anders Carlsson69c68b72009-02-07 23:51:38 +0000628 // Handle the cleanup attribute
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000629 if (const CleanupAttr *CA = D.getAttr<CleanupAttr>()) {
Anders Carlsson69c68b72009-02-07 23:51:38 +0000630 const FunctionDecl *FD = CA->getFunctionDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000631
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000632 llvm::Constant* F = CGM.GetAddrOfFunction(FD);
Anders Carlsson69c68b72009-02-07 23:51:38 +0000633 assert(F && "Could not find function!");
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Anders Carlssoncabec032009-04-26 00:34:20 +0000635 const CGFunctionInfo &Info = CGM.getTypes().getFunctionInfo(FD);
636
637 // In some cases, the type of the function argument will be different from
638 // the type of the pointer. An example of this is
639 // void f(void* arg);
640 // __attribute__((cleanup(f))) void *g;
Mike Stump1eb44332009-09-09 15:08:12 +0000641 //
Anders Carlssoncabec032009-04-26 00:34:20 +0000642 // To fix this we insert a bitcast here.
643 QualType ArgTy = Info.arg_begin()->type;
Mike Stumpd88ea562009-12-09 03:35:49 +0000644 {
645 DelayedCleanupBlock scope(*this);
Mike Stump1eb44332009-09-09 15:08:12 +0000646
Mike Stumpd88ea562009-12-09 03:35:49 +0000647 CallArgList Args;
648 Args.push_back(std::make_pair(RValue::get(Builder.CreateBitCast(DeclPtr,
649 ConvertType(ArgTy))),
650 getContext().getPointerType(D.getType())));
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000651 EmitCall(Info, F, ReturnValueSlot(), Args);
Mike Stumpd88ea562009-12-09 03:35:49 +0000652 }
653 if (Exceptions) {
654 EHCleanupBlock Cleanup(*this);
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Mike Stumpd88ea562009-12-09 03:35:49 +0000656 CallArgList Args;
657 Args.push_back(std::make_pair(RValue::get(Builder.CreateBitCast(DeclPtr,
658 ConvertType(ArgTy))),
659 getContext().getPointerType(D.getType())));
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000660 EmitCall(Info, F, ReturnValueSlot(), Args);
Mike Stumpd88ea562009-12-09 03:35:49 +0000661 }
Anders Carlsson69c68b72009-02-07 23:51:38 +0000662 }
Mike Stump797b6322009-03-05 01:23:13 +0000663
Mike Stumpd9e0fae2009-03-05 02:34:38 +0000664 if (needsDispose && CGM.getLangOptions().getGCMode() != LangOptions::GCOnly) {
Mike Stumpd88ea562009-12-09 03:35:49 +0000665 {
666 DelayedCleanupBlock scope(*this);
667 llvm::Value *V = Builder.CreateStructGEP(DeclPtr, 1, "forwarding");
668 V = Builder.CreateLoad(V);
669 BuildBlockRelease(V);
670 }
671 // FIXME: Turn this on and audit the codegen
672 if (0 && Exceptions) {
673 EHCleanupBlock Cleanup(*this);
674 llvm::Value *V = Builder.CreateStructGEP(DeclPtr, 1, "forwarding");
675 V = Builder.CreateLoad(V);
676 BuildBlockRelease(V);
677 }
Mike Stump797b6322009-03-05 01:23:13 +0000678 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000679}
680
Mike Stump1eb44332009-09-09 15:08:12 +0000681/// Emit an alloca (or GlobalValue depending on target)
Chris Lattner2621fd12008-05-08 05:58:21 +0000682/// for the specified parameter and set up LocalDeclMap.
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000683void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg) {
684 // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl?
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000685 assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) &&
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000686 "Invalid argument to EmitParmDecl");
Chris Lattner8bcfc5b2008-04-06 23:10:54 +0000687 QualType Ty = D.getType();
Mike Stumpdf317bf2009-11-03 23:25:48 +0000688 CanQualType CTy = getContext().getCanonicalType(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000689
Reid Spencer5f016e22007-07-11 17:01:13 +0000690 llvm::Value *DeclPtr;
Eli Friedman3c2b3172008-02-15 12:20:59 +0000691 if (!Ty->isConstantSizeType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000692 // Variable sized values always are passed by-reference.
693 DeclPtr = Arg;
Reid Spencer5f016e22007-07-11 17:01:13 +0000694 } else {
Dan Gohmand79a7262008-05-22 22:12:56 +0000695 // A fixed sized single-value variable becomes an alloca in the entry block.
Eli Friedmana3460ac2009-03-04 04:25:14 +0000696 const llvm::Type *LTy = ConvertTypeForMem(Ty);
Dan Gohmand79a7262008-05-22 22:12:56 +0000697 if (LTy->isSingleValueType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000698 // TODO: Alignment
Chris Lattnerf1466842009-03-22 00:24:14 +0000699 DeclPtr = CreateTempAlloca(LTy);
Benjamin Kramera4d6ca12009-12-08 13:07:37 +0000700 DeclPtr->setName(D.getNameAsString() + llvm::StringRef(".addr"));
Mike Stump1eb44332009-09-09 15:08:12 +0000701
Reid Spencer5f016e22007-07-11 17:01:13 +0000702 // Store the initial value into the alloca.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000703 EmitStoreOfScalar(Arg, DeclPtr, CTy.isVolatileQualified(), Ty);
Reid Spencer5f016e22007-07-11 17:01:13 +0000704 } else {
705 // Otherwise, if this is an aggregate, just use the input pointer.
706 DeclPtr = Arg;
707 }
Chris Lattner39f34e92008-11-24 04:00:27 +0000708 Arg->setName(D.getNameAsString());
Reid Spencer5f016e22007-07-11 17:01:13 +0000709 }
710
711 llvm::Value *&DMEntry = LocalDeclMap[&D];
712 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
713 DMEntry = DeclPtr;
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000714
715 // Emit debug info for param declaration.
Devang Patelfee53aa2009-10-09 22:06:15 +0000716 if (CGDebugInfo *DI = getDebugInfo()) {
717 DI->setLocation(D.getLocation());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000718 DI->EmitDeclareOfArgVariable(&D, DeclPtr, Builder);
Devang Patelfee53aa2009-10-09 22:06:15 +0000719 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000720}