blob: fb72a9a54339ba62a69036252c2bb5da0a269a1d [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
14#include "CodeGenFunction.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000015#include "CGDebugInfo.h"
Peter Collingbourne8c25fc52011-09-19 21:14:35 +000016#include "CGOpenCLRuntime.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "CodeGenModule.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000018#include "clang/AST/ASTContext.h"
Ken Dyckbdc601b2009-12-22 14:23:30 +000019#include "clang/AST/CharUnits.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000020#include "clang/AST/Decl.h"
Anders Carlsson19567ee2008-08-25 01:38:19 +000021#include "clang/AST/DeclObjC.h"
Nate Begeman8bd4afe2008-04-19 04:17:09 +000022#include "clang/Basic/SourceManager.h"
Chris Lattner2621fd12008-05-08 05:58:21 +000023#include "clang/Basic/TargetInfo.h"
Mark Lacey8b549992013-10-30 21:53:58 +000024#include "clang/CodeGen/CGFunctionInfo.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000025#include "clang/Frontend/CodeGenOptions.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000026#include "llvm/IR/DataLayout.h"
27#include "llvm/IR/GlobalVariable.h"
28#include "llvm/IR/Intrinsics.h"
29#include "llvm/IR/Type.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000030using namespace clang;
31using namespace CodeGen;
32
33
34void CodeGenFunction::EmitDecl(const Decl &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +000035 switch (D.getKind()) {
Douglas Gregor08688ac2010-04-23 02:02:43 +000036 case Decl::TranslationUnit:
37 case Decl::Namespace:
38 case Decl::UnresolvedUsingTypename:
39 case Decl::ClassTemplateSpecialization:
40 case Decl::ClassTemplatePartialSpecialization:
Larisse Voufoef4579c2013-08-06 01:03:05 +000041 case Decl::VarTemplateSpecialization:
42 case Decl::VarTemplatePartialSpecialization:
Douglas Gregor08688ac2010-04-23 02:02:43 +000043 case Decl::TemplateTypeParm:
44 case Decl::UnresolvedUsingValue:
Sean Hunt9a555912010-05-30 07:21:58 +000045 case Decl::NonTypeTemplateParm:
Douglas Gregor08688ac2010-04-23 02:02:43 +000046 case Decl::CXXMethod:
47 case Decl::CXXConstructor:
48 case Decl::CXXDestructor:
49 case Decl::CXXConversion:
50 case Decl::Field:
John McCall76da55d2013-04-16 07:28:30 +000051 case Decl::MSProperty:
Francois Pichet41f5e662010-11-21 06:49:41 +000052 case Decl::IndirectField:
Douglas Gregor08688ac2010-04-23 02:02:43 +000053 case Decl::ObjCIvar:
Eric Christophere1f54902011-08-23 22:38:00 +000054 case Decl::ObjCAtDefsField:
Chris Lattneraa9fc462007-10-08 21:37:32 +000055 case Decl::ParmVar:
Douglas Gregor08688ac2010-04-23 02:02:43 +000056 case Decl::ImplicitParam:
57 case Decl::ClassTemplate:
Larisse Voufoef4579c2013-08-06 01:03:05 +000058 case Decl::VarTemplate:
Douglas Gregor08688ac2010-04-23 02:02:43 +000059 case Decl::FunctionTemplate:
Richard Smith3e4c6c42011-05-05 21:57:07 +000060 case Decl::TypeAliasTemplate:
Douglas Gregor08688ac2010-04-23 02:02:43 +000061 case Decl::TemplateTemplateParm:
62 case Decl::ObjCMethod:
63 case Decl::ObjCCategory:
64 case Decl::ObjCProtocol:
65 case Decl::ObjCInterface:
66 case Decl::ObjCCategoryImpl:
67 case Decl::ObjCImplementation:
68 case Decl::ObjCProperty:
69 case Decl::ObjCCompatibleAlias:
Abramo Bagnara6206d532010-06-05 05:09:32 +000070 case Decl::AccessSpec:
Douglas Gregor08688ac2010-04-23 02:02:43 +000071 case Decl::LinkageSpec:
72 case Decl::ObjCPropertyImpl:
Douglas Gregor08688ac2010-04-23 02:02:43 +000073 case Decl::FileScopeAsm:
74 case Decl::Friend:
75 case Decl::FriendTemplate:
76 case Decl::Block:
Tareq A. Siraj6afcf882013-04-16 19:37:38 +000077 case Decl::Captured:
Francois Pichetaf0f4d02011-08-14 03:52:19 +000078 case Decl::ClassScopeFunctionSpecialization:
David Blaikie9faebd22013-05-20 04:58:53 +000079 case Decl::UsingShadow:
David Blaikieb219cfc2011-09-23 05:06:16 +000080 llvm_unreachable("Declaration should not be in declstmts!");
Reid Spencer5f016e22007-07-11 17:01:13 +000081 case Decl::Function: // void X();
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +000082 case Decl::Record: // struct/union/class X;
Reid Spencer5f016e22007-07-11 17:01:13 +000083 case Decl::Enum: // enum X;
Mike Stump1eb44332009-09-09 15:08:12 +000084 case Decl::EnumConstant: // enum ? { X = ? }
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +000085 case Decl::CXXRecord: // struct/union/class X; [C++]
Anders Carlsson7b0ca3f2009-12-03 17:26:31 +000086 case Decl::StaticAssert: // static_assert(X, ""); [C++0x]
Chris Lattner4ae493c2011-02-18 02:08:43 +000087 case Decl::Label: // __label__ x;
Douglas Gregor15de72c2011-12-02 23:23:56 +000088 case Decl::Import:
Alexey Bataevc6400582013-03-22 06:34:35 +000089 case Decl::OMPThreadPrivate:
Michael Han684aa732013-02-22 17:15:32 +000090 case Decl::Empty:
Reid Spencer5f016e22007-07-11 17:01:13 +000091 // None of these decls require codegen support.
92 return;
David Blaikiec8c24272013-02-02 00:39:32 +000093
David Blaikiefc46ebc2013-05-20 22:50:41 +000094 case Decl::NamespaceAlias:
95 if (CGDebugInfo *DI = getDebugInfo())
96 DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(D));
97 return;
David Blaikie9faebd22013-05-20 04:58:53 +000098 case Decl::Using: // using X; [C++]
99 if (CGDebugInfo *DI = getDebugInfo())
100 DI->EmitUsingDecl(cast<UsingDecl>(D));
101 return;
David Blaikie957dac52013-04-22 06:13:21 +0000102 case Decl::UsingDirective: // using namespace X; [C++]
103 if (CGDebugInfo *DI = getDebugInfo())
104 DI->EmitUsingDirective(cast<UsingDirectiveDecl>(D));
105 return;
Daniel Dunbar662174c82008-08-29 17:28:43 +0000106 case Decl::Var: {
107 const VarDecl &VD = cast<VarDecl>(D);
John McCallb6bbcc92010-10-15 04:57:14 +0000108 assert(VD.isLocalVarDecl() &&
Daniel Dunbar662174c82008-08-29 17:28:43 +0000109 "Should not see file-scope variables inside a function!");
John McCallb6bbcc92010-10-15 04:57:14 +0000110 return EmitVarDecl(VD);
Reid Spencer5f016e22007-07-11 17:01:13 +0000111 }
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Richard Smith162e1c12011-04-15 14:24:37 +0000113 case Decl::Typedef: // typedef int X;
114 case Decl::TypeAlias: { // using X = int; [C++0x]
115 const TypedefNameDecl &TD = cast<TypedefNameDecl>(D);
Anders Carlssonfcdbb932008-12-20 21:51:53 +0000116 QualType Ty = TD.getUnderlyingType();
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Anders Carlssonfcdbb932008-12-20 21:51:53 +0000118 if (Ty->isVariablyModifiedType())
John McCallbc8d40d2011-06-24 21:55:10 +0000119 EmitVariablyModifiedType(Ty);
Anders Carlssonfcdbb932008-12-20 21:51:53 +0000120 }
Daniel Dunbar662174c82008-08-29 17:28:43 +0000121 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000122}
123
John McCallb6bbcc92010-10-15 04:57:14 +0000124/// EmitVarDecl - This method handles emission of any variable declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000125/// inside a function, including static vars etc.
John McCallb6bbcc92010-10-15 04:57:14 +0000126void CodeGenFunction::EmitVarDecl(const VarDecl &D) {
Enea Zaffanella713e3f22013-05-16 11:27:56 +0000127 if (D.isStaticLocal()) {
Eric Christophere1f54902011-08-23 22:38:00 +0000128 llvm::GlobalValue::LinkageTypes Linkage =
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700129 CGM.getLLVMLinkageVarDefinition(&D, /*isConstant=*/false);
Anders Carlssonf6b89a12010-02-07 02:03:08 +0000130
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700131 // FIXME: We need to force the emission/use of a guard variable for
132 // some variables even if we can constant-evaluate them because
133 // we can't guarantee every translation unit will constant-evaluate them.
Eric Christophere1f54902011-08-23 22:38:00 +0000134
John McCallb6bbcc92010-10-15 04:57:14 +0000135 return EmitStaticVarDecl(D, Linkage);
Anders Carlssonf6b89a12010-02-07 02:03:08 +0000136 }
Enea Zaffanella713e3f22013-05-16 11:27:56 +0000137
138 if (D.hasExternalStorage())
Lauro Ramos Venanciofea90b82008-02-16 22:30:38 +0000139 // Don't emit it now, allow it to be emitted lazily on its first use.
140 return;
Daniel Dunbar5466c7b2009-04-14 02:25:56 +0000141
Enea Zaffanella713e3f22013-05-16 11:27:56 +0000142 if (D.getStorageClass() == SC_OpenCLWorkGroupLocal)
143 return CGM.getOpenCLRuntime().EmitWorkGroupLocalVarDecl(*this, D);
144
145 assert(D.hasLocalStorage());
146 return EmitAutoVarDecl(D);
Reid Spencer5f016e22007-07-11 17:01:13 +0000147}
148
Stephen Hines176edba2014-12-01 14:53:08 -0800149static std::string getStaticDeclName(CodeGenModule &CGM, const VarDecl &D) {
150 if (CGM.getLangOpts().CPlusPlus)
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700151 return CGM.getMangledName(&D).str();
152
Stephen Hines176edba2014-12-01 14:53:08 -0800153 // If this isn't C++, we don't need a mangled name, just a pretty one.
154 assert(!D.isExternallyVisible() && "name shouldn't matter");
155 std::string ContextName;
156 const DeclContext *DC = D.getDeclContext();
157 if (const auto *FD = dyn_cast<FunctionDecl>(DC))
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700158 ContextName = CGM.getMangledName(FD);
Stephen Hines176edba2014-12-01 14:53:08 -0800159 else if (const auto *BD = dyn_cast<BlockDecl>(DC))
160 ContextName = CGM.getBlockMangledName(GlobalDecl(), BD);
161 else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(DC))
162 ContextName = OMD->getSelector().getAsString();
Chris Lattner761acc12009-12-05 08:22:11 +0000163 else
David Blaikieb219cfc2011-09-23 05:06:16 +0000164 llvm_unreachable("Unknown context for static var decl");
Eric Christophere1f54902011-08-23 22:38:00 +0000165
Stephen Hines176edba2014-12-01 14:53:08 -0800166 ContextName += "." + D.getNameAsString();
167 return ContextName;
Chris Lattner761acc12009-12-05 08:22:11 +0000168}
169
Stephen Hines176edba2014-12-01 14:53:08 -0800170llvm::Constant *CodeGenModule::getOrCreateStaticVarDecl(
171 const VarDecl &D, llvm::GlobalValue::LinkageTypes Linkage) {
172 // In general, we don't always emit static var decls once before we reference
173 // them. It is possible to reference them before emitting the function that
174 // contains them, and it is possible to emit the containing function multiple
175 // times.
176 if (llvm::Constant *ExistingGV = StaticLocalDeclMap[&D])
177 return ExistingGV;
178
Chris Lattner8bcfc5b2008-04-06 23:10:54 +0000179 QualType Ty = D.getType();
Eli Friedman3c2b3172008-02-15 12:20:59 +0000180 assert(Ty->isConstantSizeType() && "VLAs can't be static");
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000181
Benjamin Kramer5c247db2011-11-20 21:05:04 +0000182 // Use the label if the variable is renamed with the asm-label extension.
183 std::string Name;
Benjamin Kramerc3c8f222011-11-21 15:47:23 +0000184 if (D.hasAttr<AsmLabelAttr>())
Stephen Hines176edba2014-12-01 14:53:08 -0800185 Name = getMangledName(&D);
Benjamin Kramerc3c8f222011-11-21 15:47:23 +0000186 else
Stephen Hines176edba2014-12-01 14:53:08 -0800187 Name = getStaticDeclName(*this, D);
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000188
Stephen Hines176edba2014-12-01 14:53:08 -0800189 llvm::Type *LTy = getTypes().ConvertTypeForMem(Ty);
Peter Collingbourne1aba7782012-08-28 20:37:10 +0000190 unsigned AddrSpace =
Stephen Hines176edba2014-12-01 14:53:08 -0800191 GetGlobalVarAddressSpace(&D, getContext().getTargetAddressSpace(Ty));
192
193 // Local address space cannot have an initializer.
194 llvm::Constant *Init = nullptr;
195 if (Ty.getAddressSpace() != LangAS::opencl_local)
196 Init = EmitNullConstant(Ty);
197 else
198 Init = llvm::UndefValue::get(LTy);
199
Anders Carlsson41f8a132009-09-26 18:16:06 +0000200 llvm::GlobalVariable *GV =
Stephen Hines176edba2014-12-01 14:53:08 -0800201 new llvm::GlobalVariable(getModule(), LTy,
Anders Carlsson41f8a132009-09-26 18:16:06 +0000202 Ty.isConstant(getContext()), Linkage,
Stephen Hines176edba2014-12-01 14:53:08 -0800203 Init, Name, nullptr,
Hans Wennborgde981f32012-06-28 08:01:44 +0000204 llvm::GlobalVariable::NotThreadLocal,
Peter Collingbourne1aba7782012-08-28 20:37:10 +0000205 AddrSpace);
Ken Dyck8b752f12010-01-27 17:10:57 +0000206 GV->setAlignment(getContext().getDeclAlign(&D).getQuantity());
Stephen Hines176edba2014-12-01 14:53:08 -0800207 setGlobalVisibility(GV, &D);
Hans Wennborgde981f32012-06-28 08:01:44 +0000208
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700209 if (supportsCOMDAT() && GV->isWeakForLinker())
210 GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
211
Richard Smith38afbc72013-04-13 02:43:54 +0000212 if (D.getTLSKind())
Stephen Hines176edba2014-12-01 14:53:08 -0800213 setTLSMode(GV, D);
Hans Wennborgde981f32012-06-28 08:01:44 +0000214
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700215 if (D.isExternallyVisible()) {
216 if (D.hasAttr<DLLImportAttr>())
217 GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass);
218 else if (D.hasAttr<DLLExportAttr>())
219 GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass);
220 }
221
Stephen Hines651f13c2014-04-23 16:59:28 -0700222 // Make sure the result is of the correct type.
Stephen Hines176edba2014-12-01 14:53:08 -0800223 unsigned ExpectedAddrSpace = getContext().getTargetAddressSpace(Ty);
224 llvm::Constant *Addr = GV;
Stephen Hines651f13c2014-04-23 16:59:28 -0700225 if (AddrSpace != ExpectedAddrSpace) {
226 llvm::PointerType *PTy = llvm::PointerType::get(LTy, ExpectedAddrSpace);
Stephen Hines176edba2014-12-01 14:53:08 -0800227 Addr = llvm::ConstantExpr::getAddrSpaceCast(GV, PTy);
Stephen Hines651f13c2014-04-23 16:59:28 -0700228 }
229
Stephen Hines176edba2014-12-01 14:53:08 -0800230 setStaticLocalDeclAddress(&D, Addr);
231
232 // Ensure that the static local gets initialized by making sure the parent
233 // function gets emitted eventually.
234 const Decl *DC = cast<Decl>(D.getDeclContext());
235
236 // We can't name blocks or captured statements directly, so try to emit their
237 // parents.
238 if (isa<BlockDecl>(DC) || isa<CapturedDecl>(DC)) {
239 DC = DC->getNonClosureContext();
240 // FIXME: Ensure that global blocks get emitted.
241 if (!DC)
242 return Addr;
243 }
244
245 GlobalDecl GD;
246 if (const auto *CD = dyn_cast<CXXConstructorDecl>(DC))
247 GD = GlobalDecl(CD, Ctor_Base);
248 else if (const auto *DD = dyn_cast<CXXDestructorDecl>(DC))
249 GD = GlobalDecl(DD, Dtor_Base);
250 else if (const auto *FD = dyn_cast<FunctionDecl>(DC))
251 GD = GlobalDecl(FD);
252 else {
253 // Don't do anything for Obj-C method decls or global closures. We should
254 // never defer them.
255 assert(isa<ObjCMethodDecl>(DC) && "unexpected parent code decl");
256 }
257 if (GD.getDecl())
258 (void)GetAddrOfGlobal(GD);
259
260 return Addr;
Daniel Dunbar0096acf2009-02-25 19:24:29 +0000261}
262
Richard Smith7ca48502012-02-13 22:16:19 +0000263/// hasNontrivialDestruction - Determine whether a type's destruction is
264/// non-trivial. If so, and the variable uses static initialization, we must
265/// register its destructor to run on exit.
266static bool hasNontrivialDestruction(QualType T) {
267 CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
268 return RD && !RD->hasTrivialDestructor();
269}
270
Chandler Carruth0f30a122012-03-30 19:44:53 +0000271/// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the
272/// global variable that has already been created for it. If the initializer
273/// has a different type than GV does, this may free GV and return a different
274/// one. Otherwise it just returns GV.
275llvm::GlobalVariable *
John McCallb6bbcc92010-10-15 04:57:14 +0000276CodeGenFunction::AddInitializerToStaticVarDecl(const VarDecl &D,
Chandler Carruth0f30a122012-03-30 19:44:53 +0000277 llvm::GlobalVariable *GV) {
278 llvm::Constant *Init = CGM.EmitConstantInit(D, this);
John McCallbf40cb52010-07-15 23:40:35 +0000279
Chris Lattner761acc12009-12-05 08:22:11 +0000280 // If constant emission failed, then this should be a C++ static
281 // initializer.
Chandler Carruth0f30a122012-03-30 19:44:53 +0000282 if (!Init) {
Richard Smith7edf9e32012-11-01 22:30:59 +0000283 if (!getLangOpts().CPlusPlus)
Chris Lattner761acc12009-12-05 08:22:11 +0000284 CGM.ErrorUnsupported(D.getInit(), "constant l-value expression");
John McCall5cd91b52010-09-08 01:44:27 +0000285 else if (Builder.GetInsertBlock()) {
Eric Christophere1f54902011-08-23 22:38:00 +0000286 // Since we have a static initializer, this global variable can't
Anders Carlsson071c8102010-01-26 04:02:23 +0000287 // be constant.
Chandler Carruth0f30a122012-03-30 19:44:53 +0000288 GV->setConstant(false);
John McCall5cd91b52010-09-08 01:44:27 +0000289
Chandler Carruth0f30a122012-03-30 19:44:53 +0000290 EmitCXXGuardedInit(D, GV, /*PerformInit*/true);
Anders Carlsson071c8102010-01-26 04:02:23 +0000291 }
Chandler Carruth0f30a122012-03-30 19:44:53 +0000292 return GV;
Chris Lattner761acc12009-12-05 08:22:11 +0000293 }
John McCallbf40cb52010-07-15 23:40:35 +0000294
Chris Lattner761acc12009-12-05 08:22:11 +0000295 // The initializer may differ in type from the global. Rewrite
296 // the global to match the initializer. (We have to do this
297 // because some types, like unions, can't be completely represented
298 // in the LLVM type system.)
Chandler Carruth0f30a122012-03-30 19:44:53 +0000299 if (GV->getType()->getElementType() != Init->getType()) {
300 llvm::GlobalVariable *OldGV = GV;
301
302 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
303 OldGV->isConstant(),
304 OldGV->getLinkage(), Init, "",
305 /*InsertBefore*/ OldGV,
Hans Wennborg5e2d5de2012-06-23 11:51:46 +0000306 OldGV->getThreadLocalMode(),
Chandler Carruth0f30a122012-03-30 19:44:53 +0000307 CGM.getContext().getTargetAddressSpace(D.getType()));
308 GV->setVisibility(OldGV->getVisibility());
Eric Christophere1f54902011-08-23 22:38:00 +0000309
Chris Lattner761acc12009-12-05 08:22:11 +0000310 // Steal the name of the old global
Chandler Carruth0f30a122012-03-30 19:44:53 +0000311 GV->takeName(OldGV);
Eric Christophere1f54902011-08-23 22:38:00 +0000312
Chris Lattner761acc12009-12-05 08:22:11 +0000313 // Replace all uses of the old global with the new global
Chandler Carruth0f30a122012-03-30 19:44:53 +0000314 llvm::Constant *NewPtrForOldDecl =
315 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
316 OldGV->replaceAllUsesWith(NewPtrForOldDecl);
Eric Christophere1f54902011-08-23 22:38:00 +0000317
Chris Lattner761acc12009-12-05 08:22:11 +0000318 // Erase the old global, since it is no longer used.
Chandler Carruth0f30a122012-03-30 19:44:53 +0000319 OldGV->eraseFromParent();
Chris Lattner761acc12009-12-05 08:22:11 +0000320 }
Eric Christophere1f54902011-08-23 22:38:00 +0000321
Chandler Carruth0f30a122012-03-30 19:44:53 +0000322 GV->setConstant(CGM.isTypeConstant(D.getType(), true));
323 GV->setInitializer(Init);
Richard Smith7ca48502012-02-13 22:16:19 +0000324
325 if (hasNontrivialDestruction(D.getType())) {
326 // We have a constant initializer, but a nontrivial destructor. We still
327 // need to perform a guarded "initialization" in order to register the
Richard Smitha9b21d22012-02-17 06:48:11 +0000328 // destructor.
Chandler Carruth0f30a122012-03-30 19:44:53 +0000329 EmitCXXGuardedInit(D, GV, /*PerformInit*/false);
Richard Smith7ca48502012-02-13 22:16:19 +0000330 }
331
Chandler Carruth0f30a122012-03-30 19:44:53 +0000332 return GV;
Chris Lattner761acc12009-12-05 08:22:11 +0000333}
334
John McCallb6bbcc92010-10-15 04:57:14 +0000335void CodeGenFunction::EmitStaticVarDecl(const VarDecl &D,
Anders Carlssonf6b89a12010-02-07 02:03:08 +0000336 llvm::GlobalValue::LinkageTypes Linkage) {
Chandler Carruth0f30a122012-03-30 19:44:53 +0000337 llvm::Value *&DMEntry = LocalDeclMap[&D];
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700338 assert(!DMEntry && "Decl already exists in localdeclmap!");
Mike Stump1eb44332009-09-09 15:08:12 +0000339
John McCall355bba72012-03-30 21:00:39 +0000340 // Check to see if we already have a global variable for this
341 // declaration. This can happen when double-emitting function
342 // bodies, e.g. with complete and base constructors.
Stephen Hines176edba2014-12-01 14:53:08 -0800343 llvm::Constant *addr = CGM.getOrCreateStaticVarDecl(D, Linkage);
Daniel Dunbara985b312009-02-25 19:45:19 +0000344
Daniel Dunbare5731f82009-02-25 20:08:33 +0000345 // Store into LocalDeclMap before generating initializer to handle
346 // circular references.
John McCall355bba72012-03-30 21:00:39 +0000347 DMEntry = addr;
Daniel Dunbare5731f82009-02-25 20:08:33 +0000348
John McCallfe67f3b2010-05-04 20:45:42 +0000349 // We can't have a VLA here, but we can have a pointer to a VLA,
350 // even though that doesn't really make any sense.
Eli Friedmanc62aad82009-04-20 03:54:15 +0000351 // Make sure to evaluate VLA bounds now so that we have them for later.
352 if (D.getType()->isVariablyModifiedType())
John McCallbc8d40d2011-06-24 21:55:10 +0000353 EmitVariablyModifiedType(D.getType());
Eric Christophere1f54902011-08-23 22:38:00 +0000354
John McCall355bba72012-03-30 21:00:39 +0000355 // Save the type in case adding the initializer forces a type change.
356 llvm::Type *expectedType = addr->getType();
Eli Friedmanc62aad82009-04-20 03:54:15 +0000357
Stephen Hines651f13c2014-04-23 16:59:28 -0700358 llvm::GlobalVariable *var =
359 cast<llvm::GlobalVariable>(addr->stripPointerCasts());
Chris Lattner761acc12009-12-05 08:22:11 +0000360 // If this value has an initializer, emit it.
361 if (D.getInit())
John McCall355bba72012-03-30 21:00:39 +0000362 var = AddInitializerToStaticVarDecl(D, var);
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000363
John McCall355bba72012-03-30 21:00:39 +0000364 var->setAlignment(getContext().getDeclAlign(&D).getQuantity());
Chris Lattner0af95232010-03-10 23:59:59 +0000365
Julien Lerouge77f68bb2011-09-09 22:41:49 +0000366 if (D.hasAttr<AnnotateAttr>())
John McCall355bba72012-03-30 21:00:39 +0000367 CGM.AddGlobalAnnotations(&D, var);
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000368
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000369 if (const SectionAttr *SA = D.getAttr<SectionAttr>())
John McCall355bba72012-03-30 21:00:39 +0000370 var->setSection(SA->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000372 if (D.hasAttr<UsedAttr>())
Stephen Hines651f13c2014-04-23 16:59:28 -0700373 CGM.addUsedGlobal(var);
Daniel Dunbar5c61d972009-02-13 22:08:43 +0000374
Chandler Carruth0f30a122012-03-30 19:44:53 +0000375 // We may have to cast the constant because of the initializer
376 // mismatch above.
377 //
378 // FIXME: It is really dangerous to store this in the map; if anyone
379 // RAUW's the GV uses of this constant will be invalid.
Stephen Hines651f13c2014-04-23 16:59:28 -0700380 llvm::Constant *castedAddr =
381 llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(var, expectedType);
John McCall355bba72012-03-30 21:00:39 +0000382 DMEntry = castedAddr;
383 CGM.setStaticLocalDeclAddress(&D, castedAddr);
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000384
Stephen Hines176edba2014-12-01 14:53:08 -0800385 CGM.getSanitizerMetadata()->reportGlobalToASan(var, D);
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700386
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000387 // Emit global variable debug descriptor for static vars.
Anders Carlssone896d982009-02-13 08:11:52 +0000388 CGDebugInfo *DI = getDebugInfo();
Alexey Samsonovfd00eec2012-05-04 07:39:27 +0000389 if (DI &&
Douglas Gregor4cdad312012-10-23 20:05:01 +0000390 CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo) {
Daniel Dunbar66031a52008-10-17 16:15:48 +0000391 DI->setLocation(D.getLocation());
John McCall355bba72012-03-30 21:00:39 +0000392 DI->EmitGlobalVariable(var, &D);
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000393 }
Anders Carlsson1a86b332007-10-17 00:52:43 +0000394}
Mike Stump1eb44332009-09-09 15:08:12 +0000395
John McCallda65ea82010-07-13 20:32:21 +0000396namespace {
John McCallbdc4d802011-07-09 01:37:26 +0000397 struct DestroyObject : EHScopeStack::Cleanup {
398 DestroyObject(llvm::Value *addr, QualType type,
John McCall2673c682011-07-11 08:38:19 +0000399 CodeGenFunction::Destroyer *destroyer,
400 bool useEHCleanupForArray)
Peter Collingbourne516bbd42012-01-26 03:33:36 +0000401 : addr(addr), type(type), destroyer(destroyer),
John McCall2673c682011-07-11 08:38:19 +0000402 useEHCleanupForArray(useEHCleanupForArray) {}
John McCallda65ea82010-07-13 20:32:21 +0000403
John McCallbdc4d802011-07-09 01:37:26 +0000404 llvm::Value *addr;
405 QualType type;
Peter Collingbourne516bbd42012-01-26 03:33:36 +0000406 CodeGenFunction::Destroyer *destroyer;
John McCall2673c682011-07-11 08:38:19 +0000407 bool useEHCleanupForArray;
John McCallda65ea82010-07-13 20:32:21 +0000408
Stephen Hines651f13c2014-04-23 16:59:28 -0700409 void Emit(CodeGenFunction &CGF, Flags flags) override {
John McCall2673c682011-07-11 08:38:19 +0000410 // Don't use an EH cleanup recursively from an EH cleanup.
John McCallad346f42011-07-12 20:27:29 +0000411 bool useEHCleanupForArray =
412 flags.isForNormalCleanup() && this->useEHCleanupForArray;
John McCall2673c682011-07-11 08:38:19 +0000413
414 CGF.emitDestroy(addr, type, destroyer, useEHCleanupForArray);
John McCallda65ea82010-07-13 20:32:21 +0000415 }
416 };
417
John McCallbdc4d802011-07-09 01:37:26 +0000418 struct DestroyNRVOVariable : EHScopeStack::Cleanup {
419 DestroyNRVOVariable(llvm::Value *addr,
420 const CXXDestructorDecl *Dtor,
421 llvm::Value *NRVOFlag)
422 : Dtor(Dtor), NRVOFlag(NRVOFlag), Loc(addr) {}
John McCallda65ea82010-07-13 20:32:21 +0000423
424 const CXXDestructorDecl *Dtor;
425 llvm::Value *NRVOFlag;
426 llvm::Value *Loc;
427
Stephen Hines651f13c2014-04-23 16:59:28 -0700428 void Emit(CodeGenFunction &CGF, Flags flags) override {
John McCallda65ea82010-07-13 20:32:21 +0000429 // Along the exceptions path we always execute the dtor.
John McCallad346f42011-07-12 20:27:29 +0000430 bool NRVO = flags.isForNormalCleanup() && NRVOFlag;
John McCallda65ea82010-07-13 20:32:21 +0000431
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700432 llvm::BasicBlock *SkipDtorBB = nullptr;
John McCallda65ea82010-07-13 20:32:21 +0000433 if (NRVO) {
434 // If we exited via NRVO, we skip the destructor call.
435 llvm::BasicBlock *RunDtorBB = CGF.createBasicBlock("nrvo.unused");
436 SkipDtorBB = CGF.createBasicBlock("nrvo.skipdtor");
437 llvm::Value *DidNRVO = CGF.Builder.CreateLoad(NRVOFlag, "nrvo.val");
438 CGF.Builder.CreateCondBr(DidNRVO, SkipDtorBB, RunDtorBB);
439 CGF.EmitBlock(RunDtorBB);
440 }
Eric Christophere1f54902011-08-23 22:38:00 +0000441
John McCallda65ea82010-07-13 20:32:21 +0000442 CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete,
Douglas Gregor378e1e72013-01-31 05:50:40 +0000443 /*ForVirtualBase=*/false,
444 /*Delegating=*/false,
445 Loc);
John McCallda65ea82010-07-13 20:32:21 +0000446
447 if (NRVO) CGF.EmitBlock(SkipDtorBB);
448 }
449 };
John McCallda65ea82010-07-13 20:32:21 +0000450
John McCall1f0fca52010-07-21 07:22:38 +0000451 struct CallStackRestore : EHScopeStack::Cleanup {
John McCalld8715092010-07-21 06:13:08 +0000452 llvm::Value *Stack;
453 CallStackRestore(llvm::Value *Stack) : Stack(Stack) {}
Stephen Hines651f13c2014-04-23 16:59:28 -0700454 void Emit(CodeGenFunction &CGF, Flags flags) override {
Benjamin Kramer578faa82011-09-27 21:06:10 +0000455 llvm::Value *V = CGF.Builder.CreateLoad(Stack);
John McCalld8715092010-07-21 06:13:08 +0000456 llvm::Value *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stackrestore);
457 CGF.Builder.CreateCall(F, V);
458 }
459 };
460
John McCall0c24c802011-06-24 23:21:27 +0000461 struct ExtendGCLifetime : EHScopeStack::Cleanup {
462 const VarDecl &Var;
463 ExtendGCLifetime(const VarDecl *var) : Var(*var) {}
464
Stephen Hines651f13c2014-04-23 16:59:28 -0700465 void Emit(CodeGenFunction &CGF, Flags flags) override {
John McCall0c24c802011-06-24 23:21:27 +0000466 // Compute the address of the local variable, in case it's a
467 // byref or something.
John McCallf4b88a42012-03-10 09:33:50 +0000468 DeclRefExpr DRE(const_cast<VarDecl*>(&Var), false,
469 Var.getType(), VK_LValue, SourceLocation());
Nick Lewycky4ee7dc22013-10-02 02:29:49 +0000470 llvm::Value *value = CGF.EmitLoadOfScalar(CGF.EmitDeclRefLValue(&DRE),
471 SourceLocation());
John McCall0c24c802011-06-24 23:21:27 +0000472 CGF.EmitExtendGCLifetime(value);
473 }
474 };
475
John McCall1f0fca52010-07-21 07:22:38 +0000476 struct CallCleanupFunction : EHScopeStack::Cleanup {
John McCalld8715092010-07-21 06:13:08 +0000477 llvm::Constant *CleanupFn;
478 const CGFunctionInfo &FnInfo;
John McCalld8715092010-07-21 06:13:08 +0000479 const VarDecl &Var;
Eric Christophere1f54902011-08-23 22:38:00 +0000480
John McCalld8715092010-07-21 06:13:08 +0000481 CallCleanupFunction(llvm::Constant *CleanupFn, const CGFunctionInfo *Info,
John McCall34695852011-02-22 06:44:22 +0000482 const VarDecl *Var)
483 : CleanupFn(CleanupFn), FnInfo(*Info), Var(*Var) {}
John McCalld8715092010-07-21 06:13:08 +0000484
Stephen Hines651f13c2014-04-23 16:59:28 -0700485 void Emit(CodeGenFunction &CGF, Flags flags) override {
John McCallf4b88a42012-03-10 09:33:50 +0000486 DeclRefExpr DRE(const_cast<VarDecl*>(&Var), false,
487 Var.getType(), VK_LValue, SourceLocation());
John McCall34695852011-02-22 06:44:22 +0000488 // 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 McCalld8715092010-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;
Eli Friedman04c9a492011-05-02 17:57:46 +0000503 Args.add(RValue::get(Arg),
504 CGF.getContext().getPointerType(Var.getType()));
John McCalld8715092010-07-21 06:13:08 +0000505 CGF.EmitCall(FnInfo, CleanupFn, ReturnValueSlot(), Args);
506 }
507 };
Nadav Rotem495cfa42013-03-23 06:43:35 +0000508
509 /// A cleanup to call @llvm.lifetime.end.
510 class CallLifetimeEnd : public EHScopeStack::Cleanup {
511 llvm::Value *Addr;
512 llvm::Value *Size;
513 public:
514 CallLifetimeEnd(llvm::Value *addr, llvm::Value *size)
515 : Addr(addr), Size(size) {}
516
Stephen Hines651f13c2014-04-23 16:59:28 -0700517 void Emit(CodeGenFunction &CGF, Flags flags) override {
Nadav Rotem495cfa42013-03-23 06:43:35 +0000518 llvm::Value *castAddr = CGF.Builder.CreateBitCast(Addr, CGF.Int8PtrTy);
519 CGF.Builder.CreateCall2(CGF.CGM.getLLVMLifetimeEndFn(),
520 Size, castAddr)
521 ->setDoesNotThrow();
522 }
523 };
John McCalld8715092010-07-21 06:13:08 +0000524}
525
John McCallf85e1932011-06-15 23:02:42 +0000526/// EmitAutoVarWithLifetime - Does the setup required for an automatic
527/// variable with lifetime.
528static void EmitAutoVarWithLifetime(CodeGenFunction &CGF, const VarDecl &var,
529 llvm::Value *addr,
530 Qualifiers::ObjCLifetime lifetime) {
531 switch (lifetime) {
532 case Qualifiers::OCL_None:
533 llvm_unreachable("present but none");
534
535 case Qualifiers::OCL_ExplicitNone:
536 // nothing to do
537 break;
538
539 case Qualifiers::OCL_Strong: {
Peter Collingbourne516bbd42012-01-26 03:33:36 +0000540 CodeGenFunction::Destroyer *destroyer =
John McCall9928c482011-07-12 16:41:08 +0000541 (var.hasAttr<ObjCPreciseLifetimeAttr>()
542 ? CodeGenFunction::destroyARCStrongPrecise
543 : CodeGenFunction::destroyARCStrongImprecise);
544
545 CleanupKind cleanupKind = CGF.getARCCleanupKind();
546 CGF.pushDestroy(cleanupKind, addr, var.getType(), destroyer,
547 cleanupKind & EHCleanup);
John McCallf85e1932011-06-15 23:02:42 +0000548 break;
549 }
550 case Qualifiers::OCL_Autoreleasing:
551 // nothing to do
552 break;
Eric Christophere1f54902011-08-23 22:38:00 +0000553
John McCallf85e1932011-06-15 23:02:42 +0000554 case Qualifiers::OCL_Weak:
555 // __weak objects always get EH cleanups; otherwise, exceptions
556 // could cause really nasty crashes instead of mere leaks.
John McCall9928c482011-07-12 16:41:08 +0000557 CGF.pushDestroy(NormalAndEHCleanup, addr, var.getType(),
558 CodeGenFunction::destroyARCWeak,
559 /*useEHCleanup*/ true);
John McCallf85e1932011-06-15 23:02:42 +0000560 break;
561 }
562}
563
564static bool isAccessedBy(const VarDecl &var, const Stmt *s) {
565 if (const Expr *e = dyn_cast<Expr>(s)) {
566 // Skip the most common kinds of expressions that make
567 // hierarchy-walking expensive.
568 s = e = e->IgnoreParenCasts();
569
570 if (const DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e))
571 return (ref->getDecl() == &var);
Fariborz Jahanianddfc8a12012-06-19 20:53:26 +0000572 if (const BlockExpr *be = dyn_cast<BlockExpr>(e)) {
573 const BlockDecl *block = be->getBlockDecl();
Stephen Hines651f13c2014-04-23 16:59:28 -0700574 for (const auto &I : block->captures()) {
575 if (I.getVariable() == &var)
Fariborz Jahanianddfc8a12012-06-19 20:53:26 +0000576 return true;
577 }
578 }
John McCallf85e1932011-06-15 23:02:42 +0000579 }
580
581 for (Stmt::const_child_range children = s->children(); children; ++children)
Fariborz Jahanian8fefc8e2011-06-29 20:00:16 +0000582 // children might be null; as in missing decl or conditional of an if-stmt.
583 if ((*children) && isAccessedBy(var, *children))
John McCallf85e1932011-06-15 23:02:42 +0000584 return true;
585
586 return false;
587}
588
589static bool isAccessedBy(const ValueDecl *decl, const Expr *e) {
590 if (!decl) return false;
591 if (!isa<VarDecl>(decl)) return false;
592 const VarDecl *var = cast<VarDecl>(decl);
593 return isAccessedBy(*var, e);
594}
595
John McCalla07398e2011-06-16 04:16:24 +0000596static void drillIntoBlockVariable(CodeGenFunction &CGF,
597 LValue &lvalue,
598 const VarDecl *var) {
599 lvalue.setAddress(CGF.BuildBlockByrefAddress(lvalue.getAddress(), var));
600}
601
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700602void CodeGenFunction::EmitScalarInit(const Expr *init, const ValueDecl *D,
603 LValue lvalue, bool capturedByInit) {
John McCalla07398e2011-06-16 04:16:24 +0000604 Qualifiers::ObjCLifetime lifetime = lvalue.getObjCLifetime();
John McCallf85e1932011-06-15 23:02:42 +0000605 if (!lifetime) {
606 llvm::Value *value = EmitScalarExpr(init);
John McCalla07398e2011-06-16 04:16:24 +0000607 if (capturedByInit)
608 drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
David Chisnall7a7ee302012-01-16 17:27:18 +0000609 EmitStoreThroughLValue(RValue::get(value), lvalue, true);
John McCallf85e1932011-06-15 23:02:42 +0000610 return;
611 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700612
613 if (const CXXDefaultInitExpr *DIE = dyn_cast<CXXDefaultInitExpr>(init))
614 init = DIE->getExpr();
615
John McCallf85e1932011-06-15 23:02:42 +0000616 // If we're emitting a value with lifetime, we have to do the
617 // initialization *before* we leave the cleanup scopes.
John McCall1a343eb2011-11-10 08:15:53 +0000618 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(init)) {
619 enterFullExpression(ewc);
John McCallf85e1932011-06-15 23:02:42 +0000620 init = ewc->getSubExpr();
John McCall1a343eb2011-11-10 08:15:53 +0000621 }
622 CodeGenFunction::RunCleanupsScope Scope(*this);
John McCallf85e1932011-06-15 23:02:42 +0000623
624 // We have to maintain the illusion that the variable is
625 // zero-initialized. If the variable might be accessed in its
626 // initializer, zero-initialize before running the initializer, then
627 // actually perform the initialization with an assign.
628 bool accessedByInit = false;
629 if (lifetime != Qualifiers::OCL_ExplicitNone)
John McCallfb720812011-07-28 07:23:35 +0000630 accessedByInit = (capturedByInit || isAccessedBy(D, init));
John McCallf85e1932011-06-15 23:02:42 +0000631 if (accessedByInit) {
John McCalla07398e2011-06-16 04:16:24 +0000632 LValue tempLV = lvalue;
John McCallf85e1932011-06-15 23:02:42 +0000633 // Drill down to the __block object if necessary.
John McCallf85e1932011-06-15 23:02:42 +0000634 if (capturedByInit) {
635 // We can use a simple GEP for this because it can't have been
636 // moved yet.
John McCalla07398e2011-06-16 04:16:24 +0000637 tempLV.setAddress(Builder.CreateStructGEP(tempLV.getAddress(),
638 getByRefValueLLVMField(cast<VarDecl>(D))));
John McCallf85e1932011-06-15 23:02:42 +0000639 }
640
Chris Lattner2acc6e32011-07-18 04:24:23 +0000641 llvm::PointerType *ty
John McCalla07398e2011-06-16 04:16:24 +0000642 = cast<llvm::PointerType>(tempLV.getAddress()->getType());
John McCallf85e1932011-06-15 23:02:42 +0000643 ty = cast<llvm::PointerType>(ty->getElementType());
644
645 llvm::Value *zero = llvm::ConstantPointerNull::get(ty);
Eric Christophere1f54902011-08-23 22:38:00 +0000646
John McCallf85e1932011-06-15 23:02:42 +0000647 // If __weak, we want to use a barrier under certain conditions.
648 if (lifetime == Qualifiers::OCL_Weak)
John McCalla07398e2011-06-16 04:16:24 +0000649 EmitARCInitWeak(tempLV.getAddress(), zero);
John McCallf85e1932011-06-15 23:02:42 +0000650
651 // Otherwise just do a simple store.
652 else
David Chisnall7a7ee302012-01-16 17:27:18 +0000653 EmitStoreOfScalar(zero, tempLV, /* isInitialization */ true);
John McCallf85e1932011-06-15 23:02:42 +0000654 }
655
656 // Emit the initializer.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700657 llvm::Value *value = nullptr;
John McCallf85e1932011-06-15 23:02:42 +0000658
659 switch (lifetime) {
660 case Qualifiers::OCL_None:
661 llvm_unreachable("present but none");
662
663 case Qualifiers::OCL_ExplicitNone:
664 // nothing to do
665 value = EmitScalarExpr(init);
666 break;
667
668 case Qualifiers::OCL_Strong: {
669 value = EmitARCRetainScalarExpr(init);
670 break;
671 }
672
673 case Qualifiers::OCL_Weak: {
674 // No way to optimize a producing initializer into this. It's not
675 // worth optimizing for, because the value will immediately
676 // disappear in the common case.
677 value = EmitScalarExpr(init);
678
John McCalla07398e2011-06-16 04:16:24 +0000679 if (capturedByInit) drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
John McCallf85e1932011-06-15 23:02:42 +0000680 if (accessedByInit)
John McCalla07398e2011-06-16 04:16:24 +0000681 EmitARCStoreWeak(lvalue.getAddress(), value, /*ignored*/ true);
John McCallf85e1932011-06-15 23:02:42 +0000682 else
John McCalla07398e2011-06-16 04:16:24 +0000683 EmitARCInitWeak(lvalue.getAddress(), value);
John McCallf85e1932011-06-15 23:02:42 +0000684 return;
685 }
686
687 case Qualifiers::OCL_Autoreleasing:
688 value = EmitARCRetainAutoreleaseScalarExpr(init);
689 break;
690 }
691
John McCalla07398e2011-06-16 04:16:24 +0000692 if (capturedByInit) drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
John McCallf85e1932011-06-15 23:02:42 +0000693
694 // If the variable might have been accessed by its initializer, we
695 // might have to initialize with a barrier. We have to do this for
696 // both __weak and __strong, but __weak got filtered out above.
697 if (accessedByInit && lifetime == Qualifiers::OCL_Strong) {
Nick Lewycky4ee7dc22013-10-02 02:29:49 +0000698 llvm::Value *oldValue = EmitLoadOfScalar(lvalue, init->getExprLoc());
David Chisnall7a7ee302012-01-16 17:27:18 +0000699 EmitStoreOfScalar(value, lvalue, /* isInitialization */ true);
John McCall5b07e802013-03-13 03:10:54 +0000700 EmitARCRelease(oldValue, ARCImpreciseLifetime);
John McCallf85e1932011-06-15 23:02:42 +0000701 return;
702 }
703
David Chisnall7a7ee302012-01-16 17:27:18 +0000704 EmitStoreOfScalar(value, lvalue, /* isInitialization */ true);
John McCallf85e1932011-06-15 23:02:42 +0000705}
Chris Lattner94cd0112010-12-01 02:05:19 +0000706
John McCall7acddac2011-06-17 06:42:21 +0000707/// EmitScalarInit - Initialize the given lvalue with the given object.
708void CodeGenFunction::EmitScalarInit(llvm::Value *init, LValue lvalue) {
709 Qualifiers::ObjCLifetime lifetime = lvalue.getObjCLifetime();
710 if (!lifetime)
David Chisnall7a7ee302012-01-16 17:27:18 +0000711 return EmitStoreThroughLValue(RValue::get(init), lvalue, true);
John McCall7acddac2011-06-17 06:42:21 +0000712
713 switch (lifetime) {
714 case Qualifiers::OCL_None:
715 llvm_unreachable("present but none");
716
717 case Qualifiers::OCL_ExplicitNone:
718 // nothing to do
719 break;
720
721 case Qualifiers::OCL_Strong:
722 init = EmitARCRetain(lvalue.getType(), init);
723 break;
724
725 case Qualifiers::OCL_Weak:
726 // Initialize and then skip the primitive store.
727 EmitARCInitWeak(lvalue.getAddress(), init);
728 return;
729
730 case Qualifiers::OCL_Autoreleasing:
731 init = EmitARCRetainAutorelease(lvalue.getType(), init);
732 break;
733 }
734
David Chisnall7a7ee302012-01-16 17:27:18 +0000735 EmitStoreOfScalar(init, lvalue, /* isInitialization */ true);
John McCall7acddac2011-06-17 06:42:21 +0000736}
737
Chris Lattner94cd0112010-12-01 02:05:19 +0000738/// canEmitInitWithFewStoresAfterMemset - Decide whether we can emit the
739/// non-zero parts of the specified initializer with equal or fewer than
740/// NumStores scalar stores.
741static bool canEmitInitWithFewStoresAfterMemset(llvm::Constant *Init,
742 unsigned &NumStores) {
Chris Lattner70b02942010-12-02 01:58:41 +0000743 // Zero and Undef never requires any extra stores.
744 if (isa<llvm::ConstantAggregateZero>(Init) ||
745 isa<llvm::ConstantPointerNull>(Init) ||
746 isa<llvm::UndefValue>(Init))
747 return true;
748 if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) ||
749 isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) ||
750 isa<llvm::ConstantExpr>(Init))
751 return Init->isNullValue() || NumStores--;
752
753 // See if we can emit each element.
754 if (isa<llvm::ConstantArray>(Init) || isa<llvm::ConstantStruct>(Init)) {
755 for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) {
756 llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i));
757 if (!canEmitInitWithFewStoresAfterMemset(Elt, NumStores))
758 return false;
759 }
760 return true;
761 }
Chris Lattnerf492cb12012-01-31 04:36:19 +0000762
763 if (llvm::ConstantDataSequential *CDS =
764 dyn_cast<llvm::ConstantDataSequential>(Init)) {
765 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
766 llvm::Constant *Elt = CDS->getElementAsConstant(i);
767 if (!canEmitInitWithFewStoresAfterMemset(Elt, NumStores))
768 return false;
769 }
770 return true;
771 }
Eric Christophere1f54902011-08-23 22:38:00 +0000772
Chris Lattner94cd0112010-12-01 02:05:19 +0000773 // Anything else is hard and scary.
774 return false;
775}
776
777/// emitStoresForInitAfterMemset - For inits that
778/// canEmitInitWithFewStoresAfterMemset returned true for, emit the scalar
779/// stores that would be required.
780static void emitStoresForInitAfterMemset(llvm::Constant *Init, llvm::Value *Loc,
John McCall34695852011-02-22 06:44:22 +0000781 bool isVolatile, CGBuilderTy &Builder) {
Benjamin Kramer06d43682012-08-27 22:07:02 +0000782 assert(!Init->isNullValue() && !isa<llvm::UndefValue>(Init) &&
783 "called emitStoresForInitAfterMemset for zero or undef value.");
Eric Christophere1f54902011-08-23 22:38:00 +0000784
Chris Lattner70b02942010-12-02 01:58:41 +0000785 if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) ||
786 isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) ||
787 isa<llvm::ConstantExpr>(Init)) {
Chris Lattnerf492cb12012-01-31 04:36:19 +0000788 Builder.CreateStore(Init, Loc, isVolatile);
789 return;
790 }
791
792 if (llvm::ConstantDataSequential *CDS =
793 dyn_cast<llvm::ConstantDataSequential>(Init)) {
794 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
795 llvm::Constant *Elt = CDS->getElementAsConstant(i);
Benjamin Kramercfa07e32012-08-27 21:35:58 +0000796
797 // If necessary, get a pointer to the element and emit it.
798 if (!Elt->isNullValue() && !isa<llvm::UndefValue>(Elt))
799 emitStoresForInitAfterMemset(Elt, Builder.CreateConstGEP2_32(Loc, 0, i),
800 isVolatile, Builder);
Chris Lattnerf492cb12012-01-31 04:36:19 +0000801 }
Chris Lattner70b02942010-12-02 01:58:41 +0000802 return;
803 }
Eric Christophere1f54902011-08-23 22:38:00 +0000804
Chris Lattner70b02942010-12-02 01:58:41 +0000805 assert((isa<llvm::ConstantStruct>(Init) || isa<llvm::ConstantArray>(Init)) &&
806 "Unknown value type!");
Eric Christophere1f54902011-08-23 22:38:00 +0000807
Chris Lattner70b02942010-12-02 01:58:41 +0000808 for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) {
809 llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i));
Benjamin Kramercfa07e32012-08-27 21:35:58 +0000810
811 // If necessary, get a pointer to the element and emit it.
812 if (!Elt->isNullValue() && !isa<llvm::UndefValue>(Elt))
813 emitStoresForInitAfterMemset(Elt, Builder.CreateConstGEP2_32(Loc, 0, i),
814 isVolatile, Builder);
Chris Lattner70b02942010-12-02 01:58:41 +0000815 }
Chris Lattner94cd0112010-12-01 02:05:19 +0000816}
817
818
819/// shouldUseMemSetPlusStoresToInitialize - Decide whether we should use memset
820/// plus some stores to initialize a local variable instead of using a memcpy
821/// from a constant global. It is beneficial to use memset if the global is all
822/// zeros, or mostly zeros and large.
823static bool shouldUseMemSetPlusStoresToInitialize(llvm::Constant *Init,
824 uint64_t GlobalSize) {
825 // If a global is all zeros, always use a memset.
826 if (isa<llvm::ConstantAggregateZero>(Init)) return true;
827
Chris Lattner94cd0112010-12-01 02:05:19 +0000828 // If a non-zero global is <= 32 bytes, always use a memcpy. If it is large,
829 // do it if it will require 6 or fewer scalar stores.
830 // TODO: Should budget depends on the size? Avoiding a large global warrants
831 // plopping in more stores.
832 unsigned StoreBudget = 6;
833 uint64_t SizeLimit = 32;
Eric Christophere1f54902011-08-23 22:38:00 +0000834
835 return GlobalSize > SizeLimit &&
Chris Lattner94cd0112010-12-01 02:05:19 +0000836 canEmitInitWithFewStoresAfterMemset(Init, StoreBudget);
837}
838
Nadav Rotem495cfa42013-03-23 06:43:35 +0000839/// Should we use the LLVM lifetime intrinsics for the given local variable?
840static bool shouldUseLifetimeMarkers(CodeGenFunction &CGF, const VarDecl &D,
841 unsigned Size) {
842 // For now, only in optimized builds.
843 if (CGF.CGM.getCodeGenOpts().OptimizationLevel == 0)
844 return false;
845
846 // Limit the size of marked objects to 32 bytes. We don't want to increase
847 // compile time by marking tiny objects.
848 unsigned SizeThreshold = 32;
849
850 return Size > SizeThreshold;
851}
852
Chris Lattner94cd0112010-12-01 02:05:19 +0000853
Nick Lewyckya9de3fa2010-12-30 20:21:55 +0000854/// EmitAutoVarDecl - Emit code and set up an entry in LocalDeclMap for a
Reid Spencer5f016e22007-07-11 17:01:13 +0000855/// variable declaration with auto, register, or no storage class specifier.
Chris Lattner2621fd12008-05-08 05:58:21 +0000856/// These turn into simple stack objects, or GlobalValues depending on target.
John McCall34695852011-02-22 06:44:22 +0000857void CodeGenFunction::EmitAutoVarDecl(const VarDecl &D) {
858 AutoVarEmission emission = EmitAutoVarAlloca(D);
859 EmitAutoVarInit(emission);
860 EmitAutoVarCleanups(emission);
861}
Reid Spencer5f016e22007-07-11 17:01:13 +0000862
John McCall34695852011-02-22 06:44:22 +0000863/// EmitAutoVarAlloca - Emit the alloca and debug information for a
Stephen Hines651f13c2014-04-23 16:59:28 -0700864/// local variable. Does not emit initialization or destruction.
John McCall34695852011-02-22 06:44:22 +0000865CodeGenFunction::AutoVarEmission
866CodeGenFunction::EmitAutoVarAlloca(const VarDecl &D) {
867 QualType Ty = D.getType();
868
869 AutoVarEmission emission(D);
870
871 bool isByRef = D.hasAttr<BlocksAttr>();
872 emission.IsByRef = isByRef;
873
874 CharUnits alignment = getContext().getDeclAlign(&D);
875 emission.Alignment = alignment;
876
John McCallbc8d40d2011-06-24 21:55:10 +0000877 // If the type is variably-modified, emit all the VLA sizes for it.
878 if (Ty->isVariablyModifiedType())
879 EmitVariablyModifiedType(Ty);
880
Reid Spencer5f016e22007-07-11 17:01:13 +0000881 llvm::Value *DeclPtr;
Eli Friedman3c2b3172008-02-15 12:20:59 +0000882 if (Ty->isConstantSizeType()) {
Rafael Espindola6c82fc62013-03-26 18:41:47 +0000883 bool NRVO = getLangOpts().ElideConstructors &&
884 D.isNRVOVariable();
John McCall34695852011-02-22 06:44:22 +0000885
Richard Smithe67ca582013-06-02 00:09:52 +0000886 // If this value is an array or struct with a statically determinable
887 // constant initializer, there are optimizations we can do.
Rafael Espindola6c82fc62013-03-26 18:41:47 +0000888 //
889 // TODO: We should constant-evaluate the initializer of any variable,
890 // as long as it is initialized by a constant expression. Currently,
891 // isConstantInitializer produces wrong answers for structs with
892 // reference or bitfield members, and a few other cases, and checking
893 // for POD-ness protects us from some of these.
Richard Smithe67ca582013-06-02 00:09:52 +0000894 if (D.getInit() && (Ty->isArrayType() || Ty->isRecordType()) &&
895 (D.isConstexpr() ||
896 ((Ty.isPODType(getContext()) ||
897 getContext().getBaseElementType(Ty)->isObjCObjectPointerType()) &&
898 D.getInit()->isConstantInitializer(getContext(), false)))) {
John McCall34695852011-02-22 06:44:22 +0000899
Rafael Espindola6c82fc62013-03-26 18:41:47 +0000900 // If the variable's a const type, and it's neither an NRVO
901 // candidate nor a __block variable and has no mutable members,
902 // emit it as a global instead.
903 if (CGM.getCodeGenOpts().MergeAllConstants && !NRVO && !isByRef &&
904 CGM.isTypeConstant(Ty, true)) {
905 EmitStaticVarDecl(D, llvm::GlobalValue::InternalLinkage);
John McCall34695852011-02-22 06:44:22 +0000906
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700907 emission.Address = nullptr; // signal this condition to later callbacks
Rafael Espindola6c82fc62013-03-26 18:41:47 +0000908 assert(emission.wasEmittedAsGlobal());
909 return emission;
Tanya Lattner59876c22009-11-04 01:18:09 +0000910 }
Eric Christophere1f54902011-08-23 22:38:00 +0000911
Rafael Espindola6c82fc62013-03-26 18:41:47 +0000912 // Otherwise, tell the initialization code that we're in this case.
913 emission.IsConstantAggregate = true;
914 }
Eric Christophere1f54902011-08-23 22:38:00 +0000915
Rafael Espindola6c82fc62013-03-26 18:41:47 +0000916 // A normal fixed sized variable becomes an alloca in the entry block,
917 // unless it's an NRVO variable.
918 llvm::Type *LTy = ConvertTypeForMem(Ty);
Eric Christophere1f54902011-08-23 22:38:00 +0000919
Rafael Espindola6c82fc62013-03-26 18:41:47 +0000920 if (NRVO) {
921 // The named return value optimization: allocate this variable in the
922 // return slot, so that we can elide the copy when returning this
923 // variable (C++0x [class.copy]p34).
924 DeclPtr = ReturnValue;
Eric Christophere1f54902011-08-23 22:38:00 +0000925
Rafael Espindola6c82fc62013-03-26 18:41:47 +0000926 if (const RecordType *RecordTy = Ty->getAs<RecordType>()) {
927 if (!cast<CXXRecordDecl>(RecordTy->getDecl())->hasTrivialDestructor()) {
928 // Create a flag that is used to indicate when the NRVO was applied
929 // to this variable. Set it to zero to indicate that NRVO was not
930 // applied.
931 llvm::Value *Zero = Builder.getFalse();
932 llvm::Value *NRVOFlag = CreateTempAlloca(Zero->getType(), "nrvo");
933 EnsureInsertPoint();
934 Builder.CreateStore(Zero, NRVOFlag);
Eric Christophere1f54902011-08-23 22:38:00 +0000935
Rafael Espindola6c82fc62013-03-26 18:41:47 +0000936 // Record the NRVO flag for this variable.
937 NRVOFlags[&D] = NRVOFlag;
938 emission.NRVOFlag = NRVOFlag;
Nadav Rotem495cfa42013-03-23 06:43:35 +0000939 }
Douglas Gregord86c4772010-05-15 06:46:45 +0000940 }
Chris Lattner2621fd12008-05-08 05:58:21 +0000941 } else {
Rafael Espindola6c82fc62013-03-26 18:41:47 +0000942 if (isByRef)
943 LTy = BuildByRefType(&D);
944
945 llvm::AllocaInst *Alloc = CreateTempAlloca(LTy);
946 Alloc->setName(D.getName());
947
948 CharUnits allocaAlignment = alignment;
949 if (isByRef)
950 allocaAlignment = std::max(allocaAlignment,
John McCall64aa4b32013-04-16 22:48:15 +0000951 getContext().toCharUnitsFromBits(getTarget().getPointerAlign(0)));
Rafael Espindola6c82fc62013-03-26 18:41:47 +0000952 Alloc->setAlignment(allocaAlignment.getQuantity());
953 DeclPtr = Alloc;
954
955 // Emit a lifetime intrinsic if meaningful. There's no point
956 // in doing this if we don't have a valid insertion point (?).
957 uint64_t size = CGM.getDataLayout().getTypeAllocSize(LTy);
958 if (HaveInsertPoint() && shouldUseLifetimeMarkers(*this, D, size)) {
959 llvm::Value *sizeV = llvm::ConstantInt::get(Int64Ty, size);
960
961 emission.SizeForLifetimeMarkers = sizeV;
962 llvm::Value *castAddr = Builder.CreateBitCast(Alloc, Int8PtrTy);
963 Builder.CreateCall2(CGM.getLLVMLifetimeStartFn(), sizeV, castAddr)
964 ->setDoesNotThrow();
965 } else {
966 assert(!emission.useLifetimeMarkers());
967 }
Chris Lattner2621fd12008-05-08 05:58:21 +0000968 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000969 } else {
Daniel Dunbard286f052009-07-19 06:58:07 +0000970 EnsureInsertPoint();
971
Anders Carlsson5ecb1b92009-02-09 20:41:50 +0000972 if (!DidCallStackSave) {
Anders Carlsson5d463152008-12-12 07:38:43 +0000973 // Save the stack.
John McCalld16c2cf2011-02-08 08:22:06 +0000974 llvm::Value *Stack = CreateTempAlloca(Int8PtrTy, "saved_stack");
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Anders Carlsson5d463152008-12-12 07:38:43 +0000976 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave);
977 llvm::Value *V = Builder.CreateCall(F);
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Anders Carlsson5d463152008-12-12 07:38:43 +0000979 Builder.CreateStore(V, Stack);
Anders Carlsson5ecb1b92009-02-09 20:41:50 +0000980
981 DidCallStackSave = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000982
John McCalld8715092010-07-21 06:13:08 +0000983 // Push a cleanup block and restore the stack there.
John McCall3ad32c82011-01-28 08:37:24 +0000984 // FIXME: in general circumstances, this should be an EH cleanup.
Stephen Hines651f13c2014-04-23 16:59:28 -0700985 pushStackRestore(NormalCleanup, Stack);
Anders Carlsson5d463152008-12-12 07:38:43 +0000986 }
Mike Stump1eb44332009-09-09 15:08:12 +0000987
John McCallbc8d40d2011-06-24 21:55:10 +0000988 llvm::Value *elementCount;
989 QualType elementType;
Stephen Hines651f13c2014-04-23 16:59:28 -0700990 std::tie(elementCount, elementType) = getVLASize(Ty);
Anders Carlsson5d463152008-12-12 07:38:43 +0000991
Chris Lattner2acc6e32011-07-18 04:24:23 +0000992 llvm::Type *llvmTy = ConvertTypeForMem(elementType);
Anders Carlsson5d463152008-12-12 07:38:43 +0000993
994 // Allocate memory for the array.
John McCallbc8d40d2011-06-24 21:55:10 +0000995 llvm::AllocaInst *vla = Builder.CreateAlloca(llvmTy, elementCount, "vla");
996 vla->setAlignment(alignment.getQuantity());
Anders Carlsson41f8a132009-09-26 18:16:06 +0000997
John McCallbc8d40d2011-06-24 21:55:10 +0000998 DeclPtr = vla;
Reid Spencer5f016e22007-07-11 17:01:13 +0000999 }
Eli Friedman8f39f5e2008-12-20 23:11:59 +00001000
Reid Spencer5f016e22007-07-11 17:01:13 +00001001 llvm::Value *&DMEntry = LocalDeclMap[&D];
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001002 assert(!DMEntry && "Decl already exists in localdeclmap!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001003 DMEntry = DeclPtr;
John McCall34695852011-02-22 06:44:22 +00001004 emission.Address = DeclPtr;
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001005
1006 // Emit debug info for local var declaration.
Devang Patelc594abd2011-06-03 19:21:47 +00001007 if (HaveInsertPoint())
1008 if (CGDebugInfo *DI = getDebugInfo()) {
Douglas Gregor4cdad312012-10-23 20:05:01 +00001009 if (CGM.getCodeGenOpts().getDebugInfo()
1010 >= CodeGenOptions::LimitedDebugInfo) {
Alexey Samsonovfd00eec2012-05-04 07:39:27 +00001011 DI->setLocation(D.getLocation());
Rafael Espindola6c82fc62013-03-26 18:41:47 +00001012 DI->EmitDeclareOfAutoVariable(&D, DeclPtr, Builder);
Alexey Samsonovfd00eec2012-05-04 07:39:27 +00001013 }
Devang Patelc594abd2011-06-03 19:21:47 +00001014 }
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001015
Julien Lerouge77f68bb2011-09-09 22:41:49 +00001016 if (D.hasAttr<AnnotateAttr>())
1017 EmitVarAnnotations(&D, emission.Address);
1018
John McCall34695852011-02-22 06:44:22 +00001019 return emission;
1020}
1021
1022/// Determines whether the given __block variable is potentially
1023/// captured by the given expression.
1024static bool isCapturedBy(const VarDecl &var, const Expr *e) {
1025 // Skip the most common kinds of expressions that make
1026 // hierarchy-walking expensive.
1027 e = e->IgnoreParenCasts();
1028
1029 if (const BlockExpr *be = dyn_cast<BlockExpr>(e)) {
1030 const BlockDecl *block = be->getBlockDecl();
Stephen Hines651f13c2014-04-23 16:59:28 -07001031 for (const auto &I : block->captures()) {
1032 if (I.getVariable() == &var)
John McCall34695852011-02-22 06:44:22 +00001033 return true;
1034 }
1035
1036 // No need to walk into the subexpressions.
1037 return false;
1038 }
1039
Fariborz Jahanian5033be12011-08-23 16:47:15 +00001040 if (const StmtExpr *SE = dyn_cast<StmtExpr>(e)) {
1041 const CompoundStmt *CS = SE->getSubStmt();
Stephen Hines651f13c2014-04-23 16:59:28 -07001042 for (const auto *BI : CS->body())
1043 if (const auto *E = dyn_cast<Expr>(BI)) {
Fariborz Jahanian5033be12011-08-23 16:47:15 +00001044 if (isCapturedBy(var, E))
1045 return true;
Fariborz Jahanian045c8422011-08-25 00:06:26 +00001046 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001047 else if (const auto *DS = dyn_cast<DeclStmt>(BI)) {
Fariborz Jahanian045c8422011-08-25 00:06:26 +00001048 // special case declarations
Stephen Hines651f13c2014-04-23 16:59:28 -07001049 for (const auto *I : DS->decls()) {
1050 if (const auto *VD = dyn_cast<VarDecl>((I))) {
1051 const Expr *Init = VD->getInit();
Fariborz Jahanian045c8422011-08-25 00:06:26 +00001052 if (Init && isCapturedBy(var, Init))
1053 return true;
1054 }
1055 }
1056 }
1057 else
1058 // FIXME. Make safe assumption assuming arbitrary statements cause capturing.
1059 // Later, provide code to poke into statements for capture analysis.
1060 return true;
Fariborz Jahanian5033be12011-08-23 16:47:15 +00001061 return false;
1062 }
Eric Christophere1f54902011-08-23 22:38:00 +00001063
John McCall34695852011-02-22 06:44:22 +00001064 for (Stmt::const_child_range children = e->children(); children; ++children)
1065 if (isCapturedBy(var, cast<Expr>(*children)))
1066 return true;
1067
1068 return false;
1069}
1070
Douglas Gregorbcc3e662011-07-01 21:08:19 +00001071/// \brief Determine whether the given initializer is trivial in the sense
1072/// that it requires no code to be generated.
Stephen Hines176edba2014-12-01 14:53:08 -08001073bool CodeGenFunction::isTrivialInitializer(const Expr *Init) {
Douglas Gregorbcc3e662011-07-01 21:08:19 +00001074 if (!Init)
1075 return true;
Eric Christophere1f54902011-08-23 22:38:00 +00001076
Douglas Gregorbcc3e662011-07-01 21:08:19 +00001077 if (const CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init))
1078 if (CXXConstructorDecl *Constructor = Construct->getConstructor())
1079 if (Constructor->isTrivial() &&
1080 Constructor->isDefaultConstructor() &&
1081 !Construct->requiresZeroInitialization())
1082 return true;
Eric Christophere1f54902011-08-23 22:38:00 +00001083
Douglas Gregorbcc3e662011-07-01 21:08:19 +00001084 return false;
1085}
John McCall34695852011-02-22 06:44:22 +00001086void CodeGenFunction::EmitAutoVarInit(const AutoVarEmission &emission) {
John McCall57b3b6a2011-02-22 07:16:58 +00001087 assert(emission.Variable && "emission was not valid!");
1088
John McCall34695852011-02-22 06:44:22 +00001089 // If this was emitted as a global constant, we're done.
1090 if (emission.wasEmittedAsGlobal()) return;
1091
John McCall57b3b6a2011-02-22 07:16:58 +00001092 const VarDecl &D = *emission.Variable;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001093 auto DL = ApplyDebugLocation::CreateDefaultArtificial(*this, D.getLocation());
John McCall34695852011-02-22 06:44:22 +00001094 QualType type = D.getType();
1095
Chris Lattner19785962007-07-12 00:39:48 +00001096 // If this local has an initializer, emit it now.
Daniel Dunbard286f052009-07-19 06:58:07 +00001097 const Expr *Init = D.getInit();
1098
1099 // If we are at an unreachable point, we don't need to emit the initializer
1100 // unless it contains a label.
1101 if (!HaveInsertPoint()) {
John McCall34695852011-02-22 06:44:22 +00001102 if (!Init || !ContainsLabel(Init)) return;
1103 EnsureInsertPoint();
Daniel Dunbard286f052009-07-19 06:58:07 +00001104 }
1105
John McCall5af02db2011-03-31 01:59:53 +00001106 // Initialize the structure of a __block variable.
1107 if (emission.IsByRef)
1108 emitByrefStructureInit(emission);
Anders Carlsson69c68b72009-02-07 23:51:38 +00001109
Douglas Gregorbcc3e662011-07-01 21:08:19 +00001110 if (isTrivialInitializer(Init))
1111 return;
Eric Christophere1f54902011-08-23 22:38:00 +00001112
John McCall5af02db2011-03-31 01:59:53 +00001113 CharUnits alignment = emission.Alignment;
1114
John McCall34695852011-02-22 06:44:22 +00001115 // Check whether this is a byref variable that's potentially
1116 // captured and moved by its own initializer. If so, we'll need to
1117 // emit the initializer first, then copy into the variable.
1118 bool capturedByInit = emission.IsByRef && isCapturedBy(D, Init);
1119
1120 llvm::Value *Loc =
1121 capturedByInit ? emission.Address : emission.getObjectAddress(*this);
1122
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001123 llvm::Constant *constant = nullptr;
Richard Smithe67ca582013-06-02 00:09:52 +00001124 if (emission.IsConstantAggregate || D.isConstexpr()) {
Richard Smith51201882011-12-30 21:15:51 +00001125 assert(!capturedByInit && "constant init contains a capturing block?");
Richard Smith2d6a5672012-01-14 04:30:29 +00001126 constant = CGM.EmitConstantInit(D, this);
Richard Smith51201882011-12-30 21:15:51 +00001127 }
1128
1129 if (!constant) {
Eli Friedman6da2c712011-12-03 04:14:32 +00001130 LValue lv = MakeAddrLValue(Loc, type, alignment);
John McCalla07398e2011-06-16 04:16:24 +00001131 lv.setNonGC(true);
1132 return EmitExprAsInit(Init, &D, lv, capturedByInit);
1133 }
John McCall60d33652011-03-08 09:11:50 +00001134
Richard Smithe67ca582013-06-02 00:09:52 +00001135 if (!emission.IsConstantAggregate) {
1136 // For simple scalar/complex initialization, store the value directly.
1137 LValue lv = MakeAddrLValue(Loc, type, alignment);
1138 lv.setNonGC(true);
1139 return EmitStoreThroughLValue(RValue::get(constant), lv, true);
1140 }
1141
John McCall34695852011-02-22 06:44:22 +00001142 // If this is a simple aggregate initialization, we can optimize it
1143 // in various ways.
John McCall60d33652011-03-08 09:11:50 +00001144 bool isVolatile = type.isVolatileQualified();
John McCall34695852011-02-22 06:44:22 +00001145
John McCall60d33652011-03-08 09:11:50 +00001146 llvm::Value *SizeVal =
Eric Christophere1f54902011-08-23 22:38:00 +00001147 llvm::ConstantInt::get(IntPtrTy,
John McCall60d33652011-03-08 09:11:50 +00001148 getContext().getTypeSizeInChars(type).getQuantity());
John McCall34695852011-02-22 06:44:22 +00001149
Chris Lattner2acc6e32011-07-18 04:24:23 +00001150 llvm::Type *BP = Int8PtrTy;
John McCall60d33652011-03-08 09:11:50 +00001151 if (Loc->getType() != BP)
Benjamin Kramer578faa82011-09-27 21:06:10 +00001152 Loc = Builder.CreateBitCast(Loc, BP);
Mon P Wang3ecd7852010-04-04 03:10:52 +00001153
John McCall60d33652011-03-08 09:11:50 +00001154 // If the initializer is all or mostly zeros, codegen with memset then do
1155 // a few stores afterward.
Eric Christophere1f54902011-08-23 22:38:00 +00001156 if (shouldUseMemSetPlusStoresToInitialize(constant,
Micah Villmow25a6a842012-10-08 16:25:52 +00001157 CGM.getDataLayout().getTypeAllocSize(constant->getType()))) {
John McCall60d33652011-03-08 09:11:50 +00001158 Builder.CreateMemSet(Loc, llvm::ConstantInt::get(Int8Ty, 0), SizeVal,
1159 alignment.getQuantity(), isVolatile);
Benjamin Kramer06d43682012-08-27 22:07:02 +00001160 // Zero and undef don't require a stores.
1161 if (!constant->isNullValue() && !isa<llvm::UndefValue>(constant)) {
John McCall60d33652011-03-08 09:11:50 +00001162 Loc = Builder.CreateBitCast(Loc, constant->getType()->getPointerTo());
1163 emitStoresForInitAfterMemset(constant, Loc, isVolatile, Builder);
Fariborz Jahanian20e1c7e2010-03-12 21:40:43 +00001164 }
John McCall60d33652011-03-08 09:11:50 +00001165 } else {
Eric Christophere1f54902011-08-23 22:38:00 +00001166 // Otherwise, create a temporary global with the initializer then
John McCall60d33652011-03-08 09:11:50 +00001167 // memcpy from the global to the alloca.
Stephen Hines176edba2014-12-01 14:53:08 -08001168 std::string Name = getStaticDeclName(CGM, D);
John McCall60d33652011-03-08 09:11:50 +00001169 llvm::GlobalVariable *GV =
1170 new llvm::GlobalVariable(CGM.getModule(), constant->getType(), true,
Eric Christopher736a9c22011-08-24 00:33:55 +00001171 llvm::GlobalValue::PrivateLinkage,
Hans Wennborg5e2d5de2012-06-23 11:51:46 +00001172 constant, Name);
John McCall60d33652011-03-08 09:11:50 +00001173 GV->setAlignment(alignment.getQuantity());
Eli Friedman460980d2011-05-27 22:32:55 +00001174 GV->setUnnamedAddr(true);
Eric Christophere1f54902011-08-23 22:38:00 +00001175
John McCall60d33652011-03-08 09:11:50 +00001176 llvm::Value *SrcPtr = GV;
1177 if (SrcPtr->getType() != BP)
Benjamin Kramer578faa82011-09-27 21:06:10 +00001178 SrcPtr = Builder.CreateBitCast(SrcPtr, BP);
John McCall60d33652011-03-08 09:11:50 +00001179
1180 Builder.CreateMemCpy(Loc, SrcPtr, SizeVal, alignment.getQuantity(),
1181 isVolatile);
1182 }
1183}
1184
1185/// Emit an expression as an initializer for a variable at the given
1186/// location. The expression is not necessarily the normal
1187/// initializer for the variable, and the address is not necessarily
1188/// its normal location.
1189///
1190/// \param init the initializing expression
1191/// \param var the variable to act as if we're initializing
1192/// \param loc the address to initialize; its type is a pointer
1193/// to the LLVM mapping of the variable's type
1194/// \param alignment the alignment of the address
1195/// \param capturedByInit true if the variable is a __block variable
1196/// whose address is potentially changed by the initializer
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001197void CodeGenFunction::EmitExprAsInit(const Expr *init, const ValueDecl *D,
1198 LValue lvalue, bool capturedByInit) {
John McCallf85e1932011-06-15 23:02:42 +00001199 QualType type = D->getType();
John McCall60d33652011-03-08 09:11:50 +00001200
1201 if (type->isReferenceType()) {
Richard Smithd4ec5622013-06-12 23:38:09 +00001202 RValue rvalue = EmitReferenceBindingToExpr(init);
Eric Christophere1f54902011-08-23 22:38:00 +00001203 if (capturedByInit)
John McCalla07398e2011-06-16 04:16:24 +00001204 drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
David Chisnall7a7ee302012-01-16 17:27:18 +00001205 EmitStoreThroughLValue(rvalue, lvalue, true);
John McCall9d232c82013-03-07 21:37:08 +00001206 return;
1207 }
1208 switch (getEvaluationKind(type)) {
1209 case TEK_Scalar:
John McCalla07398e2011-06-16 04:16:24 +00001210 EmitScalarInit(init, D, lvalue, capturedByInit);
John McCall9d232c82013-03-07 21:37:08 +00001211 return;
1212 case TEK_Complex: {
John McCall60d33652011-03-08 09:11:50 +00001213 ComplexPairTy complex = EmitComplexExpr(init);
John McCalla07398e2011-06-16 04:16:24 +00001214 if (capturedByInit)
1215 drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
John McCall9d232c82013-03-07 21:37:08 +00001216 EmitStoreOfComplex(complex, lvalue, /*init*/ true);
1217 return;
1218 }
1219 case TEK_Aggregate:
John McCall9eda3ab2013-03-07 21:37:17 +00001220 if (type->isAtomicType()) {
1221 EmitAtomicInit(const_cast<Expr*>(init), lvalue);
1222 } else {
1223 // TODO: how can we delay here if D is captured by its initializer?
1224 EmitAggExpr(init, AggValueSlot::forLValue(lvalue,
Chad Rosier649b4a12012-03-29 17:37:10 +00001225 AggValueSlot::IsDestructed,
John McCall410ffb22011-08-25 23:04:34 +00001226 AggValueSlot::DoesNotNeedGCBarriers,
Chad Rosier649b4a12012-03-29 17:37:10 +00001227 AggValueSlot::IsNotAliased));
John McCall9eda3ab2013-03-07 21:37:17 +00001228 }
John McCall9d232c82013-03-07 21:37:08 +00001229 return;
Fariborz Jahanian20e1c7e2010-03-12 21:40:43 +00001230 }
John McCall9d232c82013-03-07 21:37:08 +00001231 llvm_unreachable("bad evaluation kind");
John McCall34695852011-02-22 06:44:22 +00001232}
John McCallf1549f62010-07-06 01:34:17 +00001233
John McCallbdc4d802011-07-09 01:37:26 +00001234/// Enter a destroy cleanup for the given local variable.
1235void CodeGenFunction::emitAutoVarTypeCleanup(
1236 const CodeGenFunction::AutoVarEmission &emission,
1237 QualType::DestructionKind dtorKind) {
1238 assert(dtorKind != QualType::DK_none);
1239
1240 // Note that for __block variables, we want to destroy the
1241 // original stack object, not the possibly forwarded object.
1242 llvm::Value *addr = emission.getObjectAddress(*this);
1243
1244 const VarDecl *var = emission.Variable;
1245 QualType type = var->getType();
1246
1247 CleanupKind cleanupKind = NormalAndEHCleanup;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001248 CodeGenFunction::Destroyer *destroyer = nullptr;
John McCallbdc4d802011-07-09 01:37:26 +00001249
1250 switch (dtorKind) {
1251 case QualType::DK_none:
1252 llvm_unreachable("no cleanup for trivially-destructible variable");
1253
1254 case QualType::DK_cxx_destructor:
1255 // If there's an NRVO flag on the emission, we need a different
1256 // cleanup.
1257 if (emission.NRVOFlag) {
1258 assert(!type->isArrayType());
1259 CXXDestructorDecl *dtor = type->getAsCXXRecordDecl()->getDestructor();
1260 EHStack.pushCleanup<DestroyNRVOVariable>(cleanupKind, addr, dtor,
1261 emission.NRVOFlag);
1262 return;
1263 }
1264 break;
1265
1266 case QualType::DK_objc_strong_lifetime:
1267 // Suppress cleanups for pseudo-strong variables.
1268 if (var->isARCPseudoStrong()) return;
Eric Christophere1f54902011-08-23 22:38:00 +00001269
John McCallbdc4d802011-07-09 01:37:26 +00001270 // Otherwise, consider whether to use an EH cleanup or not.
1271 cleanupKind = getARCCleanupKind();
1272
1273 // Use the imprecise destroyer by default.
1274 if (!var->hasAttr<ObjCPreciseLifetimeAttr>())
1275 destroyer = CodeGenFunction::destroyARCStrongImprecise;
1276 break;
1277
1278 case QualType::DK_objc_weak_lifetime:
1279 break;
1280 }
1281
1282 // If we haven't chosen a more specific destroyer, use the default.
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001283 if (!destroyer) destroyer = getDestroyer(dtorKind);
John McCall2673c682011-07-11 08:38:19 +00001284
Sylvestre Ledruf3477c12012-09-27 10:16:10 +00001285 // Use an EH cleanup in array destructors iff the destructor itself
John McCall2673c682011-07-11 08:38:19 +00001286 // is being pushed as an EH cleanup.
1287 bool useEHCleanup = (cleanupKind & EHCleanup);
1288 EHStack.pushCleanup<DestroyObject>(cleanupKind, addr, type, destroyer,
1289 useEHCleanup);
John McCallbdc4d802011-07-09 01:37:26 +00001290}
1291
John McCall34695852011-02-22 06:44:22 +00001292void CodeGenFunction::EmitAutoVarCleanups(const AutoVarEmission &emission) {
John McCall57b3b6a2011-02-22 07:16:58 +00001293 assert(emission.Variable && "emission was not valid!");
1294
John McCall34695852011-02-22 06:44:22 +00001295 // If this was emitted as a global constant, we're done.
1296 if (emission.wasEmittedAsGlobal()) return;
1297
John McCall38baeab2012-04-13 18:44:05 +00001298 // If we don't have an insertion point, we're done. Sema prevents
1299 // us from jumping into any of these scopes anyway.
1300 if (!HaveInsertPoint()) return;
1301
John McCall57b3b6a2011-02-22 07:16:58 +00001302 const VarDecl &D = *emission.Variable;
John McCall34695852011-02-22 06:44:22 +00001303
Nadav Rotem495cfa42013-03-23 06:43:35 +00001304 // Make sure we call @llvm.lifetime.end. This needs to happen
1305 // *last*, so the cleanup needs to be pushed *first*.
1306 if (emission.useLifetimeMarkers()) {
1307 EHStack.pushCleanup<CallLifetimeEnd>(NormalCleanup,
1308 emission.getAllocatedAddress(),
1309 emission.getSizeForLifetimeMarkers());
1310 }
1311
John McCallbdc4d802011-07-09 01:37:26 +00001312 // Check the type for a cleanup.
1313 if (QualType::DestructionKind dtorKind = D.getType().isDestructedType())
1314 emitAutoVarTypeCleanup(emission, dtorKind);
John McCallf85e1932011-06-15 23:02:42 +00001315
John McCall0c24c802011-06-24 23:21:27 +00001316 // In GC mode, honor objc_precise_lifetime.
David Blaikie4e4d0842012-03-11 07:00:24 +00001317 if (getLangOpts().getGC() != LangOptions::NonGC &&
John McCall0c24c802011-06-24 23:21:27 +00001318 D.hasAttr<ObjCPreciseLifetimeAttr>()) {
1319 EHStack.pushCleanup<ExtendGCLifetime>(NormalCleanup, &D);
1320 }
1321
John McCall34695852011-02-22 06:44:22 +00001322 // Handle the cleanup attribute.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001323 if (const CleanupAttr *CA = D.getAttr<CleanupAttr>()) {
Anders Carlsson69c68b72009-02-07 23:51:38 +00001324 const FunctionDecl *FD = CA->getFunctionDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001325
John McCall34695852011-02-22 06:44:22 +00001326 llvm::Constant *F = CGM.GetAddrOfFunction(FD);
Anders Carlsson69c68b72009-02-07 23:51:38 +00001327 assert(F && "Could not find function!");
Mike Stump1eb44332009-09-09 15:08:12 +00001328
John McCallde5d3c72012-02-17 03:33:10 +00001329 const CGFunctionInfo &Info = CGM.getTypes().arrangeFunctionDeclaration(FD);
John McCall34695852011-02-22 06:44:22 +00001330 EHStack.pushCleanup<CallCleanupFunction>(NormalAndEHCleanup, F, &Info, &D);
Anders Carlsson69c68b72009-02-07 23:51:38 +00001331 }
Mike Stump797b6322009-03-05 01:23:13 +00001332
John McCall34695852011-02-22 06:44:22 +00001333 // If this is a block variable, call _Block_object_destroy
1334 // (on the unforwarded address).
John McCall5af02db2011-03-31 01:59:53 +00001335 if (emission.IsByRef)
1336 enterByrefCleanup(emission);
Reid Spencer5f016e22007-07-11 17:01:13 +00001337}
1338
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001339CodeGenFunction::Destroyer *
John McCallbdc4d802011-07-09 01:37:26 +00001340CodeGenFunction::getDestroyer(QualType::DestructionKind kind) {
1341 switch (kind) {
1342 case QualType::DK_none: llvm_unreachable("no destroyer for trivial dtor");
John McCall0850e8d2011-07-09 09:09:00 +00001343 case QualType::DK_cxx_destructor:
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001344 return destroyCXXObject;
John McCall0850e8d2011-07-09 09:09:00 +00001345 case QualType::DK_objc_strong_lifetime:
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001346 return destroyARCStrongPrecise;
John McCall0850e8d2011-07-09 09:09:00 +00001347 case QualType::DK_objc_weak_lifetime:
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001348 return destroyARCWeak;
John McCallbdc4d802011-07-09 01:37:26 +00001349 }
Matt Beaumont-Gayba4be252012-01-27 00:46:27 +00001350 llvm_unreachable("Unknown DestructionKind");
John McCallbdc4d802011-07-09 01:37:26 +00001351}
1352
John McCall074cae02013-02-01 05:11:40 +00001353/// pushEHDestroy - Push the standard destructor for the given type as
1354/// an EH-only cleanup.
1355void CodeGenFunction::pushEHDestroy(QualType::DestructionKind dtorKind,
1356 llvm::Value *addr, QualType type) {
1357 assert(dtorKind && "cannot push destructor for trivial type");
1358 assert(needsEHCleanup(dtorKind));
1359
1360 pushDestroy(EHCleanup, addr, type, getDestroyer(dtorKind), true);
1361}
1362
1363/// pushDestroy - Push the standard destructor for the given type as
1364/// at least a normal cleanup.
John McCall9928c482011-07-12 16:41:08 +00001365void CodeGenFunction::pushDestroy(QualType::DestructionKind dtorKind,
1366 llvm::Value *addr, QualType type) {
1367 assert(dtorKind && "cannot push destructor for trivial type");
1368
1369 CleanupKind cleanupKind = getCleanupKind(dtorKind);
1370 pushDestroy(cleanupKind, addr, type, getDestroyer(dtorKind),
1371 cleanupKind & EHCleanup);
1372}
1373
John McCallbdc4d802011-07-09 01:37:26 +00001374void CodeGenFunction::pushDestroy(CleanupKind cleanupKind, llvm::Value *addr,
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001375 QualType type, Destroyer *destroyer,
John McCall2673c682011-07-11 08:38:19 +00001376 bool useEHCleanupForArray) {
John McCall9928c482011-07-12 16:41:08 +00001377 pushFullExprCleanup<DestroyObject>(cleanupKind, addr, type,
1378 destroyer, useEHCleanupForArray);
John McCallbdc4d802011-07-09 01:37:26 +00001379}
1380
Stephen Hines651f13c2014-04-23 16:59:28 -07001381void CodeGenFunction::pushStackRestore(CleanupKind Kind, llvm::Value *SPMem) {
1382 EHStack.pushCleanup<CallStackRestore>(Kind, SPMem);
1383}
1384
Richard Smith8a07cd32013-06-12 20:42:33 +00001385void CodeGenFunction::pushLifetimeExtendedDestroy(
1386 CleanupKind cleanupKind, llvm::Value *addr, QualType type,
1387 Destroyer *destroyer, bool useEHCleanupForArray) {
1388 assert(!isInConditionalBranch() &&
1389 "performing lifetime extension from within conditional");
1390
1391 // Push an EH-only cleanup for the object now.
1392 // FIXME: When popping normal cleanups, we need to keep this EH cleanup
1393 // around in case a temporary's destructor throws an exception.
1394 if (cleanupKind & EHCleanup)
1395 EHStack.pushCleanup<DestroyObject>(
1396 static_cast<CleanupKind>(cleanupKind & ~NormalCleanup), addr, type,
1397 destroyer, useEHCleanupForArray);
1398
1399 // Remember that we need to push a full cleanup for the object at the
1400 // end of the full-expression.
1401 pushCleanupAfterFullExpr<DestroyObject>(
1402 cleanupKind, addr, type, destroyer, useEHCleanupForArray);
1403}
1404
John McCall2673c682011-07-11 08:38:19 +00001405/// emitDestroy - Immediately perform the destruction of the given
1406/// object.
1407///
1408/// \param addr - the address of the object; a type*
1409/// \param type - the type of the object; if an array type, all
1410/// objects are destroyed in reverse order
1411/// \param destroyer - the function to call to destroy individual
1412/// elements
1413/// \param useEHCleanupForArray - whether an EH cleanup should be
1414/// used when destroying array elements, in case one of the
1415/// destructions throws an exception
John McCallbdc4d802011-07-09 01:37:26 +00001416void CodeGenFunction::emitDestroy(llvm::Value *addr, QualType type,
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001417 Destroyer *destroyer,
John McCall2673c682011-07-11 08:38:19 +00001418 bool useEHCleanupForArray) {
John McCallbdc4d802011-07-09 01:37:26 +00001419 const ArrayType *arrayType = getContext().getAsArrayType(type);
1420 if (!arrayType)
1421 return destroyer(*this, addr, type);
1422
1423 llvm::Value *begin = addr;
1424 llvm::Value *length = emitArrayLength(arrayType, type, begin);
John McCallfbf780a2011-07-13 08:09:46 +00001425
1426 // Normally we have to check whether the array is zero-length.
1427 bool checkZeroLength = true;
1428
1429 // But if the array length is constant, we can suppress that.
1430 if (llvm::ConstantInt *constLength = dyn_cast<llvm::ConstantInt>(length)) {
1431 // ...and if it's constant zero, we can just skip the entire thing.
1432 if (constLength->isZero()) return;
1433 checkZeroLength = false;
1434 }
1435
John McCallbdc4d802011-07-09 01:37:26 +00001436 llvm::Value *end = Builder.CreateInBoundsGEP(begin, length);
John McCallfbf780a2011-07-13 08:09:46 +00001437 emitArrayDestroy(begin, end, type, destroyer,
1438 checkZeroLength, useEHCleanupForArray);
John McCallbdc4d802011-07-09 01:37:26 +00001439}
1440
John McCall2673c682011-07-11 08:38:19 +00001441/// emitArrayDestroy - Destroys all the elements of the given array,
1442/// beginning from last to first. The array cannot be zero-length.
1443///
1444/// \param begin - a type* denoting the first element of the array
1445/// \param end - a type* denoting one past the end of the array
1446/// \param type - the element type of the array
1447/// \param destroyer - the function to call to destroy elements
1448/// \param useEHCleanup - whether to push an EH cleanup to destroy
1449/// the remaining elements in case the destruction of a single
1450/// element throws
John McCallbdc4d802011-07-09 01:37:26 +00001451void CodeGenFunction::emitArrayDestroy(llvm::Value *begin,
1452 llvm::Value *end,
1453 QualType type,
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001454 Destroyer *destroyer,
John McCallfbf780a2011-07-13 08:09:46 +00001455 bool checkZeroLength,
John McCall2673c682011-07-11 08:38:19 +00001456 bool useEHCleanup) {
John McCallbdc4d802011-07-09 01:37:26 +00001457 assert(!type->isArrayType());
1458
1459 // The basic structure here is a do-while loop, because we don't
1460 // need to check for the zero-element case.
1461 llvm::BasicBlock *bodyBB = createBasicBlock("arraydestroy.body");
1462 llvm::BasicBlock *doneBB = createBasicBlock("arraydestroy.done");
1463
John McCallfbf780a2011-07-13 08:09:46 +00001464 if (checkZeroLength) {
1465 llvm::Value *isEmpty = Builder.CreateICmpEQ(begin, end,
1466 "arraydestroy.isempty");
1467 Builder.CreateCondBr(isEmpty, doneBB, bodyBB);
1468 }
1469
John McCallbdc4d802011-07-09 01:37:26 +00001470 // Enter the loop body, making that address the current address.
1471 llvm::BasicBlock *entryBB = Builder.GetInsertBlock();
1472 EmitBlock(bodyBB);
1473 llvm::PHINode *elementPast =
1474 Builder.CreatePHI(begin->getType(), 2, "arraydestroy.elementPast");
1475 elementPast->addIncoming(end, entryBB);
1476
1477 // Shift the address back by one element.
1478 llvm::Value *negativeOne = llvm::ConstantInt::get(SizeTy, -1, true);
1479 llvm::Value *element = Builder.CreateInBoundsGEP(elementPast, negativeOne,
1480 "arraydestroy.element");
1481
John McCall2673c682011-07-11 08:38:19 +00001482 if (useEHCleanup)
1483 pushRegularPartialArrayCleanup(begin, element, type, destroyer);
1484
John McCallbdc4d802011-07-09 01:37:26 +00001485 // Perform the actual destruction there.
1486 destroyer(*this, element, type);
1487
John McCall2673c682011-07-11 08:38:19 +00001488 if (useEHCleanup)
1489 PopCleanupBlock();
1490
John McCallbdc4d802011-07-09 01:37:26 +00001491 // Check whether we've reached the end.
1492 llvm::Value *done = Builder.CreateICmpEQ(element, begin, "arraydestroy.done");
1493 Builder.CreateCondBr(done, doneBB, bodyBB);
1494 elementPast->addIncoming(element, Builder.GetInsertBlock());
1495
1496 // Done.
1497 EmitBlock(doneBB);
1498}
1499
John McCall2673c682011-07-11 08:38:19 +00001500/// Perform partial array destruction as if in an EH cleanup. Unlike
1501/// emitArrayDestroy, the element type here may still be an array type.
John McCall2673c682011-07-11 08:38:19 +00001502static void emitPartialArrayDestroy(CodeGenFunction &CGF,
1503 llvm::Value *begin, llvm::Value *end,
1504 QualType type,
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001505 CodeGenFunction::Destroyer *destroyer) {
John McCall2673c682011-07-11 08:38:19 +00001506 // If the element type is itself an array, drill down.
John McCallfbf780a2011-07-13 08:09:46 +00001507 unsigned arrayDepth = 0;
John McCall2673c682011-07-11 08:38:19 +00001508 while (const ArrayType *arrayType = CGF.getContext().getAsArrayType(type)) {
1509 // VLAs don't require a GEP index to walk into.
1510 if (!isa<VariableArrayType>(arrayType))
John McCallfbf780a2011-07-13 08:09:46 +00001511 arrayDepth++;
John McCall2673c682011-07-11 08:38:19 +00001512 type = arrayType->getElementType();
1513 }
John McCallfbf780a2011-07-13 08:09:46 +00001514
1515 if (arrayDepth) {
1516 llvm::Value *zero = llvm::ConstantInt::get(CGF.SizeTy, arrayDepth+1);
1517
Chris Lattner5f9e2722011-07-23 10:55:15 +00001518 SmallVector<llvm::Value*,4> gepIndices(arrayDepth, zero);
Jay Foad0f6ac7c2011-07-22 08:16:57 +00001519 begin = CGF.Builder.CreateInBoundsGEP(begin, gepIndices, "pad.arraybegin");
1520 end = CGF.Builder.CreateInBoundsGEP(end, gepIndices, "pad.arrayend");
John McCall2673c682011-07-11 08:38:19 +00001521 }
1522
John McCallfbf780a2011-07-13 08:09:46 +00001523 // Destroy the array. We don't ever need an EH cleanup because we
1524 // assume that we're in an EH cleanup ourselves, so a throwing
1525 // destructor causes an immediate terminate.
1526 CGF.emitArrayDestroy(begin, end, type, destroyer,
1527 /*checkZeroLength*/ true, /*useEHCleanup*/ false);
John McCall2673c682011-07-11 08:38:19 +00001528}
1529
John McCallbdc4d802011-07-09 01:37:26 +00001530namespace {
John McCall2673c682011-07-11 08:38:19 +00001531 /// RegularPartialArrayDestroy - a cleanup which performs a partial
1532 /// array destroy where the end pointer is regularly determined and
1533 /// does not need to be loaded from a local.
1534 class RegularPartialArrayDestroy : public EHScopeStack::Cleanup {
1535 llvm::Value *ArrayBegin;
1536 llvm::Value *ArrayEnd;
1537 QualType ElementType;
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001538 CodeGenFunction::Destroyer *Destroyer;
John McCall2673c682011-07-11 08:38:19 +00001539 public:
1540 RegularPartialArrayDestroy(llvm::Value *arrayBegin, llvm::Value *arrayEnd,
1541 QualType elementType,
1542 CodeGenFunction::Destroyer *destroyer)
1543 : ArrayBegin(arrayBegin), ArrayEnd(arrayEnd),
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001544 ElementType(elementType), Destroyer(destroyer) {}
John McCall2673c682011-07-11 08:38:19 +00001545
Stephen Hines651f13c2014-04-23 16:59:28 -07001546 void Emit(CodeGenFunction &CGF, Flags flags) override {
John McCall2673c682011-07-11 08:38:19 +00001547 emitPartialArrayDestroy(CGF, ArrayBegin, ArrayEnd,
1548 ElementType, Destroyer);
1549 }
1550 };
1551
1552 /// IrregularPartialArrayDestroy - a cleanup which performs a
1553 /// partial array destroy where the end pointer is irregularly
1554 /// determined and must be loaded from a local.
1555 class IrregularPartialArrayDestroy : public EHScopeStack::Cleanup {
John McCallbdc4d802011-07-09 01:37:26 +00001556 llvm::Value *ArrayBegin;
1557 llvm::Value *ArrayEndPointer;
1558 QualType ElementType;
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001559 CodeGenFunction::Destroyer *Destroyer;
John McCallbdc4d802011-07-09 01:37:26 +00001560 public:
John McCall2673c682011-07-11 08:38:19 +00001561 IrregularPartialArrayDestroy(llvm::Value *arrayBegin,
1562 llvm::Value *arrayEndPointer,
1563 QualType elementType,
1564 CodeGenFunction::Destroyer *destroyer)
John McCallbdc4d802011-07-09 01:37:26 +00001565 : ArrayBegin(arrayBegin), ArrayEndPointer(arrayEndPointer),
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001566 ElementType(elementType), Destroyer(destroyer) {}
John McCallbdc4d802011-07-09 01:37:26 +00001567
Stephen Hines651f13c2014-04-23 16:59:28 -07001568 void Emit(CodeGenFunction &CGF, Flags flags) override {
John McCallbdc4d802011-07-09 01:37:26 +00001569 llvm::Value *arrayEnd = CGF.Builder.CreateLoad(ArrayEndPointer);
John McCall2673c682011-07-11 08:38:19 +00001570 emitPartialArrayDestroy(CGF, ArrayBegin, arrayEnd,
1571 ElementType, Destroyer);
John McCallbdc4d802011-07-09 01:37:26 +00001572 }
1573 };
1574}
1575
John McCall2673c682011-07-11 08:38:19 +00001576/// pushIrregularPartialArrayCleanup - Push an EH cleanup to destroy
John McCallbdc4d802011-07-09 01:37:26 +00001577/// already-constructed elements of the given array. The cleanup
John McCall2673c682011-07-11 08:38:19 +00001578/// may be popped with DeactivateCleanupBlock or PopCleanupBlock.
Eric Christophere1f54902011-08-23 22:38:00 +00001579///
John McCallbdc4d802011-07-09 01:37:26 +00001580/// \param elementType - the immediate element type of the array;
1581/// possibly still an array type
John McCall9928c482011-07-12 16:41:08 +00001582void CodeGenFunction::pushIrregularPartialArrayCleanup(llvm::Value *arrayBegin,
John McCall2673c682011-07-11 08:38:19 +00001583 llvm::Value *arrayEndPointer,
1584 QualType elementType,
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001585 Destroyer *destroyer) {
John McCall9928c482011-07-12 16:41:08 +00001586 pushFullExprCleanup<IrregularPartialArrayDestroy>(EHCleanup,
1587 arrayBegin, arrayEndPointer,
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001588 elementType, destroyer);
John McCall2673c682011-07-11 08:38:19 +00001589}
1590
1591/// pushRegularPartialArrayCleanup - Push an EH cleanup to destroy
1592/// already-constructed elements of the given array. The cleanup
1593/// may be popped with DeactivateCleanupBlock or PopCleanupBlock.
Eric Christophere1f54902011-08-23 22:38:00 +00001594///
John McCall2673c682011-07-11 08:38:19 +00001595/// \param elementType - the immediate element type of the array;
1596/// possibly still an array type
John McCall2673c682011-07-11 08:38:19 +00001597void CodeGenFunction::pushRegularPartialArrayCleanup(llvm::Value *arrayBegin,
1598 llvm::Value *arrayEnd,
1599 QualType elementType,
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001600 Destroyer *destroyer) {
John McCall9928c482011-07-12 16:41:08 +00001601 pushFullExprCleanup<RegularPartialArrayDestroy>(EHCleanup,
John McCall2673c682011-07-11 08:38:19 +00001602 arrayBegin, arrayEnd,
Peter Collingbourne516bbd42012-01-26 03:33:36 +00001603 elementType, destroyer);
John McCallbdc4d802011-07-09 01:37:26 +00001604}
1605
Nadav Rotem495cfa42013-03-23 06:43:35 +00001606/// Lazily declare the @llvm.lifetime.start intrinsic.
1607llvm::Constant *CodeGenModule::getLLVMLifetimeStartFn() {
1608 if (LifetimeStartFn) return LifetimeStartFn;
1609 LifetimeStartFn = llvm::Intrinsic::getDeclaration(&getModule(),
1610 llvm::Intrinsic::lifetime_start);
1611 return LifetimeStartFn;
1612}
1613
1614/// Lazily declare the @llvm.lifetime.end intrinsic.
1615llvm::Constant *CodeGenModule::getLLVMLifetimeEndFn() {
1616 if (LifetimeEndFn) return LifetimeEndFn;
1617 LifetimeEndFn = llvm::Intrinsic::getDeclaration(&getModule(),
1618 llvm::Intrinsic::lifetime_end);
1619 return LifetimeEndFn;
1620}
1621
John McCallf85e1932011-06-15 23:02:42 +00001622namespace {
1623 /// A cleanup to perform a release of an object at the end of a
1624 /// function. This is used to balance out the incoming +1 of a
1625 /// ns_consumed argument when we can't reasonably do that just by
1626 /// not doing the initial retain for a __block argument.
1627 struct ConsumeARCParameter : EHScopeStack::Cleanup {
John McCall5b07e802013-03-13 03:10:54 +00001628 ConsumeARCParameter(llvm::Value *param,
1629 ARCPreciseLifetime_t precise)
1630 : Param(param), Precise(precise) {}
John McCallf85e1932011-06-15 23:02:42 +00001631
1632 llvm::Value *Param;
John McCall5b07e802013-03-13 03:10:54 +00001633 ARCPreciseLifetime_t Precise;
John McCallf85e1932011-06-15 23:02:42 +00001634
Stephen Hines651f13c2014-04-23 16:59:28 -07001635 void Emit(CodeGenFunction &CGF, Flags flags) override {
John McCall5b07e802013-03-13 03:10:54 +00001636 CGF.EmitARCRelease(Param, Precise);
John McCallf85e1932011-06-15 23:02:42 +00001637 }
1638 };
1639}
1640
Mike Stump1eb44332009-09-09 15:08:12 +00001641/// Emit an alloca (or GlobalValue depending on target)
Chris Lattner2621fd12008-05-08 05:58:21 +00001642/// for the specified parameter and set up LocalDeclMap.
Devang Patel093ac462011-03-03 20:13:15 +00001643void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg,
Stephen Hines651f13c2014-04-23 16:59:28 -07001644 bool ArgIsPointer, unsigned ArgNo) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001645 // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl?
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001646 assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) &&
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001647 "Invalid argument to EmitParmDecl");
John McCall8178df32011-02-22 22:38:33 +00001648
1649 Arg->setName(D.getName());
1650
Adrian Prantl836e7c92013-03-14 17:53:33 +00001651 QualType Ty = D.getType();
1652
John McCall8178df32011-02-22 22:38:33 +00001653 // Use better IR generation for certain implicit parameters.
1654 if (isa<ImplicitParamDecl>(D)) {
1655 // The only implicit argument a block has is its literal.
1656 if (BlockInfo) {
1657 LocalDeclMap[&D] = Arg;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001658 llvm::Value *LocalAddr = nullptr;
Adrian Prantl836e7c92013-03-14 17:53:33 +00001659 if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
Adrian Prantl9b97adf2013-03-29 19:20:35 +00001660 // Allocate a stack slot to let the debug info survive the RA.
Adrian Prantl836e7c92013-03-14 17:53:33 +00001661 llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertTypeForMem(Ty),
1662 D.getName() + ".addr");
1663 Alloc->setAlignment(getContext().getDeclAlign(&D).getQuantity());
1664 LValue lv = MakeAddrLValue(Alloc, Ty, getContext().getDeclAlign(&D));
1665 EmitStoreOfScalar(Arg, lv, /* isInitialization */ true);
1666 LocalAddr = Builder.CreateLoad(Alloc);
1667 }
John McCall8178df32011-02-22 22:38:33 +00001668
1669 if (CGDebugInfo *DI = getDebugInfo()) {
Douglas Gregor4cdad312012-10-23 20:05:01 +00001670 if (CGM.getCodeGenOpts().getDebugInfo()
1671 >= CodeGenOptions::LimitedDebugInfo) {
Alexey Samsonovfd00eec2012-05-04 07:39:27 +00001672 DI->setLocation(D.getLocation());
Stephen Hines176edba2014-12-01 14:53:08 -08001673 DI->EmitDeclareOfBlockLiteralArgVariable(*BlockInfo, Arg, ArgNo,
1674 LocalAddr, Builder);
Alexey Samsonovfd00eec2012-05-04 07:39:27 +00001675 }
John McCall8178df32011-02-22 22:38:33 +00001676 }
1677
1678 return;
1679 }
1680 }
1681
Reid Spencer5f016e22007-07-11 17:01:13 +00001682 llvm::Value *DeclPtr;
Stephen Hines651f13c2014-04-23 16:59:28 -07001683 bool DoStore = false;
1684 bool IsScalar = hasScalarEvaluationKind(Ty);
1685 CharUnits Align = getContext().getDeclAlign(&D);
1686 // If we already have a pointer to the argument, reuse the input pointer.
1687 if (ArgIsPointer) {
1688 // If we have a prettier pointer type at this point, bitcast to that.
1689 unsigned AS = cast<llvm::PointerType>(Arg->getType())->getAddressSpace();
1690 llvm::Type *IRTy = ConvertTypeForMem(Ty)->getPointerTo(AS);
1691 DeclPtr = Arg->getType() == IRTy ? Arg : Builder.CreateBitCast(Arg, IRTy,
1692 D.getName());
Reid Kleckner9b601952013-06-21 12:45:15 +00001693 // Push a destructor cleanup for this parameter if the ABI requires it.
Stephen Hines176edba2014-12-01 14:53:08 -08001694 // Don't push a cleanup in a thunk for a method that will also emit a
1695 // cleanup.
1696 if (!IsScalar && !CurFuncIsThunk &&
Stephen Hines651f13c2014-04-23 16:59:28 -07001697 getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()) {
1698 const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
1699 if (RD && RD->hasNonTrivialDestructor())
1700 pushDestroy(QualType::DK_cxx_destructor, DeclPtr, Ty);
Reid Kleckner9b601952013-06-21 12:45:15 +00001701 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001702 } else {
Daniel Dunbare86bcf02010-02-08 22:53:07 +00001703 // Otherwise, create a temporary to hold the value.
Eli Friedmanddfb8d12011-11-03 20:31:28 +00001704 llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertTypeForMem(Ty),
1705 D.getName() + ".addr");
Fariborz Jahanian41a6a3e2013-02-21 00:40:10 +00001706 Alloc->setAlignment(Align.getQuantity());
Eli Friedmanddfb8d12011-11-03 20:31:28 +00001707 DeclPtr = Alloc;
Stephen Hines651f13c2014-04-23 16:59:28 -07001708 DoStore = true;
1709 }
Mike Stump1eb44332009-09-09 15:08:12 +00001710
Stephen Hines651f13c2014-04-23 16:59:28 -07001711 LValue lv = MakeAddrLValue(DeclPtr, Ty, Align);
1712 if (IsScalar) {
John McCallf85e1932011-06-15 23:02:42 +00001713 Qualifiers qs = Ty.getQualifiers();
John McCallf85e1932011-06-15 23:02:42 +00001714 if (Qualifiers::ObjCLifetime lt = qs.getObjCLifetime()) {
1715 // We honor __attribute__((ns_consumed)) for types with lifetime.
1716 // For __strong, it's handled by just skipping the initial retain;
1717 // otherwise we have to balance out the initial +1 with an extra
1718 // cleanup to do the release at the end of the function.
1719 bool isConsumed = D.hasAttr<NSConsumedAttr>();
1720
1721 // 'self' is always formally __strong, but if this is not an
1722 // init method then we don't want to retain it.
John McCall7acddac2011-06-17 06:42:21 +00001723 if (D.isARCPseudoStrong()) {
John McCallf85e1932011-06-15 23:02:42 +00001724 const ObjCMethodDecl *method = cast<ObjCMethodDecl>(CurCodeDecl);
1725 assert(&D == method->getSelfDecl());
John McCall7acddac2011-06-17 06:42:21 +00001726 assert(lt == Qualifiers::OCL_Strong);
1727 assert(qs.hasConst());
John McCallf85e1932011-06-15 23:02:42 +00001728 assert(method->getMethodFamily() != OMF_init);
John McCall175d6592011-06-15 23:40:09 +00001729 (void) method;
John McCallf85e1932011-06-15 23:02:42 +00001730 lt = Qualifiers::OCL_ExplicitNone;
1731 }
1732
1733 if (lt == Qualifiers::OCL_Strong) {
Fariborz Jahanian41a6a3e2013-02-21 00:40:10 +00001734 if (!isConsumed) {
1735 if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
1736 // use objc_storeStrong(&dest, value) for retaining the
1737 // object. But first, store a null into 'dest' because
1738 // objc_storeStrong attempts to release its old value.
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00001739 llvm::Value *Null = CGM.EmitNullConstant(D.getType());
Fariborz Jahanian41a6a3e2013-02-21 00:40:10 +00001740 EmitStoreOfScalar(Null, lv, /* isInitialization */ true);
1741 EmitARCStoreStrongCall(lv.getAddress(), Arg, true);
Stephen Hines651f13c2014-04-23 16:59:28 -07001742 DoStore = false;
Fariborz Jahanian41a6a3e2013-02-21 00:40:10 +00001743 }
1744 else
John McCallf85e1932011-06-15 23:02:42 +00001745 // Don't use objc_retainBlock for block pointers, because we
1746 // don't want to Block_copy something just because we got it
1747 // as a parameter.
Fariborz Jahanian41a6a3e2013-02-21 00:40:10 +00001748 Arg = EmitARCRetainNonBlock(Arg);
1749 }
John McCallf85e1932011-06-15 23:02:42 +00001750 } else {
1751 // Push the cleanup for a consumed parameter.
John McCall5b07e802013-03-13 03:10:54 +00001752 if (isConsumed) {
1753 ARCPreciseLifetime_t precise = (D.hasAttr<ObjCPreciseLifetimeAttr>()
1754 ? ARCPreciseLifetime : ARCImpreciseLifetime);
1755 EHStack.pushCleanup<ConsumeARCParameter>(getARCCleanupKind(), Arg,
1756 precise);
1757 }
John McCallf85e1932011-06-15 23:02:42 +00001758
1759 if (lt == Qualifiers::OCL_Weak) {
1760 EmitARCInitWeak(DeclPtr, Arg);
Stephen Hines651f13c2014-04-23 16:59:28 -07001761 DoStore = false; // The weak init is a store, no need to do two.
John McCallf85e1932011-06-15 23:02:42 +00001762 }
1763 }
1764
1765 // Enter the cleanup scope.
1766 EmitAutoVarWithLifetime(*this, D, DeclPtr, lt);
1767 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001768 }
1769
Stephen Hines651f13c2014-04-23 16:59:28 -07001770 // Store the initial value into the alloca.
1771 if (DoStore)
1772 EmitStoreOfScalar(Arg, lv, /* isInitialization */ true);
1773
Reid Spencer5f016e22007-07-11 17:01:13 +00001774 llvm::Value *&DMEntry = LocalDeclMap[&D];
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001775 assert(!DMEntry && "Decl already exists in localdeclmap!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001776 DMEntry = DeclPtr;
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001777
1778 // Emit debug info for param declaration.
Alexey Samsonovfd00eec2012-05-04 07:39:27 +00001779 if (CGDebugInfo *DI = getDebugInfo()) {
Douglas Gregor4cdad312012-10-23 20:05:01 +00001780 if (CGM.getCodeGenOpts().getDebugInfo()
1781 >= CodeGenOptions::LimitedDebugInfo) {
Alexey Samsonovfd00eec2012-05-04 07:39:27 +00001782 DI->EmitDeclareOfArgVariable(&D, DeclPtr, ArgNo, Builder);
1783 }
1784 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00001785
1786 if (D.hasAttr<AnnotateAttr>())
1787 EmitVarAnnotations(&D, DeclPtr);
Reid Spencer5f016e22007-07-11 17:01:13 +00001788}