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 | |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 14 | #include "CGDebugInfo.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 15 | #include "CodeGenFunction.h" |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 16 | #include "CodeGenModule.h" |
Daniel Dunbar | de7fb84 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Ken Dyck | bdc601b | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 18 | #include "clang/AST/CharUnits.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 19 | #include "clang/AST/Decl.h" |
Anders Carlsson | 19567ee | 2008-08-25 01:38:19 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclObjC.h" |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 21 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 22 | #include "clang/Basic/TargetInfo.h" |
Chandler Carruth | 06057ce | 2010-06-15 23:19:56 +0000 | [diff] [blame] | 23 | #include "clang/Frontend/CodeGenOptions.h" |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 24 | #include "llvm/GlobalVariable.h" |
Anders Carlsson | 5d46315 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 25 | #include "llvm/Intrinsics.h" |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetData.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 27 | #include "llvm/Type.h" |
| 28 | using namespace clang; |
| 29 | using namespace CodeGen; |
| 30 | |
| 31 | |
| 32 | void CodeGenFunction::EmitDecl(const Decl &D) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 33 | switch (D.getKind()) { |
Douglas Gregor | 08688ac | 2010-04-23 02:02:43 +0000 | [diff] [blame] | 34 | case Decl::TranslationUnit: |
| 35 | case Decl::Namespace: |
| 36 | case Decl::UnresolvedUsingTypename: |
| 37 | case Decl::ClassTemplateSpecialization: |
| 38 | case Decl::ClassTemplatePartialSpecialization: |
| 39 | case Decl::TemplateTypeParm: |
| 40 | case Decl::UnresolvedUsingValue: |
Sean Hunt | 9a55591 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 41 | case Decl::NonTypeTemplateParm: |
Douglas Gregor | 08688ac | 2010-04-23 02:02:43 +0000 | [diff] [blame] | 42 | case Decl::CXXMethod: |
| 43 | case Decl::CXXConstructor: |
| 44 | case Decl::CXXDestructor: |
| 45 | case Decl::CXXConversion: |
| 46 | case Decl::Field: |
Francois Pichet | 41f5e66 | 2010-11-21 06:49:41 +0000 | [diff] [blame] | 47 | case Decl::IndirectField: |
Douglas Gregor | 08688ac | 2010-04-23 02:02:43 +0000 | [diff] [blame] | 48 | case Decl::ObjCIvar: |
| 49 | case Decl::ObjCAtDefsField: |
Chris Lattner | aa9fc46 | 2007-10-08 21:37:32 +0000 | [diff] [blame] | 50 | case Decl::ParmVar: |
Douglas Gregor | 08688ac | 2010-04-23 02:02:43 +0000 | [diff] [blame] | 51 | case Decl::ImplicitParam: |
| 52 | case Decl::ClassTemplate: |
| 53 | case Decl::FunctionTemplate: |
| 54 | case Decl::TemplateTemplateParm: |
| 55 | case Decl::ObjCMethod: |
| 56 | case Decl::ObjCCategory: |
| 57 | case Decl::ObjCProtocol: |
| 58 | case Decl::ObjCInterface: |
| 59 | case Decl::ObjCCategoryImpl: |
| 60 | case Decl::ObjCImplementation: |
| 61 | case Decl::ObjCProperty: |
| 62 | case Decl::ObjCCompatibleAlias: |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 63 | case Decl::AccessSpec: |
Douglas Gregor | 08688ac | 2010-04-23 02:02:43 +0000 | [diff] [blame] | 64 | case Decl::LinkageSpec: |
| 65 | case Decl::ObjCPropertyImpl: |
| 66 | case Decl::ObjCClass: |
| 67 | case Decl::ObjCForwardProtocol: |
| 68 | case Decl::FileScopeAsm: |
| 69 | case Decl::Friend: |
| 70 | case Decl::FriendTemplate: |
| 71 | case Decl::Block: |
| 72 | |
| 73 | assert(0 && "Declaration not 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: |
| 81 | case Decl::UsingDirective: // using namespace X; [C++] |
Douglas Gregor | 08688ac | 2010-04-23 02:02:43 +0000 | [diff] [blame] | 82 | case Decl::NamespaceAlias: |
Anders Carlsson | 7b0ca3f | 2009-12-03 17:26:31 +0000 | [diff] [blame] | 83 | case Decl::StaticAssert: // static_assert(X, ""); [C++0x] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 84 | // None of these decls require codegen support. |
| 85 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 86 | |
Daniel Dunbar | 662174c8 | 2008-08-29 17:28:43 +0000 | [diff] [blame] | 87 | case Decl::Var: { |
| 88 | const VarDecl &VD = cast<VarDecl>(D); |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 89 | assert(VD.isLocalVarDecl() && |
Daniel Dunbar | 662174c8 | 2008-08-29 17:28:43 +0000 | [diff] [blame] | 90 | "Should not see file-scope variables inside a function!"); |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 91 | return EmitVarDecl(VD); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 92 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 93 | |
Anders Carlsson | fcdbb93 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 94 | case Decl::Typedef: { // typedef int X; |
| 95 | const TypedefDecl &TD = cast<TypedefDecl>(D); |
| 96 | QualType Ty = TD.getUnderlyingType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | |
Anders Carlsson | fcdbb93 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 98 | if (Ty->isVariablyModifiedType()) |
| 99 | EmitVLASize(Ty); |
| 100 | } |
Daniel Dunbar | 662174c8 | 2008-08-29 17:28:43 +0000 | [diff] [blame] | 101 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 102 | } |
| 103 | |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 104 | /// EmitVarDecl - This method handles emission of any variable declaration |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 105 | /// inside a function, including static vars etc. |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 106 | void CodeGenFunction::EmitVarDecl(const VarDecl &D) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 107 | switch (D.getStorageClass()) { |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 108 | case SC_None: |
| 109 | case SC_Auto: |
| 110 | case SC_Register: |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 111 | return EmitAutoVarDecl(D); |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 112 | case SC_Static: { |
Anders Carlsson | f6b89a1 | 2010-02-07 02:03:08 +0000 | [diff] [blame] | 113 | llvm::GlobalValue::LinkageTypes Linkage = |
| 114 | llvm::GlobalValue::InternalLinkage; |
| 115 | |
John McCall | 8b24233 | 2010-05-25 04:30:21 +0000 | [diff] [blame] | 116 | // If the function definition has some sort of weak linkage, its |
| 117 | // static variables should also be weak so that they get properly |
| 118 | // uniqued. We can't do this in C, though, because there's no |
| 119 | // standard way to agree on which variables are the same (i.e. |
| 120 | // there's no mangling). |
| 121 | if (getContext().getLangOptions().CPlusPlus) |
| 122 | if (llvm::GlobalValue::isWeakForLinker(CurFn->getLinkage())) |
| 123 | Linkage = CurFn->getLinkage(); |
Anders Carlsson | f6b89a1 | 2010-02-07 02:03:08 +0000 | [diff] [blame] | 124 | |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 125 | return EmitStaticVarDecl(D, Linkage); |
Anders Carlsson | f6b89a1 | 2010-02-07 02:03:08 +0000 | [diff] [blame] | 126 | } |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 127 | case SC_Extern: |
| 128 | case SC_PrivateExtern: |
Lauro Ramos Venancio | fea90b8 | 2008-02-16 22:30:38 +0000 | [diff] [blame] | 129 | // Don't emit it now, allow it to be emitted lazily on its first use. |
| 130 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 131 | } |
Daniel Dunbar | 5466c7b | 2009-04-14 02:25:56 +0000 | [diff] [blame] | 132 | |
| 133 | assert(0 && "Unknown storage class"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Chris Lattner | 761acc1 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 136 | static std::string GetStaticDeclName(CodeGenFunction &CGF, const VarDecl &D, |
| 137 | const char *Separator) { |
| 138 | CodeGenModule &CGM = CGF.CGM; |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 139 | if (CGF.getContext().getLangOptions().CPlusPlus) { |
Anders Carlsson | 9a20d55 | 2010-06-22 16:16:50 +0000 | [diff] [blame] | 140 | llvm::StringRef Name = CGM.getMangledName(&D); |
| 141 | return Name.str(); |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 142 | } |
Chris Lattner | 761acc1 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 143 | |
| 144 | std::string ContextName; |
Fariborz Jahanian | faa5bfc | 2010-11-30 23:07:14 +0000 | [diff] [blame] | 145 | if (!CGF.CurFuncDecl) { |
| 146 | // Better be in a block declared in global scope. |
| 147 | const NamedDecl *ND = cast<NamedDecl>(&D); |
| 148 | const DeclContext *DC = ND->getDeclContext(); |
| 149 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) { |
| 150 | MangleBuffer Name; |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 151 | CGM.getBlockMangledName(GlobalDecl(), Name, BD); |
Fariborz Jahanian | faa5bfc | 2010-11-30 23:07:14 +0000 | [diff] [blame] | 152 | ContextName = Name.getString(); |
| 153 | } |
| 154 | else |
| 155 | assert(0 && "Unknown context for block static var decl"); |
| 156 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CGF.CurFuncDecl)) { |
Anders Carlsson | 9a20d55 | 2010-06-22 16:16:50 +0000 | [diff] [blame] | 157 | llvm::StringRef Name = CGM.getMangledName(FD); |
| 158 | ContextName = Name.str(); |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 159 | } else if (isa<ObjCMethodDecl>(CGF.CurFuncDecl)) |
Chris Lattner | 761acc1 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 160 | ContextName = CGF.CurFn->getName(); |
| 161 | else |
Fariborz Jahanian | faa5bfc | 2010-11-30 23:07:14 +0000 | [diff] [blame] | 162 | assert(0 && "Unknown context for static var decl"); |
Chris Lattner | 761acc1 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 163 | |
| 164 | return ContextName + Separator + D.getNameAsString(); |
| 165 | } |
| 166 | |
Daniel Dunbar | 0096acf | 2009-02-25 19:24:29 +0000 | [diff] [blame] | 167 | llvm::GlobalVariable * |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 168 | CodeGenFunction::CreateStaticVarDecl(const VarDecl &D, |
| 169 | const char *Separator, |
| 170 | llvm::GlobalValue::LinkageTypes Linkage) { |
Chris Lattner | 8bcfc5b | 2008-04-06 23:10:54 +0000 | [diff] [blame] | 171 | QualType Ty = D.getType(); |
Eli Friedman | 3c2b317 | 2008-02-15 12:20:59 +0000 | [diff] [blame] | 172 | assert(Ty->isConstantSizeType() && "VLAs can't be static"); |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 173 | |
Chris Lattner | 761acc1 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 174 | std::string Name = GetStaticDeclName(*this, D, Separator); |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 175 | |
Daniel Dunbar | 0096acf | 2009-02-25 19:24:29 +0000 | [diff] [blame] | 176 | const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty); |
Anders Carlsson | 41f8a13 | 2009-09-26 18:16:06 +0000 | [diff] [blame] | 177 | llvm::GlobalVariable *GV = |
| 178 | new llvm::GlobalVariable(CGM.getModule(), LTy, |
| 179 | Ty.isConstant(getContext()), Linkage, |
| 180 | CGM.EmitNullConstant(D.getType()), Name, 0, |
| 181 | D.isThreadSpecified(), Ty.getAddressSpace()); |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 182 | GV->setAlignment(getContext().getDeclAlign(&D).getQuantity()); |
John McCall | 112c967 | 2010-11-02 21:04:24 +0000 | [diff] [blame] | 183 | if (Linkage != llvm::GlobalValue::InternalLinkage) |
| 184 | GV->setVisibility(CurFn->getVisibility()); |
Anders Carlsson | 41f8a13 | 2009-09-26 18:16:06 +0000 | [diff] [blame] | 185 | return GV; |
Daniel Dunbar | 0096acf | 2009-02-25 19:24:29 +0000 | [diff] [blame] | 186 | } |
| 187 | |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 188 | /// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the |
Chris Lattner | 761acc1 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 189 | /// global variable that has already been created for it. If the initializer |
| 190 | /// has a different type than GV does, this may free GV and return a different |
| 191 | /// one. Otherwise it just returns GV. |
| 192 | llvm::GlobalVariable * |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 193 | CodeGenFunction::AddInitializerToStaticVarDecl(const VarDecl &D, |
| 194 | llvm::GlobalVariable *GV) { |
Chris Lattner | 761acc1 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 195 | llvm::Constant *Init = CGM.EmitConstantExpr(D.getInit(), D.getType(), this); |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 196 | |
Chris Lattner | 761acc1 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 197 | // If constant emission failed, then this should be a C++ static |
| 198 | // initializer. |
| 199 | if (!Init) { |
| 200 | if (!getContext().getLangOptions().CPlusPlus) |
| 201 | CGM.ErrorUnsupported(D.getInit(), "constant l-value expression"); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 202 | else if (Builder.GetInsertBlock()) { |
Anders Carlsson | 071c810 | 2010-01-26 04:02:23 +0000 | [diff] [blame] | 203 | // Since we have a static initializer, this global variable can't |
| 204 | // be constant. |
| 205 | GV->setConstant(false); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 206 | |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 207 | EmitCXXGuardedInit(D, GV); |
Anders Carlsson | 071c810 | 2010-01-26 04:02:23 +0000 | [diff] [blame] | 208 | } |
Chris Lattner | 761acc1 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 209 | return GV; |
| 210 | } |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 211 | |
Chris Lattner | 761acc1 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 212 | // The initializer may differ in type from the global. Rewrite |
| 213 | // the global to match the initializer. (We have to do this |
| 214 | // because some types, like unions, can't be completely represented |
| 215 | // in the LLVM type system.) |
John McCall | 9c20fa9 | 2010-09-03 18:58:50 +0000 | [diff] [blame] | 216 | if (GV->getType()->getElementType() != Init->getType()) { |
Chris Lattner | 761acc1 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 217 | llvm::GlobalVariable *OldGV = GV; |
| 218 | |
| 219 | GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), |
| 220 | OldGV->isConstant(), |
| 221 | OldGV->getLinkage(), Init, "", |
John McCall | 112c967 | 2010-11-02 21:04:24 +0000 | [diff] [blame] | 222 | /*InsertBefore*/ OldGV, |
| 223 | D.isThreadSpecified(), |
Chris Lattner | 761acc1 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 224 | D.getType().getAddressSpace()); |
John McCall | 112c967 | 2010-11-02 21:04:24 +0000 | [diff] [blame] | 225 | GV->setVisibility(OldGV->getVisibility()); |
Chris Lattner | 761acc1 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 226 | |
| 227 | // Steal the name of the old global |
| 228 | GV->takeName(OldGV); |
| 229 | |
| 230 | // Replace all uses of the old global with the new global |
| 231 | llvm::Constant *NewPtrForOldDecl = |
| 232 | llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); |
| 233 | OldGV->replaceAllUsesWith(NewPtrForOldDecl); |
| 234 | |
| 235 | // Erase the old global, since it is no longer used. |
| 236 | OldGV->eraseFromParent(); |
| 237 | } |
| 238 | |
| 239 | GV->setInitializer(Init); |
| 240 | return GV; |
| 241 | } |
| 242 | |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 243 | void CodeGenFunction::EmitStaticVarDecl(const VarDecl &D, |
Anders Carlsson | f6b89a1 | 2010-02-07 02:03:08 +0000 | [diff] [blame] | 244 | llvm::GlobalValue::LinkageTypes Linkage) { |
Daniel Dunbar | a985b31 | 2009-02-25 19:45:19 +0000 | [diff] [blame] | 245 | llvm::Value *&DMEntry = LocalDeclMap[&D]; |
| 246 | assert(DMEntry == 0 && "Decl already exists in localdeclmap!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 247 | |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 248 | llvm::GlobalVariable *GV = CreateStaticVarDecl(D, ".", Linkage); |
Daniel Dunbar | a985b31 | 2009-02-25 19:45:19 +0000 | [diff] [blame] | 249 | |
Daniel Dunbar | e5731f8 | 2009-02-25 20:08:33 +0000 | [diff] [blame] | 250 | // Store into LocalDeclMap before generating initializer to handle |
| 251 | // circular references. |
| 252 | DMEntry = GV; |
| 253 | |
John McCall | fe67f3b | 2010-05-04 20:45:42 +0000 | [diff] [blame] | 254 | // We can't have a VLA here, but we can have a pointer to a VLA, |
| 255 | // even though that doesn't really make any sense. |
Eli Friedman | c62aad8 | 2009-04-20 03:54:15 +0000 | [diff] [blame] | 256 | // Make sure to evaluate VLA bounds now so that we have them for later. |
| 257 | if (D.getType()->isVariablyModifiedType()) |
| 258 | EmitVLASize(D.getType()); |
Fariborz Jahanian | 0934914 | 2010-09-07 23:26:17 +0000 | [diff] [blame] | 259 | |
| 260 | // Local static block variables must be treated as globals as they may be |
| 261 | // referenced in their RHS initializer block-literal expresion. |
| 262 | CGM.setStaticLocalDeclAddress(&D, GV); |
Eli Friedman | c62aad8 | 2009-04-20 03:54:15 +0000 | [diff] [blame] | 263 | |
Chris Lattner | 761acc1 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 264 | // If this value has an initializer, emit it. |
| 265 | if (D.getInit()) |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 266 | GV = AddInitializerToStaticVarDecl(D, GV); |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 267 | |
Chris Lattner | 0af9523 | 2010-03-10 23:59:59 +0000 | [diff] [blame] | 268 | GV->setAlignment(getContext().getDeclAlign(&D).getQuantity()); |
| 269 | |
Daniel Dunbar | 30510ab | 2009-02-12 23:32:54 +0000 | [diff] [blame] | 270 | // FIXME: Merge attribute handling. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 271 | if (const AnnotateAttr *AA = D.getAttr<AnnotateAttr>()) { |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 272 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 273 | llvm::Constant *Ann = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 274 | CGM.EmitAnnotateAttr(GV, AA, |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 275 | SM.getInstantiationLineNumber(D.getLocation())); |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 276 | CGM.AddAnnotation(Ann); |
| 277 | } |
| 278 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 279 | if (const SectionAttr *SA = D.getAttr<SectionAttr>()) |
Daniel Dunbar | 30510ab | 2009-02-12 23:32:54 +0000 | [diff] [blame] | 280 | GV->setSection(SA->getName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 281 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 282 | if (D.hasAttr<UsedAttr>()) |
Daniel Dunbar | 5c61d97 | 2009-02-13 22:08:43 +0000 | [diff] [blame] | 283 | CGM.AddUsedGlobal(GV); |
| 284 | |
Daniel Dunbar | e5731f8 | 2009-02-25 20:08:33 +0000 | [diff] [blame] | 285 | // We may have to cast the constant because of the initializer |
| 286 | // mismatch above. |
| 287 | // |
| 288 | // FIXME: It is really dangerous to store this in the map; if anyone |
| 289 | // RAUW's the GV uses of this constant will be invalid. |
Eli Friedman | 2659052 | 2008-06-08 01:23:18 +0000 | [diff] [blame] | 290 | const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(D.getType()); |
Chris Lattner | c0f31fd | 2010-12-02 04:27:29 +0000 | [diff] [blame] | 291 | const llvm::Type *LPtrTy = LTy->getPointerTo(D.getType().getAddressSpace()); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 292 | DMEntry = llvm::ConstantExpr::getBitCast(GV, LPtrTy); |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 293 | |
| 294 | // Emit global variable debug descriptor for static vars. |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 295 | CGDebugInfo *DI = getDebugInfo(); |
Mike Stump | 4451bd9 | 2009-02-20 00:19:45 +0000 | [diff] [blame] | 296 | if (DI) { |
Daniel Dunbar | 66031a5 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 297 | DI->setLocation(D.getLocation()); |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 298 | DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(GV), &D); |
| 299 | } |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 300 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 301 | |
Anders Carlsson | 7dfa407 | 2009-09-12 02:14:24 +0000 | [diff] [blame] | 302 | unsigned CodeGenFunction::getByRefValueLLVMField(const ValueDecl *VD) const { |
| 303 | assert(ByRefValueInfo.count(VD) && "Did not find value!"); |
| 304 | |
| 305 | return ByRefValueInfo.find(VD)->second.second; |
| 306 | } |
| 307 | |
Fariborz Jahanian | 52a80e1 | 2011-01-26 23:08:27 +0000 | [diff] [blame] | 308 | llvm::Value *CodeGenFunction::BuildBlockByrefAddress(llvm::Value *BaseAddr, |
| 309 | const VarDecl *V) { |
| 310 | llvm::Value *Loc = Builder.CreateStructGEP(BaseAddr, 1, "forwarding"); |
| 311 | Loc = Builder.CreateLoad(Loc); |
| 312 | Loc = Builder.CreateStructGEP(Loc, getByRefValueLLVMField(V), |
| 313 | V->getNameAsString()); |
| 314 | return Loc; |
| 315 | } |
| 316 | |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 317 | /// BuildByRefType - This routine changes a __block variable declared as T x |
| 318 | /// into: |
| 319 | /// |
| 320 | /// struct { |
| 321 | /// void *__isa; |
| 322 | /// void *__forwarding; |
| 323 | /// int32_t __flags; |
| 324 | /// int32_t __size; |
Mike Stump | 39605b4 | 2009-09-22 02:12:52 +0000 | [diff] [blame] | 325 | /// void *__copy_helper; // only if needed |
| 326 | /// void *__destroy_helper; // only if needed |
| 327 | /// char padding[X]; // only if needed |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 328 | /// T x; |
| 329 | /// } x |
| 330 | /// |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame^] | 331 | const llvm::Type *CodeGenFunction::BuildByRefType(const VarDecl *D) { |
Anders Carlsson | 7dfa407 | 2009-09-12 02:14:24 +0000 | [diff] [blame] | 332 | std::pair<const llvm::Type *, unsigned> &Info = ByRefValueInfo[D]; |
| 333 | if (Info.first) |
| 334 | return Info.first; |
| 335 | |
Anders Carlsson | 9ad5513 | 2009-09-09 02:51:03 +0000 | [diff] [blame] | 336 | QualType Ty = D->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 337 | |
Anders Carlsson | 18be84c | 2009-09-12 02:44:18 +0000 | [diff] [blame] | 338 | std::vector<const llvm::Type *> Types; |
Anders Carlsson | 3604386 | 2009-09-10 01:32:12 +0000 | [diff] [blame] | 339 | |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 340 | const llvm::PointerType *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
Anders Carlsson | 18be84c | 2009-09-12 02:44:18 +0000 | [diff] [blame] | 341 | |
Anders Carlsson | 3604386 | 2009-09-10 01:32:12 +0000 | [diff] [blame] | 342 | llvm::PATypeHolder ByRefTypeHolder = llvm::OpaqueType::get(VMContext); |
| 343 | |
Anders Carlsson | 18be84c | 2009-09-12 02:44:18 +0000 | [diff] [blame] | 344 | // void *__isa; |
| 345 | Types.push_back(Int8PtrTy); |
| 346 | |
| 347 | // void *__forwarding; |
| 348 | Types.push_back(llvm::PointerType::getUnqual(ByRefTypeHolder)); |
| 349 | |
| 350 | // int32_t __flags; |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 351 | Types.push_back(Int32Ty); |
Anders Carlsson | 18be84c | 2009-09-12 02:44:18 +0000 | [diff] [blame] | 352 | |
| 353 | // int32_t __size; |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 354 | Types.push_back(Int32Ty); |
Anders Carlsson | 18be84c | 2009-09-12 02:44:18 +0000 | [diff] [blame] | 355 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame^] | 356 | bool HasCopyAndDispose = getContext().BlockRequiresCopying(Ty); |
Anders Carlsson | 18be84c | 2009-09-12 02:44:18 +0000 | [diff] [blame] | 357 | if (HasCopyAndDispose) { |
| 358 | /// void *__copy_helper; |
| 359 | Types.push_back(Int8PtrTy); |
| 360 | |
| 361 | /// void *__destroy_helper; |
| 362 | Types.push_back(Int8PtrTy); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 363 | } |
Anders Carlsson | 18be84c | 2009-09-12 02:44:18 +0000 | [diff] [blame] | 364 | |
| 365 | bool Packed = false; |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 366 | CharUnits Align = getContext().getDeclAlign(D); |
Ken Dyck | 06f486e | 2011-01-18 02:01:14 +0000 | [diff] [blame] | 367 | if (Align > getContext().toCharUnitsFromBits(Target.getPointerAlign(0))) { |
Anders Carlsson | 18be84c | 2009-09-12 02:44:18 +0000 | [diff] [blame] | 368 | // We have to insert padding. |
| 369 | |
| 370 | // The struct above has 2 32-bit integers. |
| 371 | unsigned CurrentOffsetInBytes = 4 * 2; |
| 372 | |
| 373 | // And either 2 or 4 pointers. |
| 374 | CurrentOffsetInBytes += (HasCopyAndDispose ? 4 : 2) * |
| 375 | CGM.getTargetData().getTypeAllocSize(Int8PtrTy); |
| 376 | |
| 377 | // Align the offset. |
| 378 | unsigned AlignedOffsetInBytes = |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 379 | llvm::RoundUpToAlignment(CurrentOffsetInBytes, Align.getQuantity()); |
Anders Carlsson | 18be84c | 2009-09-12 02:44:18 +0000 | [diff] [blame] | 380 | |
| 381 | unsigned NumPaddingBytes = AlignedOffsetInBytes - CurrentOffsetInBytes; |
Anders Carlsson | e0c8822 | 2009-09-13 17:55:13 +0000 | [diff] [blame] | 382 | if (NumPaddingBytes > 0) { |
| 383 | const llvm::Type *Ty = llvm::Type::getInt8Ty(VMContext); |
Mike Stump | 04c688a | 2009-10-21 00:42:55 +0000 | [diff] [blame] | 384 | // FIXME: We need a sema error for alignment larger than the minimum of |
| 385 | // the maximal stack alignmint and the alignment of malloc on the system. |
Anders Carlsson | e0c8822 | 2009-09-13 17:55:13 +0000 | [diff] [blame] | 386 | if (NumPaddingBytes > 1) |
| 387 | Ty = llvm::ArrayType::get(Ty, NumPaddingBytes); |
Anders Carlsson | 18be84c | 2009-09-12 02:44:18 +0000 | [diff] [blame] | 388 | |
Anders Carlsson | e0c8822 | 2009-09-13 17:55:13 +0000 | [diff] [blame] | 389 | Types.push_back(Ty); |
Anders Carlsson | 18be84c | 2009-09-12 02:44:18 +0000 | [diff] [blame] | 390 | |
Anders Carlsson | e0c8822 | 2009-09-13 17:55:13 +0000 | [diff] [blame] | 391 | // We want a packed struct. |
| 392 | Packed = true; |
| 393 | } |
Anders Carlsson | 18be84c | 2009-09-12 02:44:18 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | // T x; |
Fariborz Jahanian | 469a20d | 2010-09-03 23:07:53 +0000 | [diff] [blame] | 397 | Types.push_back(ConvertTypeForMem(Ty)); |
Anders Carlsson | 7dfa407 | 2009-09-12 02:14:24 +0000 | [diff] [blame] | 398 | |
Anders Carlsson | 18be84c | 2009-09-12 02:44:18 +0000 | [diff] [blame] | 399 | const llvm::Type *T = llvm::StructType::get(VMContext, Types, Packed); |
Anders Carlsson | 3604386 | 2009-09-10 01:32:12 +0000 | [diff] [blame] | 400 | |
| 401 | cast<llvm::OpaqueType>(ByRefTypeHolder.get())->refineAbstractTypeTo(T); |
| 402 | CGM.getModule().addTypeName("struct.__block_byref_" + D->getNameAsString(), |
| 403 | ByRefTypeHolder.get()); |
| 404 | |
Anders Carlsson | 7dfa407 | 2009-09-12 02:14:24 +0000 | [diff] [blame] | 405 | Info.first = ByRefTypeHolder.get(); |
Anders Carlsson | 18be84c | 2009-09-12 02:44:18 +0000 | [diff] [blame] | 406 | |
| 407 | Info.second = Types.size() - 1; |
Anders Carlsson | 7dfa407 | 2009-09-12 02:14:24 +0000 | [diff] [blame] | 408 | |
| 409 | return Info.first; |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 410 | } |
| 411 | |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 412 | namespace { |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 413 | struct CallArrayDtor : EHScopeStack::Cleanup { |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 414 | CallArrayDtor(const CXXDestructorDecl *Dtor, |
| 415 | const ConstantArrayType *Type, |
| 416 | llvm::Value *Loc) |
| 417 | : Dtor(Dtor), Type(Type), Loc(Loc) {} |
| 418 | |
| 419 | const CXXDestructorDecl *Dtor; |
| 420 | const ConstantArrayType *Type; |
| 421 | llvm::Value *Loc; |
| 422 | |
| 423 | void Emit(CodeGenFunction &CGF, bool IsForEH) { |
| 424 | QualType BaseElementTy = CGF.getContext().getBaseElementType(Type); |
| 425 | const llvm::Type *BasePtr = CGF.ConvertType(BaseElementTy); |
| 426 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
| 427 | llvm::Value *BaseAddrPtr = CGF.Builder.CreateBitCast(Loc, BasePtr); |
| 428 | CGF.EmitCXXAggrDestructorCall(Dtor, Type, BaseAddrPtr); |
| 429 | } |
| 430 | }; |
| 431 | |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 432 | struct CallVarDtor : EHScopeStack::Cleanup { |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 433 | CallVarDtor(const CXXDestructorDecl *Dtor, |
| 434 | llvm::Value *NRVOFlag, |
| 435 | llvm::Value *Loc) |
| 436 | : Dtor(Dtor), NRVOFlag(NRVOFlag), Loc(Loc) {} |
| 437 | |
| 438 | const CXXDestructorDecl *Dtor; |
| 439 | llvm::Value *NRVOFlag; |
| 440 | llvm::Value *Loc; |
| 441 | |
| 442 | void Emit(CodeGenFunction &CGF, bool IsForEH) { |
| 443 | // Along the exceptions path we always execute the dtor. |
| 444 | bool NRVO = !IsForEH && NRVOFlag; |
| 445 | |
| 446 | llvm::BasicBlock *SkipDtorBB = 0; |
| 447 | if (NRVO) { |
| 448 | // If we exited via NRVO, we skip the destructor call. |
| 449 | llvm::BasicBlock *RunDtorBB = CGF.createBasicBlock("nrvo.unused"); |
| 450 | SkipDtorBB = CGF.createBasicBlock("nrvo.skipdtor"); |
| 451 | llvm::Value *DidNRVO = CGF.Builder.CreateLoad(NRVOFlag, "nrvo.val"); |
| 452 | CGF.Builder.CreateCondBr(DidNRVO, SkipDtorBB, RunDtorBB); |
| 453 | CGF.EmitBlock(RunDtorBB); |
| 454 | } |
| 455 | |
| 456 | CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, |
| 457 | /*ForVirtualBase=*/false, Loc); |
| 458 | |
| 459 | if (NRVO) CGF.EmitBlock(SkipDtorBB); |
| 460 | } |
| 461 | }; |
| 462 | } |
| 463 | |
John McCall | d871509 | 2010-07-21 06:13:08 +0000 | [diff] [blame] | 464 | namespace { |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 465 | struct CallStackRestore : EHScopeStack::Cleanup { |
John McCall | d871509 | 2010-07-21 06:13:08 +0000 | [diff] [blame] | 466 | llvm::Value *Stack; |
| 467 | CallStackRestore(llvm::Value *Stack) : Stack(Stack) {} |
| 468 | void Emit(CodeGenFunction &CGF, bool IsForEH) { |
| 469 | llvm::Value *V = CGF.Builder.CreateLoad(Stack, "tmp"); |
| 470 | llvm::Value *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stackrestore); |
| 471 | CGF.Builder.CreateCall(F, V); |
| 472 | } |
| 473 | }; |
| 474 | |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 475 | struct CallCleanupFunction : EHScopeStack::Cleanup { |
John McCall | d871509 | 2010-07-21 06:13:08 +0000 | [diff] [blame] | 476 | llvm::Constant *CleanupFn; |
| 477 | const CGFunctionInfo &FnInfo; |
| 478 | llvm::Value *Addr; |
| 479 | const VarDecl &Var; |
| 480 | |
| 481 | CallCleanupFunction(llvm::Constant *CleanupFn, const CGFunctionInfo *Info, |
| 482 | llvm::Value *Addr, const VarDecl *Var) |
| 483 | : CleanupFn(CleanupFn), FnInfo(*Info), Addr(Addr), Var(*Var) {} |
| 484 | |
| 485 | void Emit(CodeGenFunction &CGF, bool IsForEH) { |
| 486 | // In some cases, the type of the function argument will be different from |
| 487 | // the type of the pointer. An example of this is |
| 488 | // void f(void* arg); |
| 489 | // __attribute__((cleanup(f))) void *g; |
| 490 | // |
| 491 | // To fix this we insert a bitcast here. |
| 492 | QualType ArgTy = FnInfo.arg_begin()->type; |
| 493 | llvm::Value *Arg = |
| 494 | CGF.Builder.CreateBitCast(Addr, CGF.ConvertType(ArgTy)); |
| 495 | |
| 496 | CallArgList Args; |
| 497 | Args.push_back(std::make_pair(RValue::get(Arg), |
| 498 | CGF.getContext().getPointerType(Var.getType()))); |
| 499 | CGF.EmitCall(FnInfo, CleanupFn, ReturnValueSlot(), Args); |
| 500 | } |
| 501 | }; |
| 502 | |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 503 | struct CallBlockRelease : EHScopeStack::Cleanup { |
John McCall | d871509 | 2010-07-21 06:13:08 +0000 | [diff] [blame] | 504 | llvm::Value *Addr; |
| 505 | CallBlockRelease(llvm::Value *Addr) : Addr(Addr) {} |
| 506 | |
| 507 | void Emit(CodeGenFunction &CGF, bool IsForEH) { |
| 508 | llvm::Value *V = CGF.Builder.CreateStructGEP(Addr, 1, "forwarding"); |
| 509 | V = CGF.Builder.CreateLoad(V); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame^] | 510 | CGF.BuildBlockRelease(V, BlockFunction::BLOCK_FIELD_IS_BYREF); |
John McCall | d871509 | 2010-07-21 06:13:08 +0000 | [diff] [blame] | 511 | } |
| 512 | }; |
| 513 | } |
| 514 | |
Chris Lattner | 94cd011 | 2010-12-01 02:05:19 +0000 | [diff] [blame] | 515 | |
| 516 | /// canEmitInitWithFewStoresAfterMemset - Decide whether we can emit the |
| 517 | /// non-zero parts of the specified initializer with equal or fewer than |
| 518 | /// NumStores scalar stores. |
| 519 | static bool canEmitInitWithFewStoresAfterMemset(llvm::Constant *Init, |
| 520 | unsigned &NumStores) { |
Chris Lattner | 70b0294 | 2010-12-02 01:58:41 +0000 | [diff] [blame] | 521 | // Zero and Undef never requires any extra stores. |
| 522 | if (isa<llvm::ConstantAggregateZero>(Init) || |
| 523 | isa<llvm::ConstantPointerNull>(Init) || |
| 524 | isa<llvm::UndefValue>(Init)) |
| 525 | return true; |
| 526 | if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) || |
| 527 | isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) || |
| 528 | isa<llvm::ConstantExpr>(Init)) |
| 529 | return Init->isNullValue() || NumStores--; |
| 530 | |
| 531 | // See if we can emit each element. |
| 532 | if (isa<llvm::ConstantArray>(Init) || isa<llvm::ConstantStruct>(Init)) { |
| 533 | for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) { |
| 534 | llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i)); |
| 535 | if (!canEmitInitWithFewStoresAfterMemset(Elt, NumStores)) |
| 536 | return false; |
| 537 | } |
| 538 | return true; |
| 539 | } |
Chris Lattner | 94cd011 | 2010-12-01 02:05:19 +0000 | [diff] [blame] | 540 | |
| 541 | // Anything else is hard and scary. |
| 542 | return false; |
| 543 | } |
| 544 | |
| 545 | /// emitStoresForInitAfterMemset - For inits that |
| 546 | /// canEmitInitWithFewStoresAfterMemset returned true for, emit the scalar |
| 547 | /// stores that would be required. |
| 548 | static void emitStoresForInitAfterMemset(llvm::Constant *Init, llvm::Value *Loc, |
| 549 | CGBuilderTy &Builder) { |
| 550 | // Zero doesn't require any stores. |
Chris Lattner | 70b0294 | 2010-12-02 01:58:41 +0000 | [diff] [blame] | 551 | if (isa<llvm::ConstantAggregateZero>(Init) || |
| 552 | isa<llvm::ConstantPointerNull>(Init) || |
| 553 | isa<llvm::UndefValue>(Init)) |
| 554 | return; |
Chris Lattner | 94cd011 | 2010-12-01 02:05:19 +0000 | [diff] [blame] | 555 | |
Chris Lattner | 70b0294 | 2010-12-02 01:58:41 +0000 | [diff] [blame] | 556 | if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) || |
| 557 | isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) || |
| 558 | isa<llvm::ConstantExpr>(Init)) { |
| 559 | if (!Init->isNullValue()) |
| 560 | Builder.CreateStore(Init, Loc); |
| 561 | return; |
| 562 | } |
Chris Lattner | 94cd011 | 2010-12-01 02:05:19 +0000 | [diff] [blame] | 563 | |
Chris Lattner | 70b0294 | 2010-12-02 01:58:41 +0000 | [diff] [blame] | 564 | assert((isa<llvm::ConstantStruct>(Init) || isa<llvm::ConstantArray>(Init)) && |
| 565 | "Unknown value type!"); |
| 566 | |
| 567 | for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) { |
| 568 | llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i)); |
| 569 | if (Elt->isNullValue()) continue; |
| 570 | |
| 571 | // Otherwise, get a pointer to the element and emit it. |
| 572 | emitStoresForInitAfterMemset(Elt, Builder.CreateConstGEP2_32(Loc, 0, i), |
| 573 | Builder); |
| 574 | } |
Chris Lattner | 94cd011 | 2010-12-01 02:05:19 +0000 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | |
| 578 | /// shouldUseMemSetPlusStoresToInitialize - Decide whether we should use memset |
| 579 | /// plus some stores to initialize a local variable instead of using a memcpy |
| 580 | /// from a constant global. It is beneficial to use memset if the global is all |
| 581 | /// zeros, or mostly zeros and large. |
| 582 | static bool shouldUseMemSetPlusStoresToInitialize(llvm::Constant *Init, |
| 583 | uint64_t GlobalSize) { |
| 584 | // If a global is all zeros, always use a memset. |
| 585 | if (isa<llvm::ConstantAggregateZero>(Init)) return true; |
| 586 | |
| 587 | |
| 588 | // If a non-zero global is <= 32 bytes, always use a memcpy. If it is large, |
| 589 | // do it if it will require 6 or fewer scalar stores. |
| 590 | // TODO: Should budget depends on the size? Avoiding a large global warrants |
| 591 | // plopping in more stores. |
| 592 | unsigned StoreBudget = 6; |
| 593 | uint64_t SizeLimit = 32; |
| 594 | |
| 595 | return GlobalSize > SizeLimit && |
| 596 | canEmitInitWithFewStoresAfterMemset(Init, StoreBudget); |
| 597 | } |
| 598 | |
| 599 | |
Nick Lewycky | a9de3fa | 2010-12-30 20:21:55 +0000 | [diff] [blame] | 600 | /// EmitAutoVarDecl - Emit code and set up an entry in LocalDeclMap for a |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 601 | /// variable declaration with auto, register, or no storage class specifier. |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 602 | /// These turn into simple stack objects, or GlobalValues depending on target. |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 603 | void CodeGenFunction::EmitAutoVarDecl(const VarDecl &D, |
| 604 | SpecialInitFn *SpecialInit) { |
Chris Lattner | 8bcfc5b | 2008-04-06 23:10:54 +0000 | [diff] [blame] | 605 | QualType Ty = D.getType(); |
Daniel Dunbar | 91a16fa | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 606 | unsigned Alignment = getContext().getDeclAlign(&D).getQuantity(); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 607 | bool isByRef = D.hasAttr<BlocksAttr>(); |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 608 | bool needsDispose = false; |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 609 | CharUnits Align = CharUnits::Zero(); |
Chris Lattner | 761acc1 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 610 | bool IsSimpleConstantInitializer = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 611 | |
Douglas Gregor | d86c477 | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 612 | bool NRVO = false; |
Douglas Gregor | 3d91bbc | 2010-05-17 15:52:46 +0000 | [diff] [blame] | 613 | llvm::Value *NRVOFlag = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 614 | llvm::Value *DeclPtr; |
Eli Friedman | 3c2b317 | 2008-02-15 12:20:59 +0000 | [diff] [blame] | 615 | if (Ty->isConstantSizeType()) { |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 616 | if (!Target.useGlobalsForAutomaticVariables()) { |
Douglas Gregor | d86c477 | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 617 | NRVO = getContext().getLangOptions().ElideConstructors && |
| 618 | D.isNRVOVariable(); |
Chris Lattner | ff933b7 | 2009-12-05 06:49:57 +0000 | [diff] [blame] | 619 | // If this value is an array or struct, is POD, and if the initializer is |
Douglas Gregor | d86c477 | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 620 | // a staticly determinable constant, try to optimize it (unless the NRVO |
| 621 | // is already optimizing this). |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 622 | if (!NRVO && D.getInit() && !isByRef && |
Chris Lattner | ff933b7 | 2009-12-05 06:49:57 +0000 | [diff] [blame] | 623 | (Ty->isArrayType() || Ty->isRecordType()) && |
| 624 | Ty->isPODType() && |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 625 | D.getInit()->isConstantInitializer(getContext(), false)) { |
Chris Lattner | ff933b7 | 2009-12-05 06:49:57 +0000 | [diff] [blame] | 626 | // If this variable is marked 'const', emit the value as a global. |
| 627 | if (CGM.getCodeGenOpts().MergeAllConstants && |
| 628 | Ty.isConstant(getContext())) { |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 629 | EmitStaticVarDecl(D, llvm::GlobalValue::InternalLinkage); |
Tanya Lattner | 59876c2 | 2009-11-04 01:18:09 +0000 | [diff] [blame] | 630 | return; |
| 631 | } |
Chris Lattner | 761acc1 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 632 | |
| 633 | IsSimpleConstantInitializer = true; |
Tanya Lattner | 59876c2 | 2009-11-04 01:18:09 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Douglas Gregor | d86c477 | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 636 | // A normal fixed sized variable becomes an alloca in the entry block, |
| 637 | // unless it's an NRVO variable. |
Eli Friedman | a3460ac | 2009-03-04 04:25:14 +0000 | [diff] [blame] | 638 | const llvm::Type *LTy = ConvertTypeForMem(Ty); |
Douglas Gregor | d86c477 | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 639 | |
| 640 | if (NRVO) { |
| 641 | // The named return value optimization: allocate this variable in the |
| 642 | // return slot, so that we can elide the copy when returning this |
| 643 | // variable (C++0x [class.copy]p34). |
| 644 | DeclPtr = ReturnValue; |
Douglas Gregor | 3d91bbc | 2010-05-17 15:52:46 +0000 | [diff] [blame] | 645 | |
| 646 | if (const RecordType *RecordTy = Ty->getAs<RecordType>()) { |
| 647 | if (!cast<CXXRecordDecl>(RecordTy->getDecl())->hasTrivialDestructor()) { |
| 648 | // Create a flag that is used to indicate when the NRVO was applied |
| 649 | // to this variable. Set it to zero to indicate that NRVO was not |
| 650 | // applied. |
Chris Lattner | 4c53dc1 | 2010-12-01 01:47:15 +0000 | [diff] [blame] | 651 | llvm::Value *Zero = Builder.getFalse(); |
| 652 | NRVOFlag = CreateTempAlloca(Zero->getType(), "nrvo"); |
Douglas Gregor | 3d91bbc | 2010-05-17 15:52:46 +0000 | [diff] [blame] | 653 | Builder.CreateStore(Zero, NRVOFlag); |
| 654 | |
| 655 | // Record the NRVO flag for this variable. |
| 656 | NRVOFlags[&D] = NRVOFlag; |
| 657 | } |
| 658 | } |
Douglas Gregor | d86c477 | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 659 | } else { |
| 660 | if (isByRef) |
| 661 | LTy = BuildByRefType(&D); |
| 662 | |
| 663 | llvm::AllocaInst *Alloc = CreateTempAlloca(LTy); |
| 664 | Alloc->setName(D.getNameAsString()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 665 | |
Douglas Gregor | d86c477 | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 666 | Align = getContext().getDeclAlign(&D); |
| 667 | if (isByRef) |
| 668 | Align = std::max(Align, |
Ken Dyck | 06f486e | 2011-01-18 02:01:14 +0000 | [diff] [blame] | 669 | getContext().toCharUnitsFromBits(Target.getPointerAlign(0))); |
Douglas Gregor | d86c477 | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 670 | Alloc->setAlignment(Align.getQuantity()); |
| 671 | DeclPtr = Alloc; |
| 672 | } |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 673 | } else { |
| 674 | // Targets that don't support recursion emit locals as globals. |
| 675 | const char *Class = |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 676 | D.getStorageClass() == SC_Register ? ".reg." : ".auto."; |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 677 | DeclPtr = CreateStaticVarDecl(D, Class, |
| 678 | llvm::GlobalValue::InternalLinkage); |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 679 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 680 | |
Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 681 | // FIXME: Can this happen? |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 682 | if (Ty->isVariablyModifiedType()) |
| 683 | EmitVLASize(Ty); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 684 | } else { |
Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 685 | EnsureInsertPoint(); |
| 686 | |
Anders Carlsson | 5ecb1b9 | 2009-02-09 20:41:50 +0000 | [diff] [blame] | 687 | if (!DidCallStackSave) { |
Anders Carlsson | 5d46315 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 688 | // Save the stack. |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 689 | const llvm::Type *LTy = llvm::Type::getInt8PtrTy(VMContext); |
Anders Carlsson | 5d46315 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 690 | llvm::Value *Stack = CreateTempAlloca(LTy, "saved_stack"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 691 | |
Anders Carlsson | 5d46315 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 692 | llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave); |
| 693 | llvm::Value *V = Builder.CreateCall(F); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 694 | |
Anders Carlsson | 5d46315 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 695 | Builder.CreateStore(V, Stack); |
Anders Carlsson | 5ecb1b9 | 2009-02-09 20:41:50 +0000 | [diff] [blame] | 696 | |
| 697 | DidCallStackSave = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 698 | |
John McCall | d871509 | 2010-07-21 06:13:08 +0000 | [diff] [blame] | 699 | // Push a cleanup block and restore the stack there. |
John McCall | 3ad32c8 | 2011-01-28 08:37:24 +0000 | [diff] [blame] | 700 | // FIXME: in general circumstances, this should be an EH cleanup. |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 701 | EHStack.pushCleanup<CallStackRestore>(NormalCleanup, Stack); |
Anders Carlsson | 5d46315 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 702 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 703 | |
Anders Carlsson | 5d46315 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 704 | // Get the element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 705 | const llvm::Type *LElemTy = ConvertTypeForMem(Ty); |
Chris Lattner | 1b72677 | 2010-12-02 07:07:26 +0000 | [diff] [blame] | 706 | const llvm::Type *LElemPtrTy = LElemTy->getPointerTo(Ty.getAddressSpace()); |
Anders Carlsson | 5d46315 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 707 | |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 708 | llvm::Value *VLASize = EmitVLASize(Ty); |
Anders Carlsson | 5d46315 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 709 | |
| 710 | // Allocate memory for the array. |
Anders Carlsson | 41f8a13 | 2009-09-26 18:16:06 +0000 | [diff] [blame] | 711 | llvm::AllocaInst *VLA = |
| 712 | Builder.CreateAlloca(llvm::Type::getInt8Ty(VMContext), VLASize, "vla"); |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 713 | VLA->setAlignment(getContext().getDeclAlign(&D).getQuantity()); |
Anders Carlsson | 41f8a13 | 2009-09-26 18:16:06 +0000 | [diff] [blame] | 714 | |
Eli Friedman | 8f39f5e | 2008-12-20 23:11:59 +0000 | [diff] [blame] | 715 | DeclPtr = Builder.CreateBitCast(VLA, LElemPtrTy, "tmp"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 716 | } |
Eli Friedman | 8f39f5e | 2008-12-20 23:11:59 +0000 | [diff] [blame] | 717 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 718 | llvm::Value *&DMEntry = LocalDeclMap[&D]; |
| 719 | assert(DMEntry == 0 && "Decl already exists in localdeclmap!"); |
| 720 | DMEntry = DeclPtr; |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 721 | |
| 722 | // Emit debug info for local var declaration. |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 723 | if (CGDebugInfo *DI = getDebugInfo()) { |
Daniel Dunbar | 25b6ebf | 2009-07-19 07:03:11 +0000 | [diff] [blame] | 724 | assert(HaveInsertPoint() && "Unexpected unreachable point!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 725 | |
Daniel Dunbar | 66031a5 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 726 | DI->setLocation(D.getLocation()); |
Sanjiv Gupta | 4381d4b | 2009-05-22 13:54:25 +0000 | [diff] [blame] | 727 | if (Target.useGlobalsForAutomaticVariables()) { |
| 728 | DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(DeclPtr), &D); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 729 | } else |
| 730 | DI->EmitDeclareOfAutoVariable(&D, DeclPtr, Builder); |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 731 | } |
| 732 | |
Chris Lattner | 1978596 | 2007-07-12 00:39:48 +0000 | [diff] [blame] | 733 | // If this local has an initializer, emit it now. |
Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 734 | const Expr *Init = D.getInit(); |
| 735 | |
| 736 | // If we are at an unreachable point, we don't need to emit the initializer |
| 737 | // unless it contains a label. |
| 738 | if (!HaveInsertPoint()) { |
| 739 | if (!ContainsLabel(Init)) |
| 740 | Init = 0; |
| 741 | else |
| 742 | EnsureInsertPoint(); |
| 743 | } |
| 744 | |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 745 | if (isByRef) { |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 746 | const llvm::PointerType *PtrToInt8Ty = llvm::Type::getInt8PtrTy(VMContext); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 747 | |
Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 748 | EnsureInsertPoint(); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 749 | llvm::Value *isa_field = Builder.CreateStructGEP(DeclPtr, 0); |
| 750 | llvm::Value *forwarding_field = Builder.CreateStructGEP(DeclPtr, 1); |
| 751 | llvm::Value *flags_field = Builder.CreateStructGEP(DeclPtr, 2); |
| 752 | llvm::Value *size_field = Builder.CreateStructGEP(DeclPtr, 3); |
| 753 | llvm::Value *V; |
| 754 | int flag = 0; |
| 755 | int flags = 0; |
| 756 | |
Mike Stump | f4bc312 | 2009-03-07 06:04:31 +0000 | [diff] [blame] | 757 | needsDispose = true; |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 758 | |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 759 | if (Ty->isBlockPointerType()) { |
| 760 | flag |= BLOCK_FIELD_IS_BLOCK; |
| 761 | flags |= BLOCK_HAS_COPY_DISPOSE; |
Fariborz Jahanian | e38be61 | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 762 | } else if (getContext().isObjCNSObjectType(Ty) || |
| 763 | Ty->isObjCObjectPointerType()) { |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 764 | flag |= BLOCK_FIELD_IS_OBJECT; |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 765 | flags |= BLOCK_HAS_COPY_DISPOSE; |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 766 | } else if (getContext().getBlockVarCopyInits(&D)) { |
| 767 | flag |= BLOCK_HAS_CXX_OBJ; |
| 768 | flags |= BLOCK_HAS_COPY_DISPOSE; |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 769 | } |
Mike Stump | f4bc312 | 2009-03-07 06:04:31 +0000 | [diff] [blame] | 770 | |
| 771 | // FIXME: Someone double check this. |
| 772 | if (Ty.isObjCGCWeak()) |
| 773 | flag |= BLOCK_FIELD_IS_WEAK; |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 774 | |
| 775 | int isa = 0; |
Chris Lattner | 94cd011 | 2010-12-01 02:05:19 +0000 | [diff] [blame] | 776 | if (flag & BLOCK_FIELD_IS_WEAK) |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 777 | isa = 1; |
Chris Lattner | 4c53dc1 | 2010-12-01 01:47:15 +0000 | [diff] [blame] | 778 | V = Builder.CreateIntToPtr(Builder.getInt32(isa), PtrToInt8Ty, "isa"); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 779 | Builder.CreateStore(V, isa_field); |
| 780 | |
Anders Carlsson | 3604386 | 2009-09-10 01:32:12 +0000 | [diff] [blame] | 781 | Builder.CreateStore(DeclPtr, forwarding_field); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 782 | |
Chris Lattner | 4c53dc1 | 2010-12-01 01:47:15 +0000 | [diff] [blame] | 783 | Builder.CreateStore(Builder.getInt32(flags), flags_field); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 784 | |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 785 | const llvm::Type *V1; |
| 786 | V1 = cast<llvm::PointerType>(DeclPtr->getType())->getElementType(); |
Chris Lattner | 4c53dc1 | 2010-12-01 01:47:15 +0000 | [diff] [blame] | 787 | V = Builder.getInt32(CGM.GetTargetTypeStoreSize(V1).getQuantity()); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 788 | Builder.CreateStore(V, size_field); |
| 789 | |
| 790 | if (flags & BLOCK_HAS_COPY_DISPOSE) { |
| 791 | llvm::Value *copy_helper = Builder.CreateStructGEP(DeclPtr, 4); |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 792 | Builder.CreateStore(BuildbyrefCopyHelper(DeclPtr->getType(), flag, |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 793 | Align.getQuantity(), &D), |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 794 | copy_helper); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 795 | |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 796 | llvm::Value *destroy_helper = Builder.CreateStructGEP(DeclPtr, 5); |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 797 | Builder.CreateStore(BuildbyrefDestroyHelper(DeclPtr->getType(), flag, |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 798 | Align.getQuantity(), &D), |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 799 | destroy_helper); |
Chris Lattner | 9a19edf | 2007-08-26 05:13:54 +0000 | [diff] [blame] | 800 | } |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 801 | } |
Anders Carlsson | 69c68b7 | 2009-02-07 23:51:38 +0000 | [diff] [blame] | 802 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 803 | if (SpecialInit) { |
| 804 | SpecialInit(*this, D, DeclPtr); |
| 805 | } else if (Init) { |
Fariborz Jahanian | 20e1c7e | 2010-03-12 21:40:43 +0000 | [diff] [blame] | 806 | llvm::Value *Loc = DeclPtr; |
Fariborz Jahanian | 20e1c7e | 2010-03-12 21:40:43 +0000 | [diff] [blame] | 807 | |
Daniel Dunbar | 4cac2a1 | 2010-08-21 02:17:08 +0000 | [diff] [blame] | 808 | bool isVolatile = getContext().getCanonicalType(Ty).isVolatileQualified(); |
Fariborz Jahanian | 20e1c7e | 2010-03-12 21:40:43 +0000 | [diff] [blame] | 809 | |
| 810 | // If the initializer was a simple constant initializer, we can optimize it |
| 811 | // in various ways. |
| 812 | if (IsSimpleConstantInitializer) { |
Daniel Dunbar | 4cac2a1 | 2010-08-21 02:17:08 +0000 | [diff] [blame] | 813 | llvm::Constant *Init = CGM.EmitConstantExpr(D.getInit(), Ty,this); |
Fariborz Jahanian | 20e1c7e | 2010-03-12 21:40:43 +0000 | [diff] [blame] | 814 | assert(Init != 0 && "Wasn't a simple constant init?"); |
| 815 | |
Fariborz Jahanian | 20e1c7e | 2010-03-12 21:40:43 +0000 | [diff] [blame] | 816 | llvm::Value *SizeVal = |
Chris Lattner | 4c53dc1 | 2010-12-01 01:47:15 +0000 | [diff] [blame] | 817 | llvm::ConstantInt::get(CGF.IntPtrTy, |
Fariborz Jahanian | 20e1c7e | 2010-03-12 21:40:43 +0000 | [diff] [blame] | 818 | getContext().getTypeSizeInChars(Ty).getQuantity()); |
| 819 | |
Benjamin Kramer | 9f0c7cc | 2010-12-30 00:13:21 +0000 | [diff] [blame] | 820 | const llvm::Type *BP = Builder.getInt8PtrTy(); |
Chris Lattner | c0f31fd | 2010-12-02 04:27:29 +0000 | [diff] [blame] | 821 | if (Loc->getType() != BP) |
| 822 | Loc = Builder.CreateBitCast(Loc, BP, "tmp"); |
Mon P Wang | 3ecd785 | 2010-04-04 03:10:52 +0000 | [diff] [blame] | 823 | |
Chris Lattner | 94cd011 | 2010-12-01 02:05:19 +0000 | [diff] [blame] | 824 | // If the initializer is all or mostly zeros, codegen with memset then do |
| 825 | // a few stores afterward. |
| 826 | if (shouldUseMemSetPlusStoresToInitialize(Init, |
| 827 | CGM.getTargetData().getTypeAllocSize(Init->getType()))) { |
Benjamin Kramer | 9f0c7cc | 2010-12-30 00:13:21 +0000 | [diff] [blame] | 828 | Builder.CreateMemSet(Loc, Builder.getInt8(0), SizeVal, |
| 829 | Align.getQuantity(), false); |
Chris Lattner | c0f31fd | 2010-12-02 04:27:29 +0000 | [diff] [blame] | 830 | if (!Init->isNullValue()) { |
| 831 | Loc = Builder.CreateBitCast(Loc, Init->getType()->getPointerTo()); |
| 832 | emitStoresForInitAfterMemset(Init, Loc, Builder); |
| 833 | } |
Chris Lattner | 94cd011 | 2010-12-01 02:05:19 +0000 | [diff] [blame] | 834 | |
Fariborz Jahanian | 20e1c7e | 2010-03-12 21:40:43 +0000 | [diff] [blame] | 835 | } else { |
| 836 | // Otherwise, create a temporary global with the initializer then |
| 837 | // memcpy from the global to the alloca. |
| 838 | std::string Name = GetStaticDeclName(*this, D, "."); |
| 839 | llvm::GlobalVariable *GV = |
| 840 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), true, |
| 841 | llvm::GlobalValue::InternalLinkage, |
| 842 | Init, Name, 0, false, 0); |
| 843 | GV->setAlignment(Align.getQuantity()); |
| 844 | |
| 845 | llvm::Value *SrcPtr = GV; |
| 846 | if (SrcPtr->getType() != BP) |
| 847 | SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp"); |
Mon P Wang | 3ecd785 | 2010-04-04 03:10:52 +0000 | [diff] [blame] | 848 | |
Benjamin Kramer | 9f0c7cc | 2010-12-30 00:13:21 +0000 | [diff] [blame] | 849 | Builder.CreateMemCpy(Loc, SrcPtr, SizeVal, Align.getQuantity(), false); |
Fariborz Jahanian | 20e1c7e | 2010-03-12 21:40:43 +0000 | [diff] [blame] | 850 | } |
| 851 | } else if (Ty->isReferenceType()) { |
Anders Carlsson | 32f36ba | 2010-06-26 16:35:32 +0000 | [diff] [blame] | 852 | RValue RV = EmitReferenceBindingToExpr(Init, &D); |
Fariborz Jahanian | 52a80e1 | 2011-01-26 23:08:27 +0000 | [diff] [blame] | 853 | if (isByRef) |
| 854 | Loc = Builder.CreateStructGEP(DeclPtr, getByRefValueLLVMField(&D), |
| 855 | D.getNameAsString()); |
Daniel Dunbar | 91a16fa | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 856 | EmitStoreOfScalar(RV.getScalarVal(), Loc, false, Alignment, Ty); |
Fariborz Jahanian | 20e1c7e | 2010-03-12 21:40:43 +0000 | [diff] [blame] | 857 | } else if (!hasAggregateLLVMType(Init->getType())) { |
| 858 | llvm::Value *V = EmitScalarExpr(Init); |
Fariborz Jahanian | 52a80e1 | 2011-01-26 23:08:27 +0000 | [diff] [blame] | 859 | if (isByRef) { |
| 860 | // When RHS has side-effect, must go through "forwarding' field |
| 861 | // to get to the address of the __block variable descriptor. |
| 862 | if (Init->HasSideEffects(getContext())) |
| 863 | Loc = BuildBlockByrefAddress(DeclPtr, &D); |
| 864 | else |
| 865 | Loc = Builder.CreateStructGEP(DeclPtr, getByRefValueLLVMField(&D), |
| 866 | D.getNameAsString()); |
| 867 | } |
Daniel Dunbar | 91a16fa | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 868 | EmitStoreOfScalar(V, Loc, isVolatile, Alignment, Ty); |
Fariborz Jahanian | 20e1c7e | 2010-03-12 21:40:43 +0000 | [diff] [blame] | 869 | } else if (Init->getType()->isAnyComplexType()) { |
Fariborz Jahanian | 52a80e1 | 2011-01-26 23:08:27 +0000 | [diff] [blame] | 870 | if (isByRef) |
| 871 | Loc = Builder.CreateStructGEP(DeclPtr, getByRefValueLLVMField(&D), |
| 872 | D.getNameAsString()); |
Fariborz Jahanian | 20e1c7e | 2010-03-12 21:40:43 +0000 | [diff] [blame] | 873 | EmitComplexExprIntoAddr(Init, Loc, isVolatile); |
| 874 | } else { |
Fariborz Jahanian | 52a80e1 | 2011-01-26 23:08:27 +0000 | [diff] [blame] | 875 | if (isByRef) |
| 876 | Loc = Builder.CreateStructGEP(DeclPtr, getByRefValueLLVMField(&D), |
| 877 | D.getNameAsString()); |
Chris Lattner | 1b72677 | 2010-12-02 07:07:26 +0000 | [diff] [blame] | 878 | EmitAggExpr(Init, AggValueSlot::forAddr(Loc, isVolatile, true, false)); |
Fariborz Jahanian | 20e1c7e | 2010-03-12 21:40:43 +0000 | [diff] [blame] | 879 | } |
| 880 | } |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 881 | |
Fariborz Jahanian | 6ca0b32 | 2009-08-03 20:20:07 +0000 | [diff] [blame] | 882 | // Handle CXX destruction of variables. |
Fariborz Jahanian | 667f36a | 2009-08-03 20:51:29 +0000 | [diff] [blame] | 883 | QualType DtorTy(Ty); |
Douglas Gregor | 89c49f0 | 2009-11-09 22:08:55 +0000 | [diff] [blame] | 884 | while (const ArrayType *Array = getContext().getAsArrayType(DtorTy)) |
Fariborz Jahanian | 6fba746 | 2009-10-29 16:22:54 +0000 | [diff] [blame] | 885 | DtorTy = getContext().getBaseElementType(Array); |
Fariborz Jahanian | 667f36a | 2009-08-03 20:51:29 +0000 | [diff] [blame] | 886 | if (const RecordType *RT = DtorTy->getAs<RecordType>()) |
Douglas Gregor | d86c477 | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 887 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Douglas Gregor | b5b30b9 | 2010-05-15 16:39:56 +0000 | [diff] [blame] | 888 | if (!ClassDecl->hasTrivialDestructor()) { |
Douglas Gregor | 3d91bbc | 2010-05-17 15:52:46 +0000 | [diff] [blame] | 889 | // Note: We suppress the destructor call when the corresponding NRVO |
| 890 | // flag has been set. |
Douglas Gregor | d86c477 | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 891 | llvm::Value *Loc = DeclPtr; |
| 892 | if (isByRef) |
| 893 | Loc = Builder.CreateStructGEP(DeclPtr, getByRefValueLLVMField(&D), |
| 894 | D.getNameAsString()); |
| 895 | |
Douglas Gregor | 1d110e0 | 2010-07-01 14:13:13 +0000 | [diff] [blame] | 896 | const CXXDestructorDecl *D = ClassDecl->getDestructor(); |
Fariborz Jahanian | 6ca0b32 | 2009-08-03 20:20:07 +0000 | [diff] [blame] | 897 | assert(D && "EmitLocalBlockVarDecl - destructor is nul"); |
Fariborz Jahanian | 6fba746 | 2009-10-29 16:22:54 +0000 | [diff] [blame] | 898 | |
Fariborz Jahanian | 6fba746 | 2009-10-29 16:22:54 +0000 | [diff] [blame] | 899 | if (const ConstantArrayType *Array = |
| 900 | getContext().getAsConstantArrayType(Ty)) { |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 901 | EHStack.pushCleanup<CallArrayDtor>(NormalAndEHCleanup, |
| 902 | D, Array, Loc); |
Fariborz Jahanian | 7799621 | 2009-11-04 17:57:40 +0000 | [diff] [blame] | 903 | } else { |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 904 | EHStack.pushCleanup<CallVarDtor>(NormalAndEHCleanup, |
| 905 | D, NRVOFlag, Loc); |
Fariborz Jahanian | 7799621 | 2009-11-04 17:57:40 +0000 | [diff] [blame] | 906 | } |
Fariborz Jahanian | 6ca0b32 | 2009-08-03 20:20:07 +0000 | [diff] [blame] | 907 | } |
| 908 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 909 | |
Anders Carlsson | 69c68b7 | 2009-02-07 23:51:38 +0000 | [diff] [blame] | 910 | // Handle the cleanup attribute |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 911 | if (const CleanupAttr *CA = D.getAttr<CleanupAttr>()) { |
Anders Carlsson | 69c68b7 | 2009-02-07 23:51:38 +0000 | [diff] [blame] | 912 | const FunctionDecl *FD = CA->getFunctionDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 913 | |
Anders Carlsson | 555b4bb | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 914 | llvm::Constant* F = CGM.GetAddrOfFunction(FD); |
Anders Carlsson | 69c68b7 | 2009-02-07 23:51:38 +0000 | [diff] [blame] | 915 | assert(F && "Could not find function!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 916 | |
Anders Carlsson | cabec03 | 2009-04-26 00:34:20 +0000 | [diff] [blame] | 917 | const CGFunctionInfo &Info = CGM.getTypes().getFunctionInfo(FD); |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 918 | EHStack.pushCleanup<CallCleanupFunction>(NormalAndEHCleanup, |
| 919 | F, &Info, DeclPtr, &D); |
Anders Carlsson | 69c68b7 | 2009-02-07 23:51:38 +0000 | [diff] [blame] | 920 | } |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 921 | |
John McCall | db2cfec | 2010-07-22 21:25:44 +0000 | [diff] [blame] | 922 | // If this is a block variable, clean it up. |
John McCall | d871509 | 2010-07-21 06:13:08 +0000 | [diff] [blame] | 923 | if (needsDispose && CGM.getLangOptions().getGCMode() != LangOptions::GCOnly) |
John McCall | 34fdee3 | 2010-10-06 18:56:43 +0000 | [diff] [blame] | 924 | EHStack.pushCleanup<CallBlockRelease>(NormalAndEHCleanup, DeclPtr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 925 | } |
| 926 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 927 | /// Emit an alloca (or GlobalValue depending on target) |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 928 | /// for the specified parameter and set up LocalDeclMap. |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 929 | void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg) { |
| 930 | // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl? |
Sanjiv Gupta | 31fc07d | 2008-10-31 09:52:39 +0000 | [diff] [blame] | 931 | assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) && |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 932 | "Invalid argument to EmitParmDecl"); |
Chris Lattner | 8bcfc5b | 2008-04-06 23:10:54 +0000 | [diff] [blame] | 933 | QualType Ty = D.getType(); |
Mike Stump | df317bf | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 934 | CanQualType CTy = getContext().getCanonicalType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 935 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 936 | llvm::Value *DeclPtr; |
Daniel Dunbar | e86bcf0 | 2010-02-08 22:53:07 +0000 | [diff] [blame] | 937 | // If this is an aggregate or variable sized value, reuse the input pointer. |
| 938 | if (!Ty->isConstantSizeType() || |
| 939 | CodeGenFunction::hasAggregateLLVMType(Ty)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 940 | DeclPtr = Arg; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 941 | } else { |
Daniel Dunbar | e86bcf0 | 2010-02-08 22:53:07 +0000 | [diff] [blame] | 942 | // Otherwise, create a temporary to hold the value. |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 943 | DeclPtr = CreateMemTemp(Ty, D.getName() + ".addr"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 944 | |
Daniel Dunbar | e86bcf0 | 2010-02-08 22:53:07 +0000 | [diff] [blame] | 945 | // Store the initial value into the alloca. |
Daniel Dunbar | 91a16fa | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 946 | unsigned Alignment = getContext().getDeclAlign(&D).getQuantity(); |
| 947 | EmitStoreOfScalar(Arg, DeclPtr, CTy.isVolatileQualified(), Alignment, Ty); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 948 | } |
Daniel Dunbar | e86bcf0 | 2010-02-08 22:53:07 +0000 | [diff] [blame] | 949 | Arg->setName(D.getName()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 950 | |
| 951 | llvm::Value *&DMEntry = LocalDeclMap[&D]; |
| 952 | assert(DMEntry == 0 && "Decl already exists in localdeclmap!"); |
| 953 | DMEntry = DeclPtr; |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 954 | |
| 955 | // Emit debug info for param declaration. |
Devang Patel | fee53aa | 2009-10-09 22:06:15 +0000 | [diff] [blame] | 956 | if (CGDebugInfo *DI = getDebugInfo()) { |
| 957 | DI->setLocation(D.getLocation()); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 958 | DI->EmitDeclareOfArgVariable(&D, DeclPtr, Builder); |
Devang Patel | fee53aa | 2009-10-09 22:06:15 +0000 | [diff] [blame] | 959 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 960 | } |