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