Chris Lattner | 84915fa | 2007-06-02 04:16:21 +0000 | [diff] [blame] | 1 | //===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 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. |
Chris Lattner | 84915fa | 2007-06-02 04:16:21 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code to emit Decl nodes as LLVM code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Sanjiv Gupta | 18de624 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 14 | #include "CGDebugInfo.h" |
Chris Lattner | 84915fa | 2007-06-02 04:16:21 +0000 | [diff] [blame] | 15 | #include "CodeGenFunction.h" |
Anders Carlsson | f94cd1f | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 16 | #include "CodeGenModule.h" |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 17 | #include "CGBlocks.h" |
Daniel Dunbar | ad319a7 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 19 | #include "clang/AST/CharUnits.h" |
Daniel Dunbar | 6e8aa53 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 20 | #include "clang/AST/Decl.h" |
Anders Carlsson | 4c03d98 | 2008-08-25 01:38:19 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclObjC.h" |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 22 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | b781dc79 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 23 | #include "clang/Basic/TargetInfo.h" |
Chandler Carruth | 8509824 | 2010-06-15 23:19:56 +0000 | [diff] [blame] | 24 | #include "clang/Frontend/CodeGenOptions.h" |
Anders Carlsson | f94cd1f | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 25 | #include "llvm/GlobalVariable.h" |
Anders Carlsson | 3003288 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 26 | #include "llvm/Intrinsics.h" |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 53621a5 | 2007-06-13 20:44:40 +0000 | [diff] [blame] | 28 | #include "llvm/Type.h" |
Chris Lattner | 84915fa | 2007-06-02 04:16:21 +0000 | [diff] [blame] | 29 | using namespace clang; |
| 30 | using namespace CodeGen; |
| 31 | |
| 32 | |
Chris Lattner | 1ad38f8 | 2007-06-09 01:20:56 +0000 | [diff] [blame] | 33 | void CodeGenFunction::EmitDecl(const Decl &D) { |
Chris Lattner | 1ad38f8 | 2007-06-09 01:20:56 +0000 | [diff] [blame] | 34 | switch (D.getKind()) { |
Douglas Gregor | 19043f0 | 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: |
Alexis Hunt | ed05325 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 42 | case Decl::NonTypeTemplateParm: |
Douglas Gregor | 19043f0 | 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 | df946c3 | 2010-11-21 06:49:41 +0000 | [diff] [blame] | 48 | case Decl::IndirectField: |
Douglas Gregor | 19043f0 | 2010-04-23 02:02:43 +0000 | [diff] [blame] | 49 | case Decl::ObjCIvar: |
| 50 | case Decl::ObjCAtDefsField: |
Chris Lattner | ba9dddb | 2007-10-08 21:37:32 +0000 | [diff] [blame] | 51 | case Decl::ParmVar: |
Douglas Gregor | 19043f0 | 2010-04-23 02:02:43 +0000 | [diff] [blame] | 52 | case Decl::ImplicitParam: |
| 53 | case Decl::ClassTemplate: |
| 54 | case Decl::FunctionTemplate: |
| 55 | case Decl::TemplateTemplateParm: |
| 56 | case Decl::ObjCMethod: |
| 57 | case Decl::ObjCCategory: |
| 58 | case Decl::ObjCProtocol: |
| 59 | case Decl::ObjCInterface: |
| 60 | case Decl::ObjCCategoryImpl: |
| 61 | case Decl::ObjCImplementation: |
| 62 | case Decl::ObjCProperty: |
| 63 | case Decl::ObjCCompatibleAlias: |
Abramo Bagnara | d734058 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 64 | case Decl::AccessSpec: |
Douglas Gregor | 19043f0 | 2010-04-23 02:02:43 +0000 | [diff] [blame] | 65 | case Decl::LinkageSpec: |
| 66 | case Decl::ObjCPropertyImpl: |
| 67 | case Decl::ObjCClass: |
| 68 | case Decl::ObjCForwardProtocol: |
| 69 | case Decl::FileScopeAsm: |
| 70 | case Decl::Friend: |
| 71 | case Decl::FriendTemplate: |
| 72 | case Decl::Block: |
Douglas Gregor | 19043f0 | 2010-04-23 02:02:43 +0000 | [diff] [blame] | 73 | assert(0 && "Declaration not should not be in declstmts!"); |
Chris Lattner | 84915fa | 2007-06-02 04:16:21 +0000 | [diff] [blame] | 74 | case Decl::Function: // void X(); |
Argyrios Kyrtzidis | 88e1b97 | 2008-10-15 00:42:39 +0000 | [diff] [blame] | 75 | case Decl::Record: // struct/union/class X; |
Chris Lattner | 84915fa | 2007-06-02 04:16:21 +0000 | [diff] [blame] | 76 | case Decl::Enum: // enum X; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 77 | case Decl::EnumConstant: // enum ? { X = ? } |
Argyrios Kyrtzidis | 88e1b97 | 2008-10-15 00:42:39 +0000 | [diff] [blame] | 78 | case Decl::CXXRecord: // struct/union/class X; [C++] |
Daniel Dunbar | 785406b | 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 | 19043f0 | 2010-04-23 02:02:43 +0000 | [diff] [blame] | 82 | case Decl::NamespaceAlias: |
Anders Carlsson | ce2cd01 | 2009-12-03 17:26:31 +0000 | [diff] [blame] | 83 | case Decl::StaticAssert: // static_assert(X, ""); [C++0x] |
Chris Lattner | 43e7f31 | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 84 | case Decl::Label: // __label__ x; |
Chris Lattner | 84915fa | 2007-06-02 04:16:21 +0000 | [diff] [blame] | 85 | // None of these decls require codegen support. |
| 86 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | |
Daniel Dunbar | a799807 | 2008-08-29 17:28:43 +0000 | [diff] [blame] | 88 | case Decl::Var: { |
| 89 | const VarDecl &VD = cast<VarDecl>(D); |
John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 90 | assert(VD.isLocalVarDecl() && |
Daniel Dunbar | a799807 | 2008-08-29 17:28:43 +0000 | [diff] [blame] | 91 | "Should not see file-scope variables inside a function!"); |
John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 92 | return EmitVarDecl(VD); |
Chris Lattner | 84915fa | 2007-06-02 04:16:21 +0000 | [diff] [blame] | 93 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 94 | |
Anders Carlsson | 5d985f5 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 95 | case Decl::Typedef: { // typedef int X; |
| 96 | const TypedefDecl &TD = cast<TypedefDecl>(D); |
| 97 | QualType Ty = TD.getUnderlyingType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 98 | |
Anders Carlsson | 5d985f5 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 99 | if (Ty->isVariablyModifiedType()) |
| 100 | EmitVLASize(Ty); |
| 101 | } |
Daniel Dunbar | a799807 | 2008-08-29 17:28:43 +0000 | [diff] [blame] | 102 | } |
Chris Lattner | 84915fa | 2007-06-02 04:16:21 +0000 | [diff] [blame] | 103 | } |
Chris Lattner | 03df122 | 2007-06-02 04:53:11 +0000 | [diff] [blame] | 104 | |
John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 105 | /// EmitVarDecl - This method handles emission of any variable declaration |
Chris Lattner | 03df122 | 2007-06-02 04:53:11 +0000 | [diff] [blame] | 106 | /// inside a function, including static vars etc. |
John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 107 | void CodeGenFunction::EmitVarDecl(const VarDecl &D) { |
Chris Lattner | 03df122 | 2007-06-02 04:53:11 +0000 | [diff] [blame] | 108 | switch (D.getStorageClass()) { |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 109 | case SC_None: |
| 110 | case SC_Auto: |
| 111 | case SC_Register: |
John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 112 | return EmitAutoVarDecl(D); |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 113 | case SC_Static: { |
Anders Carlsson | cee2d2f | 2010-02-07 02:03:08 +0000 | [diff] [blame] | 114 | llvm::GlobalValue::LinkageTypes Linkage = |
| 115 | llvm::GlobalValue::InternalLinkage; |
| 116 | |
John McCall | 7cb0220 | 2010-05-25 04:30:21 +0000 | [diff] [blame] | 117 | // If the function definition has some sort of weak linkage, its |
| 118 | // static variables should also be weak so that they get properly |
| 119 | // uniqued. We can't do this in C, though, because there's no |
| 120 | // standard way to agree on which variables are the same (i.e. |
| 121 | // there's no mangling). |
| 122 | if (getContext().getLangOptions().CPlusPlus) |
| 123 | if (llvm::GlobalValue::isWeakForLinker(CurFn->getLinkage())) |
| 124 | Linkage = CurFn->getLinkage(); |
Anders Carlsson | cee2d2f | 2010-02-07 02:03:08 +0000 | [diff] [blame] | 125 | |
John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 126 | return EmitStaticVarDecl(D, Linkage); |
Anders Carlsson | cee2d2f | 2010-02-07 02:03:08 +0000 | [diff] [blame] | 127 | } |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 128 | case SC_Extern: |
| 129 | case SC_PrivateExtern: |
Lauro Ramos Venancio | bada8d4 | 2008-02-16 22:30:38 +0000 | [diff] [blame] | 130 | // Don't emit it now, allow it to be emitted lazily on its first use. |
| 131 | return; |
Chris Lattner | 03df122 | 2007-06-02 04:53:11 +0000 | [diff] [blame] | 132 | } |
Daniel Dunbar | 0ca1660 | 2009-04-14 02:25:56 +0000 | [diff] [blame] | 133 | |
| 134 | assert(0 && "Unknown storage class"); |
Chris Lattner | 03df122 | 2007-06-02 04:53:11 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Chris Lattner | e99c110 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 137 | static std::string GetStaticDeclName(CodeGenFunction &CGF, const VarDecl &D, |
| 138 | const char *Separator) { |
| 139 | CodeGenModule &CGM = CGF.CGM; |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 140 | if (CGF.getContext().getLangOptions().CPlusPlus) { |
Anders Carlsson | ea836bc | 2010-06-22 16:16:50 +0000 | [diff] [blame] | 141 | llvm::StringRef Name = CGM.getMangledName(&D); |
| 142 | return Name.str(); |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 143 | } |
Chris Lattner | e99c110 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 144 | |
| 145 | std::string ContextName; |
Fariborz Jahanian | 3a4ea9a | 2010-11-30 23:07:14 +0000 | [diff] [blame] | 146 | if (!CGF.CurFuncDecl) { |
| 147 | // Better be in a block declared in global scope. |
| 148 | const NamedDecl *ND = cast<NamedDecl>(&D); |
| 149 | const DeclContext *DC = ND->getDeclContext(); |
| 150 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) { |
| 151 | MangleBuffer Name; |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 152 | CGM.getBlockMangledName(GlobalDecl(), Name, BD); |
Fariborz Jahanian | 3a4ea9a | 2010-11-30 23:07:14 +0000 | [diff] [blame] | 153 | ContextName = Name.getString(); |
| 154 | } |
| 155 | else |
| 156 | assert(0 && "Unknown context for block static var decl"); |
| 157 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CGF.CurFuncDecl)) { |
Anders Carlsson | ea836bc | 2010-06-22 16:16:50 +0000 | [diff] [blame] | 158 | llvm::StringRef Name = CGM.getMangledName(FD); |
| 159 | ContextName = Name.str(); |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 160 | } else if (isa<ObjCMethodDecl>(CGF.CurFuncDecl)) |
Chris Lattner | e99c110 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 161 | ContextName = CGF.CurFn->getName(); |
| 162 | else |
Fariborz Jahanian | 3a4ea9a | 2010-11-30 23:07:14 +0000 | [diff] [blame] | 163 | assert(0 && "Unknown context for static var decl"); |
Chris Lattner | e99c110 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 164 | |
| 165 | return ContextName + Separator + D.getNameAsString(); |
| 166 | } |
| 167 | |
Daniel Dunbar | 22a87f9 | 2009-02-25 19:24:29 +0000 | [diff] [blame] | 168 | llvm::GlobalVariable * |
John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 169 | CodeGenFunction::CreateStaticVarDecl(const VarDecl &D, |
| 170 | const char *Separator, |
| 171 | llvm::GlobalValue::LinkageTypes Linkage) { |
Chris Lattner | fc4379f | 2008-04-06 23:10:54 +0000 | [diff] [blame] | 172 | QualType Ty = D.getType(); |
Eli Friedman | a682d39 | 2008-02-15 12:20:59 +0000 | [diff] [blame] | 173 | assert(Ty->isConstantSizeType() && "VLAs can't be static"); |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 174 | |
Chris Lattner | e99c110 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 175 | std::string Name = GetStaticDeclName(*this, D, Separator); |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 176 | |
Daniel Dunbar | 22a87f9 | 2009-02-25 19:24:29 +0000 | [diff] [blame] | 177 | const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty); |
Anders Carlsson | e33eed5 | 2009-09-26 18:16:06 +0000 | [diff] [blame] | 178 | llvm::GlobalVariable *GV = |
| 179 | new llvm::GlobalVariable(CGM.getModule(), LTy, |
| 180 | Ty.isConstant(getContext()), Linkage, |
| 181 | CGM.EmitNullConstant(D.getType()), Name, 0, |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame^] | 182 | D.isThreadSpecified(), |
| 183 | CGM.getContext().getTargetAddressSpace(Ty)); |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 184 | GV->setAlignment(getContext().getDeclAlign(&D).getQuantity()); |
John McCall | 8e7cb6d | 2010-11-02 21:04:24 +0000 | [diff] [blame] | 185 | if (Linkage != llvm::GlobalValue::InternalLinkage) |
| 186 | GV->setVisibility(CurFn->getVisibility()); |
Anders Carlsson | e33eed5 | 2009-09-26 18:16:06 +0000 | [diff] [blame] | 187 | return GV; |
Daniel Dunbar | 22a87f9 | 2009-02-25 19:24:29 +0000 | [diff] [blame] | 188 | } |
| 189 | |
John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 190 | /// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the |
Chris Lattner | e99c110 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 191 | /// global variable that has already been created for it. If the initializer |
| 192 | /// has a different type than GV does, this may free GV and return a different |
| 193 | /// one. Otherwise it just returns GV. |
| 194 | llvm::GlobalVariable * |
John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 195 | CodeGenFunction::AddInitializerToStaticVarDecl(const VarDecl &D, |
| 196 | llvm::GlobalVariable *GV) { |
Chris Lattner | e99c110 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 197 | llvm::Constant *Init = CGM.EmitConstantExpr(D.getInit(), D.getType(), this); |
John McCall | 70013b6 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 198 | |
Chris Lattner | e99c110 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 199 | // If constant emission failed, then this should be a C++ static |
| 200 | // initializer. |
| 201 | if (!Init) { |
| 202 | if (!getContext().getLangOptions().CPlusPlus) |
| 203 | CGM.ErrorUnsupported(D.getInit(), "constant l-value expression"); |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 204 | else if (Builder.GetInsertBlock()) { |
Anders Carlsson | 20bbbd4 | 2010-01-26 04:02:23 +0000 | [diff] [blame] | 205 | // Since we have a static initializer, this global variable can't |
| 206 | // be constant. |
| 207 | GV->setConstant(false); |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 208 | |
John McCall | cdf7ef5 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 209 | EmitCXXGuardedInit(D, GV); |
Anders Carlsson | 20bbbd4 | 2010-01-26 04:02:23 +0000 | [diff] [blame] | 210 | } |
Chris Lattner | e99c110 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 211 | return GV; |
| 212 | } |
John McCall | 70013b6 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 213 | |
Chris Lattner | e99c110 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 214 | // The initializer may differ in type from the global. Rewrite |
| 215 | // the global to match the initializer. (We have to do this |
| 216 | // because some types, like unions, can't be completely represented |
| 217 | // in the LLVM type system.) |
John McCall | 56f5758 | 2010-09-03 18:58:50 +0000 | [diff] [blame] | 218 | if (GV->getType()->getElementType() != Init->getType()) { |
Chris Lattner | e99c110 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 219 | llvm::GlobalVariable *OldGV = GV; |
| 220 | |
| 221 | GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), |
| 222 | OldGV->isConstant(), |
| 223 | OldGV->getLinkage(), Init, "", |
John McCall | 8e7cb6d | 2010-11-02 21:04:24 +0000 | [diff] [blame] | 224 | /*InsertBefore*/ OldGV, |
| 225 | D.isThreadSpecified(), |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame^] | 226 | CGM.getContext().getTargetAddressSpace(D.getType())); |
John McCall | 8e7cb6d | 2010-11-02 21:04:24 +0000 | [diff] [blame] | 227 | GV->setVisibility(OldGV->getVisibility()); |
Chris Lattner | e99c110 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 228 | |
| 229 | // Steal the name of the old global |
| 230 | GV->takeName(OldGV); |
| 231 | |
| 232 | // Replace all uses of the old global with the new global |
| 233 | llvm::Constant *NewPtrForOldDecl = |
| 234 | llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); |
| 235 | OldGV->replaceAllUsesWith(NewPtrForOldDecl); |
| 236 | |
| 237 | // Erase the old global, since it is no longer used. |
| 238 | OldGV->eraseFromParent(); |
| 239 | } |
| 240 | |
| 241 | GV->setInitializer(Init); |
| 242 | return GV; |
| 243 | } |
| 244 | |
John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 245 | void CodeGenFunction::EmitStaticVarDecl(const VarDecl &D, |
Anders Carlsson | cee2d2f | 2010-02-07 02:03:08 +0000 | [diff] [blame] | 246 | llvm::GlobalValue::LinkageTypes Linkage) { |
Daniel Dunbar | a374e60 | 2009-02-25 19:45:19 +0000 | [diff] [blame] | 247 | llvm::Value *&DMEntry = LocalDeclMap[&D]; |
| 248 | assert(DMEntry == 0 && "Decl already exists in localdeclmap!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 249 | |
John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 250 | llvm::GlobalVariable *GV = CreateStaticVarDecl(D, ".", Linkage); |
Daniel Dunbar | a374e60 | 2009-02-25 19:45:19 +0000 | [diff] [blame] | 251 | |
Daniel Dunbar | 1cdbc54 | 2009-02-25 20:08:33 +0000 | [diff] [blame] | 252 | // Store into LocalDeclMap before generating initializer to handle |
| 253 | // circular references. |
| 254 | DMEntry = GV; |
| 255 | |
John McCall | 4a39ab8 | 2010-05-04 20:45:42 +0000 | [diff] [blame] | 256 | // We can't have a VLA here, but we can have a pointer to a VLA, |
| 257 | // even though that doesn't really make any sense. |
Eli Friedman | bc633be | 2009-04-20 03:54:15 +0000 | [diff] [blame] | 258 | // Make sure to evaluate VLA bounds now so that we have them for later. |
| 259 | if (D.getType()->isVariablyModifiedType()) |
| 260 | EmitVLASize(D.getType()); |
Fariborz Jahanian | 366a948 | 2010-09-07 23:26:17 +0000 | [diff] [blame] | 261 | |
| 262 | // Local static block variables must be treated as globals as they may be |
| 263 | // referenced in their RHS initializer block-literal expresion. |
| 264 | CGM.setStaticLocalDeclAddress(&D, GV); |
Eli Friedman | bc633be | 2009-04-20 03:54:15 +0000 | [diff] [blame] | 265 | |
Chris Lattner | e99c110 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 266 | // If this value has an initializer, emit it. |
| 267 | if (D.getInit()) |
John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 268 | GV = AddInitializerToStaticVarDecl(D, GV); |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 269 | |
Chris Lattner | 4d94109 | 2010-03-10 23:59:59 +0000 | [diff] [blame] | 270 | GV->setAlignment(getContext().getDeclAlign(&D).getQuantity()); |
| 271 | |
Daniel Dunbar | 53bf741 | 2009-02-12 23:32:54 +0000 | [diff] [blame] | 272 | // FIXME: Merge attribute handling. |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 273 | if (const AnnotateAttr *AA = D.getAttr<AnnotateAttr>()) { |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 274 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 275 | llvm::Constant *Ann = |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 276 | CGM.EmitAnnotateAttr(GV, AA, |
Chris Lattner | 8a42586 | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 277 | SM.getInstantiationLineNumber(D.getLocation())); |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 278 | CGM.AddAnnotation(Ann); |
| 279 | } |
| 280 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 281 | if (const SectionAttr *SA = D.getAttr<SectionAttr>()) |
Daniel Dunbar | 53bf741 | 2009-02-12 23:32:54 +0000 | [diff] [blame] | 282 | GV->setSection(SA->getName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 283 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 284 | if (D.hasAttr<UsedAttr>()) |
Daniel Dunbar | 128a138 | 2009-02-13 22:08:43 +0000 | [diff] [blame] | 285 | CGM.AddUsedGlobal(GV); |
| 286 | |
Daniel Dunbar | 1cdbc54 | 2009-02-25 20:08:33 +0000 | [diff] [blame] | 287 | // We may have to cast the constant because of the initializer |
| 288 | // mismatch above. |
| 289 | // |
| 290 | // FIXME: It is really dangerous to store this in the map; if anyone |
| 291 | // RAUW's the GV uses of this constant will be invalid. |
Eli Friedman | c98a7ad | 2008-06-08 01:23:18 +0000 | [diff] [blame] | 292 | const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(D.getType()); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame^] | 293 | const llvm::Type *LPtrTy = |
| 294 | LTy->getPointerTo(CGM.getContext().getTargetAddressSpace(D.getType())); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 295 | DMEntry = llvm::ConstantExpr::getBitCast(GV, LPtrTy); |
Sanjiv Gupta | 158143a | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 296 | |
| 297 | // Emit global variable debug descriptor for static vars. |
Anders Carlsson | 63784f4 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 298 | CGDebugInfo *DI = getDebugInfo(); |
Mike Stump | f5a5c4f | 2009-02-20 00:19:45 +0000 | [diff] [blame] | 299 | if (DI) { |
Daniel Dunbar | b9fd902 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 300 | DI->setLocation(D.getLocation()); |
Sanjiv Gupta | 158143a | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 301 | DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(GV), &D); |
| 302 | } |
Anders Carlsson | f94cd1f | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 303 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 304 | |
Anders Carlsson | 0168f4b | 2009-09-12 02:14:24 +0000 | [diff] [blame] | 305 | unsigned CodeGenFunction::getByRefValueLLVMField(const ValueDecl *VD) const { |
| 306 | assert(ByRefValueInfo.count(VD) && "Did not find value!"); |
| 307 | |
| 308 | return ByRefValueInfo.find(VD)->second.second; |
| 309 | } |
| 310 | |
Fariborz Jahanian | 2f2fa72 | 2011-01-26 23:08:27 +0000 | [diff] [blame] | 311 | llvm::Value *CodeGenFunction::BuildBlockByrefAddress(llvm::Value *BaseAddr, |
| 312 | const VarDecl *V) { |
| 313 | llvm::Value *Loc = Builder.CreateStructGEP(BaseAddr, 1, "forwarding"); |
| 314 | Loc = Builder.CreateLoad(Loc); |
| 315 | Loc = Builder.CreateStructGEP(Loc, getByRefValueLLVMField(V), |
| 316 | V->getNameAsString()); |
| 317 | return Loc; |
| 318 | } |
| 319 | |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 320 | /// BuildByRefType - This routine changes a __block variable declared as T x |
| 321 | /// into: |
| 322 | /// |
| 323 | /// struct { |
| 324 | /// void *__isa; |
| 325 | /// void *__forwarding; |
| 326 | /// int32_t __flags; |
| 327 | /// int32_t __size; |
Mike Stump | 2114d7c | 2009-09-22 02:12:52 +0000 | [diff] [blame] | 328 | /// void *__copy_helper; // only if needed |
| 329 | /// void *__destroy_helper; // only if needed |
| 330 | /// char padding[X]; // only if needed |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 331 | /// T x; |
| 332 | /// } x |
| 333 | /// |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 334 | const llvm::Type *CodeGenFunction::BuildByRefType(const VarDecl *D) { |
Anders Carlsson | 0168f4b | 2009-09-12 02:14:24 +0000 | [diff] [blame] | 335 | std::pair<const llvm::Type *, unsigned> &Info = ByRefValueInfo[D]; |
| 336 | if (Info.first) |
| 337 | return Info.first; |
| 338 | |
Anders Carlsson | 71d1d92 | 2009-09-09 02:51:03 +0000 | [diff] [blame] | 339 | QualType Ty = D->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 340 | |
Anders Carlsson | f8e94f2 | 2009-09-12 02:44:18 +0000 | [diff] [blame] | 341 | std::vector<const llvm::Type *> Types; |
Anders Carlsson | 10f2c10 | 2009-09-10 01:32:12 +0000 | [diff] [blame] | 342 | |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 343 | llvm::PATypeHolder ByRefTypeHolder = llvm::OpaqueType::get(getLLVMContext()); |
Anders Carlsson | 10f2c10 | 2009-09-10 01:32:12 +0000 | [diff] [blame] | 344 | |
Anders Carlsson | f8e94f2 | 2009-09-12 02:44:18 +0000 | [diff] [blame] | 345 | // void *__isa; |
| 346 | Types.push_back(Int8PtrTy); |
| 347 | |
| 348 | // void *__forwarding; |
| 349 | Types.push_back(llvm::PointerType::getUnqual(ByRefTypeHolder)); |
| 350 | |
| 351 | // int32_t __flags; |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 352 | Types.push_back(Int32Ty); |
Anders Carlsson | f8e94f2 | 2009-09-12 02:44:18 +0000 | [diff] [blame] | 353 | |
| 354 | // int32_t __size; |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 355 | Types.push_back(Int32Ty); |
Anders Carlsson | f8e94f2 | 2009-09-12 02:44:18 +0000 | [diff] [blame] | 356 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 357 | bool HasCopyAndDispose = getContext().BlockRequiresCopying(Ty); |
Anders Carlsson | f8e94f2 | 2009-09-12 02:44:18 +0000 | [diff] [blame] | 358 | if (HasCopyAndDispose) { |
| 359 | /// void *__copy_helper; |
| 360 | Types.push_back(Int8PtrTy); |
| 361 | |
| 362 | /// void *__destroy_helper; |
| 363 | Types.push_back(Int8PtrTy); |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 364 | } |
Anders Carlsson | f8e94f2 | 2009-09-12 02:44:18 +0000 | [diff] [blame] | 365 | |
| 366 | bool Packed = false; |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 367 | CharUnits Align = getContext().getDeclAlign(D); |
Ken Dyck | 9a64869 | 2011-01-18 02:01:14 +0000 | [diff] [blame] | 368 | if (Align > getContext().toCharUnitsFromBits(Target.getPointerAlign(0))) { |
Anders Carlsson | f8e94f2 | 2009-09-12 02:44:18 +0000 | [diff] [blame] | 369 | // We have to insert padding. |
| 370 | |
| 371 | // The struct above has 2 32-bit integers. |
| 372 | unsigned CurrentOffsetInBytes = 4 * 2; |
| 373 | |
| 374 | // And either 2 or 4 pointers. |
| 375 | CurrentOffsetInBytes += (HasCopyAndDispose ? 4 : 2) * |
| 376 | CGM.getTargetData().getTypeAllocSize(Int8PtrTy); |
| 377 | |
| 378 | // Align the offset. |
| 379 | unsigned AlignedOffsetInBytes = |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 380 | llvm::RoundUpToAlignment(CurrentOffsetInBytes, Align.getQuantity()); |
Anders Carlsson | f8e94f2 | 2009-09-12 02:44:18 +0000 | [diff] [blame] | 381 | |
| 382 | unsigned NumPaddingBytes = AlignedOffsetInBytes - CurrentOffsetInBytes; |
Anders Carlsson | ccbabc9 | 2009-09-13 17:55:13 +0000 | [diff] [blame] | 383 | if (NumPaddingBytes > 0) { |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 384 | const llvm::Type *Ty = llvm::Type::getInt8Ty(getLLVMContext()); |
Mike Stump | 70197d5 | 2009-10-21 00:42:55 +0000 | [diff] [blame] | 385 | // FIXME: We need a sema error for alignment larger than the minimum of |
| 386 | // the maximal stack alignmint and the alignment of malloc on the system. |
Anders Carlsson | ccbabc9 | 2009-09-13 17:55:13 +0000 | [diff] [blame] | 387 | if (NumPaddingBytes > 1) |
| 388 | Ty = llvm::ArrayType::get(Ty, NumPaddingBytes); |
Anders Carlsson | f8e94f2 | 2009-09-12 02:44:18 +0000 | [diff] [blame] | 389 | |
Anders Carlsson | ccbabc9 | 2009-09-13 17:55:13 +0000 | [diff] [blame] | 390 | Types.push_back(Ty); |
Anders Carlsson | f8e94f2 | 2009-09-12 02:44:18 +0000 | [diff] [blame] | 391 | |
Anders Carlsson | ccbabc9 | 2009-09-13 17:55:13 +0000 | [diff] [blame] | 392 | // We want a packed struct. |
| 393 | Packed = true; |
| 394 | } |
Anders Carlsson | f8e94f2 | 2009-09-12 02:44:18 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | // T x; |
Fariborz Jahanian | 087206d | 2010-09-03 23:07:53 +0000 | [diff] [blame] | 398 | Types.push_back(ConvertTypeForMem(Ty)); |
Anders Carlsson | 0168f4b | 2009-09-12 02:14:24 +0000 | [diff] [blame] | 399 | |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 400 | const llvm::Type *T = llvm::StructType::get(getLLVMContext(), Types, Packed); |
Anders Carlsson | 10f2c10 | 2009-09-10 01:32:12 +0000 | [diff] [blame] | 401 | |
| 402 | cast<llvm::OpaqueType>(ByRefTypeHolder.get())->refineAbstractTypeTo(T); |
| 403 | CGM.getModule().addTypeName("struct.__block_byref_" + D->getNameAsString(), |
| 404 | ByRefTypeHolder.get()); |
| 405 | |
Anders Carlsson | 0168f4b | 2009-09-12 02:14:24 +0000 | [diff] [blame] | 406 | Info.first = ByRefTypeHolder.get(); |
Anders Carlsson | f8e94f2 | 2009-09-12 02:44:18 +0000 | [diff] [blame] | 407 | |
| 408 | Info.second = Types.size() - 1; |
Anders Carlsson | 0168f4b | 2009-09-12 02:14:24 +0000 | [diff] [blame] | 409 | |
| 410 | return Info.first; |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 411 | } |
| 412 | |
John McCall | 2b7fc38 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 413 | namespace { |
John McCall | cda666c | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 414 | struct CallArrayDtor : EHScopeStack::Cleanup { |
John McCall | 2b7fc38 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 415 | CallArrayDtor(const CXXDestructorDecl *Dtor, |
| 416 | const ConstantArrayType *Type, |
| 417 | llvm::Value *Loc) |
| 418 | : Dtor(Dtor), Type(Type), Loc(Loc) {} |
| 419 | |
| 420 | const CXXDestructorDecl *Dtor; |
| 421 | const ConstantArrayType *Type; |
| 422 | llvm::Value *Loc; |
| 423 | |
| 424 | void Emit(CodeGenFunction &CGF, bool IsForEH) { |
| 425 | QualType BaseElementTy = CGF.getContext().getBaseElementType(Type); |
| 426 | const llvm::Type *BasePtr = CGF.ConvertType(BaseElementTy); |
| 427 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
| 428 | llvm::Value *BaseAddrPtr = CGF.Builder.CreateBitCast(Loc, BasePtr); |
| 429 | CGF.EmitCXXAggrDestructorCall(Dtor, Type, BaseAddrPtr); |
| 430 | } |
| 431 | }; |
| 432 | |
John McCall | cda666c | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 433 | struct CallVarDtor : EHScopeStack::Cleanup { |
John McCall | 2b7fc38 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 434 | CallVarDtor(const CXXDestructorDecl *Dtor, |
| 435 | llvm::Value *NRVOFlag, |
| 436 | llvm::Value *Loc) |
| 437 | : Dtor(Dtor), NRVOFlag(NRVOFlag), Loc(Loc) {} |
| 438 | |
| 439 | const CXXDestructorDecl *Dtor; |
| 440 | llvm::Value *NRVOFlag; |
| 441 | llvm::Value *Loc; |
| 442 | |
| 443 | void Emit(CodeGenFunction &CGF, bool IsForEH) { |
| 444 | // Along the exceptions path we always execute the dtor. |
| 445 | bool NRVO = !IsForEH && NRVOFlag; |
| 446 | |
| 447 | llvm::BasicBlock *SkipDtorBB = 0; |
| 448 | if (NRVO) { |
| 449 | // If we exited via NRVO, we skip the destructor call. |
| 450 | llvm::BasicBlock *RunDtorBB = CGF.createBasicBlock("nrvo.unused"); |
| 451 | SkipDtorBB = CGF.createBasicBlock("nrvo.skipdtor"); |
| 452 | llvm::Value *DidNRVO = CGF.Builder.CreateLoad(NRVOFlag, "nrvo.val"); |
| 453 | CGF.Builder.CreateCondBr(DidNRVO, SkipDtorBB, RunDtorBB); |
| 454 | CGF.EmitBlock(RunDtorBB); |
| 455 | } |
| 456 | |
| 457 | CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, |
| 458 | /*ForVirtualBase=*/false, Loc); |
| 459 | |
| 460 | if (NRVO) CGF.EmitBlock(SkipDtorBB); |
| 461 | } |
| 462 | }; |
| 463 | } |
| 464 | |
John McCall | a464ff9 | 2010-07-21 06:13:08 +0000 | [diff] [blame] | 465 | namespace { |
John McCall | cda666c | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 466 | struct CallStackRestore : EHScopeStack::Cleanup { |
John McCall | a464ff9 | 2010-07-21 06:13:08 +0000 | [diff] [blame] | 467 | llvm::Value *Stack; |
| 468 | CallStackRestore(llvm::Value *Stack) : Stack(Stack) {} |
| 469 | void Emit(CodeGenFunction &CGF, bool IsForEH) { |
| 470 | llvm::Value *V = CGF.Builder.CreateLoad(Stack, "tmp"); |
| 471 | llvm::Value *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stackrestore); |
| 472 | CGF.Builder.CreateCall(F, V); |
| 473 | } |
| 474 | }; |
| 475 | |
John McCall | cda666c | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 476 | struct CallCleanupFunction : EHScopeStack::Cleanup { |
John McCall | a464ff9 | 2010-07-21 06:13:08 +0000 | [diff] [blame] | 477 | llvm::Constant *CleanupFn; |
| 478 | const CGFunctionInfo &FnInfo; |
John McCall | a464ff9 | 2010-07-21 06:13:08 +0000 | [diff] [blame] | 479 | const VarDecl &Var; |
| 480 | |
| 481 | CallCleanupFunction(llvm::Constant *CleanupFn, const CGFunctionInfo *Info, |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 482 | const VarDecl *Var) |
| 483 | : CleanupFn(CleanupFn), FnInfo(*Info), Var(*Var) {} |
John McCall | a464ff9 | 2010-07-21 06:13:08 +0000 | [diff] [blame] | 484 | |
| 485 | void Emit(CodeGenFunction &CGF, bool IsForEH) { |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 486 | DeclRefExpr DRE(const_cast<VarDecl*>(&Var), Var.getType(), VK_LValue, |
| 487 | SourceLocation()); |
| 488 | // Compute the address of the local variable, in case it's a byref |
| 489 | // or something. |
| 490 | llvm::Value *Addr = CGF.EmitDeclRefLValue(&DRE).getAddress(); |
| 491 | |
John McCall | a464ff9 | 2010-07-21 06:13:08 +0000 | [diff] [blame] | 492 | // In some cases, the type of the function argument will be different from |
| 493 | // the type of the pointer. An example of this is |
| 494 | // void f(void* arg); |
| 495 | // __attribute__((cleanup(f))) void *g; |
| 496 | // |
| 497 | // To fix this we insert a bitcast here. |
| 498 | QualType ArgTy = FnInfo.arg_begin()->type; |
| 499 | llvm::Value *Arg = |
| 500 | CGF.Builder.CreateBitCast(Addr, CGF.ConvertType(ArgTy)); |
| 501 | |
| 502 | CallArgList Args; |
| 503 | Args.push_back(std::make_pair(RValue::get(Arg), |
| 504 | CGF.getContext().getPointerType(Var.getType()))); |
| 505 | CGF.EmitCall(FnInfo, CleanupFn, ReturnValueSlot(), Args); |
| 506 | } |
| 507 | }; |
| 508 | |
John McCall | cda666c | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 509 | struct CallBlockRelease : EHScopeStack::Cleanup { |
John McCall | a464ff9 | 2010-07-21 06:13:08 +0000 | [diff] [blame] | 510 | llvm::Value *Addr; |
| 511 | CallBlockRelease(llvm::Value *Addr) : Addr(Addr) {} |
| 512 | |
| 513 | void Emit(CodeGenFunction &CGF, bool IsForEH) { |
John McCall | a9a2e8a | 2011-02-18 02:58:31 +0000 | [diff] [blame] | 514 | CGF.BuildBlockRelease(Addr, BLOCK_FIELD_IS_BYREF); |
John McCall | a464ff9 | 2010-07-21 06:13:08 +0000 | [diff] [blame] | 515 | } |
| 516 | }; |
| 517 | } |
| 518 | |
Chris Lattner | b85025f | 2010-12-01 02:05:19 +0000 | [diff] [blame] | 519 | |
| 520 | /// canEmitInitWithFewStoresAfterMemset - Decide whether we can emit the |
| 521 | /// non-zero parts of the specified initializer with equal or fewer than |
| 522 | /// NumStores scalar stores. |
| 523 | static bool canEmitInitWithFewStoresAfterMemset(llvm::Constant *Init, |
| 524 | unsigned &NumStores) { |
Chris Lattner | e6af886 | 2010-12-02 01:58:41 +0000 | [diff] [blame] | 525 | // Zero and Undef never requires any extra stores. |
| 526 | if (isa<llvm::ConstantAggregateZero>(Init) || |
| 527 | isa<llvm::ConstantPointerNull>(Init) || |
| 528 | isa<llvm::UndefValue>(Init)) |
| 529 | return true; |
| 530 | if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) || |
| 531 | isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) || |
| 532 | isa<llvm::ConstantExpr>(Init)) |
| 533 | return Init->isNullValue() || NumStores--; |
| 534 | |
| 535 | // See if we can emit each element. |
| 536 | if (isa<llvm::ConstantArray>(Init) || isa<llvm::ConstantStruct>(Init)) { |
| 537 | for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) { |
| 538 | llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i)); |
| 539 | if (!canEmitInitWithFewStoresAfterMemset(Elt, NumStores)) |
| 540 | return false; |
| 541 | } |
| 542 | return true; |
| 543 | } |
Chris Lattner | b85025f | 2010-12-01 02:05:19 +0000 | [diff] [blame] | 544 | |
| 545 | // Anything else is hard and scary. |
| 546 | return false; |
| 547 | } |
| 548 | |
| 549 | /// emitStoresForInitAfterMemset - For inits that |
| 550 | /// canEmitInitWithFewStoresAfterMemset returned true for, emit the scalar |
| 551 | /// stores that would be required. |
| 552 | static void emitStoresForInitAfterMemset(llvm::Constant *Init, llvm::Value *Loc, |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 553 | bool isVolatile, CGBuilderTy &Builder) { |
Chris Lattner | b85025f | 2010-12-01 02:05:19 +0000 | [diff] [blame] | 554 | // Zero doesn't require any stores. |
Chris Lattner | e6af886 | 2010-12-02 01:58:41 +0000 | [diff] [blame] | 555 | if (isa<llvm::ConstantAggregateZero>(Init) || |
| 556 | isa<llvm::ConstantPointerNull>(Init) || |
| 557 | isa<llvm::UndefValue>(Init)) |
| 558 | return; |
Chris Lattner | b85025f | 2010-12-01 02:05:19 +0000 | [diff] [blame] | 559 | |
Chris Lattner | e6af886 | 2010-12-02 01:58:41 +0000 | [diff] [blame] | 560 | if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) || |
| 561 | isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) || |
| 562 | isa<llvm::ConstantExpr>(Init)) { |
| 563 | if (!Init->isNullValue()) |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 564 | Builder.CreateStore(Init, Loc, isVolatile); |
Chris Lattner | e6af886 | 2010-12-02 01:58:41 +0000 | [diff] [blame] | 565 | return; |
| 566 | } |
Chris Lattner | b85025f | 2010-12-01 02:05:19 +0000 | [diff] [blame] | 567 | |
Chris Lattner | e6af886 | 2010-12-02 01:58:41 +0000 | [diff] [blame] | 568 | assert((isa<llvm::ConstantStruct>(Init) || isa<llvm::ConstantArray>(Init)) && |
| 569 | "Unknown value type!"); |
| 570 | |
| 571 | for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) { |
| 572 | llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i)); |
| 573 | if (Elt->isNullValue()) continue; |
| 574 | |
| 575 | // Otherwise, get a pointer to the element and emit it. |
| 576 | emitStoresForInitAfterMemset(Elt, Builder.CreateConstGEP2_32(Loc, 0, i), |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 577 | isVolatile, Builder); |
Chris Lattner | e6af886 | 2010-12-02 01:58:41 +0000 | [diff] [blame] | 578 | } |
Chris Lattner | b85025f | 2010-12-01 02:05:19 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | |
| 582 | /// shouldUseMemSetPlusStoresToInitialize - Decide whether we should use memset |
| 583 | /// plus some stores to initialize a local variable instead of using a memcpy |
| 584 | /// from a constant global. It is beneficial to use memset if the global is all |
| 585 | /// zeros, or mostly zeros and large. |
| 586 | static bool shouldUseMemSetPlusStoresToInitialize(llvm::Constant *Init, |
| 587 | uint64_t GlobalSize) { |
| 588 | // If a global is all zeros, always use a memset. |
| 589 | if (isa<llvm::ConstantAggregateZero>(Init)) return true; |
| 590 | |
| 591 | |
| 592 | // If a non-zero global is <= 32 bytes, always use a memcpy. If it is large, |
| 593 | // do it if it will require 6 or fewer scalar stores. |
| 594 | // TODO: Should budget depends on the size? Avoiding a large global warrants |
| 595 | // plopping in more stores. |
| 596 | unsigned StoreBudget = 6; |
| 597 | uint64_t SizeLimit = 32; |
| 598 | |
| 599 | return GlobalSize > SizeLimit && |
| 600 | canEmitInitWithFewStoresAfterMemset(Init, StoreBudget); |
| 601 | } |
| 602 | |
| 603 | |
Nick Lewycky | 8a7d90b | 2010-12-30 20:21:55 +0000 | [diff] [blame] | 604 | /// EmitAutoVarDecl - Emit code and set up an entry in LocalDeclMap for a |
Chris Lattner | 03df122 | 2007-06-02 04:53:11 +0000 | [diff] [blame] | 605 | /// variable declaration with auto, register, or no storage class specifier. |
Chris Lattner | b781dc79 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 606 | /// These turn into simple stack objects, or GlobalValues depending on target. |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 607 | void CodeGenFunction::EmitAutoVarDecl(const VarDecl &D) { |
| 608 | AutoVarEmission emission = EmitAutoVarAlloca(D); |
| 609 | EmitAutoVarInit(emission); |
| 610 | EmitAutoVarCleanups(emission); |
| 611 | } |
Chris Lattner | 03df122 | 2007-06-02 04:53:11 +0000 | [diff] [blame] | 612 | |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 613 | /// EmitAutoVarAlloca - Emit the alloca and debug information for a |
| 614 | /// local variable. Does not emit initalization or destruction. |
| 615 | CodeGenFunction::AutoVarEmission |
| 616 | CodeGenFunction::EmitAutoVarAlloca(const VarDecl &D) { |
| 617 | QualType Ty = D.getType(); |
| 618 | |
| 619 | AutoVarEmission emission(D); |
| 620 | |
| 621 | bool isByRef = D.hasAttr<BlocksAttr>(); |
| 622 | emission.IsByRef = isByRef; |
| 623 | |
| 624 | CharUnits alignment = getContext().getDeclAlign(&D); |
| 625 | emission.Alignment = alignment; |
| 626 | |
Chris Lattner | 03df122 | 2007-06-02 04:53:11 +0000 | [diff] [blame] | 627 | llvm::Value *DeclPtr; |
Eli Friedman | a682d39 | 2008-02-15 12:20:59 +0000 | [diff] [blame] | 628 | if (Ty->isConstantSizeType()) { |
Chris Lattner | b781dc79 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 629 | if (!Target.useGlobalsForAutomaticVariables()) { |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 630 | bool NRVO = getContext().getLangOptions().ElideConstructors && |
| 631 | D.isNRVOVariable(); |
| 632 | |
| 633 | // If this value is a POD array or struct with a statically |
| 634 | // determinable constant initializer, there are optimizations we |
| 635 | // can do. |
| 636 | // TODO: we can potentially constant-evaluate non-POD structs and |
| 637 | // arrays as long as the initialization is trivial (e.g. if they |
| 638 | // have a non-trivial destructor, but not a non-trivial constructor). |
| 639 | if (D.getInit() && |
| 640 | (Ty->isArrayType() || Ty->isRecordType()) && Ty->isPODType() && |
John McCall | 8b0f4ff | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 641 | D.getInit()->isConstantInitializer(getContext(), false)) { |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 642 | |
| 643 | // If the variable's a const type, and it's neither an NRVO |
| 644 | // candidate nor a __block variable, emit it as a global instead. |
| 645 | if (CGM.getCodeGenOpts().MergeAllConstants && Ty.isConstQualified() && |
| 646 | !NRVO && !isByRef) { |
Douglas Gregor | 19da4e4 | 2011-03-06 23:28:21 +0000 | [diff] [blame] | 647 | EmitStaticVarDecl(D, llvm::GlobalValue::InternalLinkage); |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 648 | |
| 649 | emission.Address = 0; // signal this condition to later callbacks |
| 650 | assert(emission.wasEmittedAsGlobal()); |
| 651 | return emission; |
Tanya Lattner | f9d41df | 2009-11-04 01:18:09 +0000 | [diff] [blame] | 652 | } |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 653 | |
| 654 | // Otherwise, tell the initialization code that we're in this case. |
| 655 | emission.IsConstantAggregate = true; |
Tanya Lattner | f9d41df | 2009-11-04 01:18:09 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Douglas Gregor | 290c93e | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 658 | // A normal fixed sized variable becomes an alloca in the entry block, |
| 659 | // unless it's an NRVO variable. |
Eli Friedman | 3efa41a | 2009-03-04 04:25:14 +0000 | [diff] [blame] | 660 | const llvm::Type *LTy = ConvertTypeForMem(Ty); |
Douglas Gregor | 290c93e | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 661 | |
| 662 | if (NRVO) { |
| 663 | // The named return value optimization: allocate this variable in the |
| 664 | // return slot, so that we can elide the copy when returning this |
| 665 | // variable (C++0x [class.copy]p34). |
| 666 | DeclPtr = ReturnValue; |
Douglas Gregor | 9154b5d | 2010-05-17 15:52:46 +0000 | [diff] [blame] | 667 | |
| 668 | if (const RecordType *RecordTy = Ty->getAs<RecordType>()) { |
| 669 | if (!cast<CXXRecordDecl>(RecordTy->getDecl())->hasTrivialDestructor()) { |
| 670 | // Create a flag that is used to indicate when the NRVO was applied |
| 671 | // to this variable. Set it to zero to indicate that NRVO was not |
| 672 | // applied. |
Chris Lattner | 46a7ad7 | 2010-12-01 01:47:15 +0000 | [diff] [blame] | 673 | llvm::Value *Zero = Builder.getFalse(); |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 674 | llvm::Value *NRVOFlag = CreateTempAlloca(Zero->getType(), "nrvo"); |
Nick Lewycky | 8d22262 | 2011-02-16 23:59:08 +0000 | [diff] [blame] | 675 | EnsureInsertPoint(); |
Douglas Gregor | 9154b5d | 2010-05-17 15:52:46 +0000 | [diff] [blame] | 676 | Builder.CreateStore(Zero, NRVOFlag); |
| 677 | |
| 678 | // Record the NRVO flag for this variable. |
| 679 | NRVOFlags[&D] = NRVOFlag; |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 680 | emission.NRVOFlag = NRVOFlag; |
Douglas Gregor | 9154b5d | 2010-05-17 15:52:46 +0000 | [diff] [blame] | 681 | } |
| 682 | } |
Douglas Gregor | 290c93e | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 683 | } else { |
| 684 | if (isByRef) |
| 685 | LTy = BuildByRefType(&D); |
| 686 | |
| 687 | llvm::AllocaInst *Alloc = CreateTempAlloca(LTy); |
| 688 | Alloc->setName(D.getNameAsString()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 689 | |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 690 | CharUnits allocaAlignment = alignment; |
Douglas Gregor | 290c93e | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 691 | if (isByRef) |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 692 | allocaAlignment = std::max(allocaAlignment, |
Ken Dyck | 9a64869 | 2011-01-18 02:01:14 +0000 | [diff] [blame] | 693 | getContext().toCharUnitsFromBits(Target.getPointerAlign(0))); |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 694 | Alloc->setAlignment(allocaAlignment.getQuantity()); |
Douglas Gregor | 290c93e | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 695 | DeclPtr = Alloc; |
| 696 | } |
Chris Lattner | b781dc79 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 697 | } else { |
| 698 | // Targets that don't support recursion emit locals as globals. |
| 699 | const char *Class = |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 700 | D.getStorageClass() == SC_Register ? ".reg." : ".auto."; |
John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 701 | DeclPtr = CreateStaticVarDecl(D, Class, |
| 702 | llvm::GlobalValue::InternalLinkage); |
Chris Lattner | b781dc79 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 703 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 704 | |
Daniel Dunbar | b6adc43 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 705 | // FIXME: Can this happen? |
Anders Carlsson | 8a01b79 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 706 | if (Ty->isVariablyModifiedType()) |
| 707 | EmitVLASize(Ty); |
Chris Lattner | 03df122 | 2007-06-02 04:53:11 +0000 | [diff] [blame] | 708 | } else { |
Daniel Dunbar | b6adc43 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 709 | EnsureInsertPoint(); |
| 710 | |
Anders Carlsson | 15949b3 | 2009-02-09 20:41:50 +0000 | [diff] [blame] | 711 | if (!DidCallStackSave) { |
Anders Carlsson | 3003288 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 712 | // Save the stack. |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 713 | llvm::Value *Stack = CreateTempAlloca(Int8PtrTy, "saved_stack"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 714 | |
Anders Carlsson | 3003288 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 715 | llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave); |
| 716 | llvm::Value *V = Builder.CreateCall(F); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 717 | |
Anders Carlsson | 3003288 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 718 | Builder.CreateStore(V, Stack); |
Anders Carlsson | 15949b3 | 2009-02-09 20:41:50 +0000 | [diff] [blame] | 719 | |
| 720 | DidCallStackSave = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 721 | |
John McCall | a464ff9 | 2010-07-21 06:13:08 +0000 | [diff] [blame] | 722 | // Push a cleanup block and restore the stack there. |
John McCall | e4df6c8 | 2011-01-28 08:37:24 +0000 | [diff] [blame] | 723 | // FIXME: in general circumstances, this should be an EH cleanup. |
John McCall | cda666c | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 724 | EHStack.pushCleanup<CallStackRestore>(NormalCleanup, Stack); |
Anders Carlsson | 3003288 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 725 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 726 | |
Anders Carlsson | 3003288 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 727 | // Get the element type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 728 | const llvm::Type *LElemTy = ConvertTypeForMem(Ty); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame^] | 729 | const llvm::Type *LElemPtrTy = |
| 730 | LElemTy->getPointerTo(CGM.getContext().getTargetAddressSpace(Ty)); |
Anders Carlsson | 3003288 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 731 | |
Anders Carlsson | 8a01b79 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 732 | llvm::Value *VLASize = EmitVLASize(Ty); |
Anders Carlsson | 3003288 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 733 | |
| 734 | // Allocate memory for the array. |
Anders Carlsson | e33eed5 | 2009-09-26 18:16:06 +0000 | [diff] [blame] | 735 | llvm::AllocaInst *VLA = |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 736 | Builder.CreateAlloca(llvm::Type::getInt8Ty(getLLVMContext()), VLASize, "vla"); |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 737 | VLA->setAlignment(alignment.getQuantity()); |
Anders Carlsson | e33eed5 | 2009-09-26 18:16:06 +0000 | [diff] [blame] | 738 | |
Eli Friedman | f408470 | 2008-12-20 23:11:59 +0000 | [diff] [blame] | 739 | DeclPtr = Builder.CreateBitCast(VLA, LElemPtrTy, "tmp"); |
Chris Lattner | 03df122 | 2007-06-02 04:53:11 +0000 | [diff] [blame] | 740 | } |
Eli Friedman | f408470 | 2008-12-20 23:11:59 +0000 | [diff] [blame] | 741 | |
Chris Lattner | 03df122 | 2007-06-02 04:53:11 +0000 | [diff] [blame] | 742 | llvm::Value *&DMEntry = LocalDeclMap[&D]; |
| 743 | assert(DMEntry == 0 && "Decl already exists in localdeclmap!"); |
| 744 | DMEntry = DeclPtr; |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 745 | emission.Address = DeclPtr; |
Sanjiv Gupta | 18de624 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 746 | |
| 747 | // Emit debug info for local var declaration. |
Anders Carlsson | 63784f4 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 748 | if (CGDebugInfo *DI = getDebugInfo()) { |
Daniel Dunbar | 669521c | 2009-07-19 07:03:11 +0000 | [diff] [blame] | 749 | assert(HaveInsertPoint() && "Unexpected unreachable point!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 750 | |
Daniel Dunbar | b9fd902 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 751 | DI->setLocation(D.getLocation()); |
Sanjiv Gupta | 47e2961 | 2009-05-22 13:54:25 +0000 | [diff] [blame] | 752 | if (Target.useGlobalsForAutomaticVariables()) { |
| 753 | DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(DeclPtr), &D); |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 754 | } else |
| 755 | DI->EmitDeclareOfAutoVariable(&D, DeclPtr, Builder); |
Sanjiv Gupta | 18de624 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 756 | } |
| 757 | |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 758 | return emission; |
| 759 | } |
| 760 | |
| 761 | /// Determines whether the given __block variable is potentially |
| 762 | /// captured by the given expression. |
| 763 | static bool isCapturedBy(const VarDecl &var, const Expr *e) { |
| 764 | // Skip the most common kinds of expressions that make |
| 765 | // hierarchy-walking expensive. |
| 766 | e = e->IgnoreParenCasts(); |
| 767 | |
| 768 | if (const BlockExpr *be = dyn_cast<BlockExpr>(e)) { |
| 769 | const BlockDecl *block = be->getBlockDecl(); |
| 770 | for (BlockDecl::capture_const_iterator i = block->capture_begin(), |
| 771 | e = block->capture_end(); i != e; ++i) { |
| 772 | if (i->getVariable() == &var) |
| 773 | return true; |
| 774 | } |
| 775 | |
| 776 | // No need to walk into the subexpressions. |
| 777 | return false; |
| 778 | } |
| 779 | |
| 780 | for (Stmt::const_child_range children = e->children(); children; ++children) |
| 781 | if (isCapturedBy(var, cast<Expr>(*children))) |
| 782 | return true; |
| 783 | |
| 784 | return false; |
| 785 | } |
| 786 | |
| 787 | void CodeGenFunction::EmitAutoVarInit(const AutoVarEmission &emission) { |
John McCall | 9e2e22f | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 788 | assert(emission.Variable && "emission was not valid!"); |
| 789 | |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 790 | // If this was emitted as a global constant, we're done. |
| 791 | if (emission.wasEmittedAsGlobal()) return; |
| 792 | |
John McCall | 9e2e22f | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 793 | const VarDecl &D = *emission.Variable; |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 794 | QualType type = D.getType(); |
| 795 | |
Chris Lattner | 07eb733 | 2007-07-12 00:39:48 +0000 | [diff] [blame] | 796 | // If this local has an initializer, emit it now. |
Daniel Dunbar | b6adc43 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 797 | const Expr *Init = D.getInit(); |
| 798 | |
| 799 | // If we are at an unreachable point, we don't need to emit the initializer |
| 800 | // unless it contains a label. |
| 801 | if (!HaveInsertPoint()) { |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 802 | if (!Init || !ContainsLabel(Init)) return; |
| 803 | EnsureInsertPoint(); |
Daniel Dunbar | b6adc43 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 804 | } |
| 805 | |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 806 | CharUnits alignment = emission.Alignment; |
| 807 | |
| 808 | if (emission.IsByRef) { |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 809 | llvm::Value *V; |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 810 | |
| 811 | BlockFieldFlags fieldFlags; |
| 812 | bool fieldNeedsCopyDispose = false; |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 813 | |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 814 | if (type->isBlockPointerType()) { |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 815 | fieldFlags |= BLOCK_FIELD_IS_BLOCK; |
| 816 | fieldNeedsCopyDispose = true; |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 817 | } else if (getContext().isObjCNSObjectType(type) || |
| 818 | type->isObjCObjectPointerType()) { |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 819 | fieldFlags |= BLOCK_FIELD_IS_OBJECT; |
| 820 | fieldNeedsCopyDispose = true; |
| 821 | } else if (getLangOptions().CPlusPlus) { |
| 822 | if (getContext().getBlockVarCopyInits(&D)) |
| 823 | fieldNeedsCopyDispose = true; |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 824 | else if (const CXXRecordDecl *record = type->getAsCXXRecordDecl()) |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 825 | fieldNeedsCopyDispose = !record->hasTrivialDestructor(); |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 826 | } |
Mike Stump | 90d8daf | 2009-03-07 06:04:31 +0000 | [diff] [blame] | 827 | |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 828 | llvm::Value *addr = emission.Address; |
| 829 | |
Mike Stump | 90d8daf | 2009-03-07 06:04:31 +0000 | [diff] [blame] | 830 | // FIXME: Someone double check this. |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 831 | if (type.isObjCGCWeak()) |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 832 | fieldFlags |= BLOCK_FIELD_IS_WEAK; |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 833 | |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 834 | // Initialize the 'isa', which is just 0 or 1. |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 835 | int isa = 0; |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 836 | if (fieldFlags & BLOCK_FIELD_IS_WEAK) |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 837 | isa = 1; |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 838 | V = Builder.CreateIntToPtr(Builder.getInt32(isa), Int8PtrTy, "isa"); |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 839 | Builder.CreateStore(V, Builder.CreateStructGEP(addr, 0, "byref.isa")); |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 840 | |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 841 | // Store the address of the variable into its own forwarding pointer. |
| 842 | Builder.CreateStore(addr, |
| 843 | Builder.CreateStructGEP(addr, 1, "byref.forwarding")); |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 844 | |
John McCall | a9a2e8a | 2011-02-18 02:58:31 +0000 | [diff] [blame] | 845 | // Blocks ABI: |
| 846 | // c) the flags field is set to either 0 if no helper functions are |
| 847 | // needed or BLOCK_HAS_COPY_DISPOSE if they are, |
| 848 | BlockFlags flags; |
| 849 | if (fieldNeedsCopyDispose) flags |= BLOCK_HAS_COPY_DISPOSE; |
| 850 | Builder.CreateStore(llvm::ConstantInt::get(IntTy, flags.getBitMask()), |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 851 | Builder.CreateStructGEP(addr, 2, "byref.flags")); |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 852 | |
Mike Stump | 4446dcf | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 853 | const llvm::Type *V1; |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 854 | V1 = cast<llvm::PointerType>(addr->getType())->getElementType(); |
John McCall | a9a2e8a | 2011-02-18 02:58:31 +0000 | [diff] [blame] | 855 | V = llvm::ConstantInt::get(IntTy, CGM.GetTargetTypeStoreSize(V1).getQuantity()); |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 856 | Builder.CreateStore(V, Builder.CreateStructGEP(addr, 3, "byref.size")); |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 857 | |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 858 | if (fieldNeedsCopyDispose) { |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 859 | llvm::Value *copy_helper = Builder.CreateStructGEP(addr, 4); |
| 860 | Builder.CreateStore(CGM.BuildbyrefCopyHelper(addr->getType(), fieldFlags, |
| 861 | alignment.getQuantity(), &D), |
Mike Stump | f89230d | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 862 | copy_helper); |
Mike Stump | 97d01d5 | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 863 | |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 864 | llvm::Value *destroy_helper = Builder.CreateStructGEP(addr, 5); |
| 865 | Builder.CreateStore(CGM.BuildbyrefDestroyHelper(addr->getType(), |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 866 | fieldFlags, |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 867 | alignment.getQuantity(), |
| 868 | &D), |
Mike Stump | fbe25dd | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 869 | destroy_helper); |
Chris Lattner | 9de9527 | 2007-08-26 05:13:54 +0000 | [diff] [blame] | 870 | } |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 871 | } |
Anders Carlsson | cadb9a6 | 2009-02-07 23:51:38 +0000 | [diff] [blame] | 872 | |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 873 | if (!Init) return; |
Mon P Wang | cc2ab0c | 2010-04-04 03:10:52 +0000 | [diff] [blame] | 874 | |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 875 | // Check whether this is a byref variable that's potentially |
| 876 | // captured and moved by its own initializer. If so, we'll need to |
| 877 | // emit the initializer first, then copy into the variable. |
| 878 | bool capturedByInit = emission.IsByRef && isCapturedBy(D, Init); |
| 879 | |
| 880 | llvm::Value *Loc = |
| 881 | capturedByInit ? emission.Address : emission.getObjectAddress(*this); |
| 882 | |
John McCall | 91ca10f | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 883 | if (!emission.IsConstantAggregate) |
| 884 | return EmitExprAsInit(Init, &D, Loc, alignment, capturedByInit); |
| 885 | |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 886 | // If this is a simple aggregate initialization, we can optimize it |
| 887 | // in various ways. |
John McCall | 91ca10f | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 888 | assert(!capturedByInit && "constant init contains a capturing block?"); |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 889 | |
John McCall | 91ca10f | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 890 | bool isVolatile = type.isVolatileQualified(); |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 891 | |
John McCall | 91ca10f | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 892 | llvm::Constant *constant = CGM.EmitConstantExpr(D.getInit(), type, this); |
| 893 | assert(constant != 0 && "Wasn't a simple constant init?"); |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 894 | |
John McCall | 91ca10f | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 895 | llvm::Value *SizeVal = |
| 896 | llvm::ConstantInt::get(IntPtrTy, |
| 897 | getContext().getTypeSizeInChars(type).getQuantity()); |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 898 | |
John McCall | 91ca10f | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 899 | const llvm::Type *BP = Int8PtrTy; |
| 900 | if (Loc->getType() != BP) |
| 901 | Loc = Builder.CreateBitCast(Loc, BP, "tmp"); |
Mon P Wang | cc2ab0c | 2010-04-04 03:10:52 +0000 | [diff] [blame] | 902 | |
John McCall | 91ca10f | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 903 | // If the initializer is all or mostly zeros, codegen with memset then do |
| 904 | // a few stores afterward. |
| 905 | if (shouldUseMemSetPlusStoresToInitialize(constant, |
| 906 | CGM.getTargetData().getTypeAllocSize(constant->getType()))) { |
| 907 | Builder.CreateMemSet(Loc, llvm::ConstantInt::get(Int8Ty, 0), SizeVal, |
| 908 | alignment.getQuantity(), isVolatile); |
| 909 | if (!constant->isNullValue()) { |
| 910 | Loc = Builder.CreateBitCast(Loc, constant->getType()->getPointerTo()); |
| 911 | emitStoresForInitAfterMemset(constant, Loc, isVolatile, Builder); |
Fariborz Jahanian | c614073 | 2010-03-12 21:40:43 +0000 | [diff] [blame] | 912 | } |
John McCall | 91ca10f | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 913 | } else { |
| 914 | // Otherwise, create a temporary global with the initializer then |
| 915 | // memcpy from the global to the alloca. |
| 916 | std::string Name = GetStaticDeclName(*this, D, "."); |
| 917 | llvm::GlobalVariable *GV = |
| 918 | new llvm::GlobalVariable(CGM.getModule(), constant->getType(), true, |
| 919 | llvm::GlobalValue::InternalLinkage, |
| 920 | constant, Name, 0, false, 0); |
| 921 | GV->setAlignment(alignment.getQuantity()); |
| 922 | |
| 923 | llvm::Value *SrcPtr = GV; |
| 924 | if (SrcPtr->getType() != BP) |
| 925 | SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp"); |
| 926 | |
| 927 | Builder.CreateMemCpy(Loc, SrcPtr, SizeVal, alignment.getQuantity(), |
| 928 | isVolatile); |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | /// Emit an expression as an initializer for a variable at the given |
| 933 | /// location. The expression is not necessarily the normal |
| 934 | /// initializer for the variable, and the address is not necessarily |
| 935 | /// its normal location. |
| 936 | /// |
| 937 | /// \param init the initializing expression |
| 938 | /// \param var the variable to act as if we're initializing |
| 939 | /// \param loc the address to initialize; its type is a pointer |
| 940 | /// to the LLVM mapping of the variable's type |
| 941 | /// \param alignment the alignment of the address |
| 942 | /// \param capturedByInit true if the variable is a __block variable |
| 943 | /// whose address is potentially changed by the initializer |
| 944 | void CodeGenFunction::EmitExprAsInit(const Expr *init, |
| 945 | const VarDecl *var, |
| 946 | llvm::Value *loc, |
| 947 | CharUnits alignment, |
| 948 | bool capturedByInit) { |
| 949 | QualType type = var->getType(); |
| 950 | bool isVolatile = type.isVolatileQualified(); |
| 951 | |
| 952 | if (type->isReferenceType()) { |
| 953 | RValue RV = EmitReferenceBindingToExpr(init, var); |
| 954 | if (capturedByInit) loc = BuildBlockByrefAddress(loc, var); |
| 955 | EmitStoreOfScalar(RV.getScalarVal(), loc, false, |
| 956 | alignment.getQuantity(), type); |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 957 | } else if (!hasAggregateLLVMType(type)) { |
John McCall | 91ca10f | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 958 | llvm::Value *V = EmitScalarExpr(init); |
| 959 | if (capturedByInit) loc = BuildBlockByrefAddress(loc, var); |
| 960 | EmitStoreOfScalar(V, loc, isVolatile, alignment.getQuantity(), type); |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 961 | } else if (type->isAnyComplexType()) { |
John McCall | 91ca10f | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 962 | ComplexPairTy complex = EmitComplexExpr(init); |
| 963 | if (capturedByInit) loc = BuildBlockByrefAddress(loc, var); |
| 964 | StoreComplexToAddr(complex, loc, isVolatile); |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 965 | } else { |
| 966 | // TODO: how can we delay here if D is captured by its initializer? |
John McCall | 91ca10f | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 967 | EmitAggExpr(init, AggValueSlot::forAddr(loc, isVolatile, true, false)); |
Fariborz Jahanian | c614073 | 2010-03-12 21:40:43 +0000 | [diff] [blame] | 968 | } |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 969 | } |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 970 | |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 971 | void CodeGenFunction::EmitAutoVarCleanups(const AutoVarEmission &emission) { |
John McCall | 9e2e22f | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 972 | assert(emission.Variable && "emission was not valid!"); |
| 973 | |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 974 | // If this was emitted as a global constant, we're done. |
| 975 | if (emission.wasEmittedAsGlobal()) return; |
| 976 | |
John McCall | 9e2e22f | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 977 | const VarDecl &D = *emission.Variable; |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 978 | |
| 979 | // Handle C++ destruction of variables. |
| 980 | if (getLangOptions().CPlusPlus) { |
| 981 | QualType type = D.getType(); |
| 982 | QualType baseType = getContext().getBaseElementType(type); |
| 983 | if (const RecordType *RT = baseType->getAs<RecordType>()) { |
| 984 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Douglas Gregor | 1701256 | 2010-05-15 16:39:56 +0000 | [diff] [blame] | 985 | if (!ClassDecl->hasTrivialDestructor()) { |
Douglas Gregor | 9154b5d | 2010-05-17 15:52:46 +0000 | [diff] [blame] | 986 | // Note: We suppress the destructor call when the corresponding NRVO |
| 987 | // flag has been set. |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 988 | |
| 989 | // Note that for __block variables, we want to destroy the |
| 990 | // original stack object, not the possible forwarded object. |
| 991 | llvm::Value *Loc = emission.getObjectAddress(*this); |
Douglas Gregor | 290c93e | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 992 | |
Douglas Gregor | bac7490 | 2010-07-01 14:13:13 +0000 | [diff] [blame] | 993 | const CXXDestructorDecl *D = ClassDecl->getDestructor(); |
Fariborz Jahanian | 18c0623 | 2009-08-03 20:20:07 +0000 | [diff] [blame] | 994 | assert(D && "EmitLocalBlockVarDecl - destructor is nul"); |
Fariborz Jahanian | c9076fe | 2009-10-29 16:22:54 +0000 | [diff] [blame] | 995 | |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 996 | if (type != baseType) { |
| 997 | const ConstantArrayType *Array = |
| 998 | getContext().getAsConstantArrayType(type); |
| 999 | assert(Array && "types changed without array?"); |
John McCall | cda666c | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 1000 | EHStack.pushCleanup<CallArrayDtor>(NormalAndEHCleanup, |
| 1001 | D, Array, Loc); |
Fariborz Jahanian | 09cc10f | 2009-11-04 17:57:40 +0000 | [diff] [blame] | 1002 | } else { |
John McCall | cda666c | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 1003 | EHStack.pushCleanup<CallVarDtor>(NormalAndEHCleanup, |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1004 | D, emission.NRVOFlag, Loc); |
Fariborz Jahanian | 09cc10f | 2009-11-04 17:57:40 +0000 | [diff] [blame] | 1005 | } |
Fariborz Jahanian | 18c0623 | 2009-08-03 20:20:07 +0000 | [diff] [blame] | 1006 | } |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1007 | } |
Fariborz Jahanian | 18c0623 | 2009-08-03 20:20:07 +0000 | [diff] [blame] | 1008 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1009 | |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1010 | // Handle the cleanup attribute. |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1011 | if (const CleanupAttr *CA = D.getAttr<CleanupAttr>()) { |
Anders Carlsson | cadb9a6 | 2009-02-07 23:51:38 +0000 | [diff] [blame] | 1012 | const FunctionDecl *FD = CA->getFunctionDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1013 | |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1014 | llvm::Constant *F = CGM.GetAddrOfFunction(FD); |
Anders Carlsson | cadb9a6 | 2009-02-07 23:51:38 +0000 | [diff] [blame] | 1015 | assert(F && "Could not find function!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1016 | |
Anders Carlsson | 6f5c156 | 2009-04-26 00:34:20 +0000 | [diff] [blame] | 1017 | const CGFunctionInfo &Info = CGM.getTypes().getFunctionInfo(FD); |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1018 | EHStack.pushCleanup<CallCleanupFunction>(NormalAndEHCleanup, F, &Info, &D); |
Anders Carlsson | cadb9a6 | 2009-02-07 23:51:38 +0000 | [diff] [blame] | 1019 | } |
Mike Stump | 626aecc | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 1020 | |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1021 | // If this is a block variable, call _Block_object_destroy |
| 1022 | // (on the unforwarded address). |
| 1023 | if (emission.IsByRef && |
| 1024 | CGM.getLangOptions().getGCMode() != LangOptions::GCOnly) |
| 1025 | EHStack.pushCleanup<CallBlockRelease>(NormalAndEHCleanup, emission.Address); |
Chris Lattner | 03df122 | 2007-06-02 04:53:11 +0000 | [diff] [blame] | 1026 | } |
Chris Lattner | 53621a5 | 2007-06-13 20:44:40 +0000 | [diff] [blame] | 1027 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1028 | /// Emit an alloca (or GlobalValue depending on target) |
Chris Lattner | b781dc79 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 1029 | /// for the specified parameter and set up LocalDeclMap. |
Devang Patel | 68a1525 | 2011-03-03 20:13:15 +0000 | [diff] [blame] | 1030 | void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg, |
| 1031 | unsigned ArgNo) { |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1032 | // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl? |
Sanjiv Gupta | d795924 | 2008-10-31 09:52:39 +0000 | [diff] [blame] | 1033 | assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) && |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1034 | "Invalid argument to EmitParmDecl"); |
John McCall | 147d021 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 1035 | |
| 1036 | Arg->setName(D.getName()); |
| 1037 | |
| 1038 | // Use better IR generation for certain implicit parameters. |
| 1039 | if (isa<ImplicitParamDecl>(D)) { |
| 1040 | // The only implicit argument a block has is its literal. |
| 1041 | if (BlockInfo) { |
| 1042 | LocalDeclMap[&D] = Arg; |
| 1043 | |
| 1044 | if (CGDebugInfo *DI = getDebugInfo()) { |
| 1045 | DI->setLocation(D.getLocation()); |
| 1046 | DI->EmitDeclareOfBlockLiteralArgVariable(*BlockInfo, Arg, Builder); |
| 1047 | } |
| 1048 | |
| 1049 | return; |
| 1050 | } |
| 1051 | } |
| 1052 | |
Chris Lattner | fc4379f | 2008-04-06 23:10:54 +0000 | [diff] [blame] | 1053 | QualType Ty = D.getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1054 | |
Chris Lattner | 53621a5 | 2007-06-13 20:44:40 +0000 | [diff] [blame] | 1055 | llvm::Value *DeclPtr; |
Daniel Dunbar | 3d33fab | 2010-02-08 22:53:07 +0000 | [diff] [blame] | 1056 | // If this is an aggregate or variable sized value, reuse the input pointer. |
| 1057 | if (!Ty->isConstantSizeType() || |
| 1058 | CodeGenFunction::hasAggregateLLVMType(Ty)) { |
Chris Lattner | 53621a5 | 2007-06-13 20:44:40 +0000 | [diff] [blame] | 1059 | DeclPtr = Arg; |
Chris Lattner | 53621a5 | 2007-06-13 20:44:40 +0000 | [diff] [blame] | 1060 | } else { |
Daniel Dunbar | 3d33fab | 2010-02-08 22:53:07 +0000 | [diff] [blame] | 1061 | // Otherwise, create a temporary to hold the value. |
Daniel Dunbar | a7566f1 | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 1062 | DeclPtr = CreateMemTemp(Ty, D.getName() + ".addr"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1063 | |
Daniel Dunbar | 3d33fab | 2010-02-08 22:53:07 +0000 | [diff] [blame] | 1064 | // Store the initial value into the alloca. |
John McCall | e3dc170 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 1065 | EmitStoreOfScalar(Arg, DeclPtr, Ty.isVolatileQualified(), |
| 1066 | getContext().getDeclAlign(&D).getQuantity(), Ty, |
| 1067 | CGM.getTBAAInfo(Ty)); |
Chris Lattner | 53621a5 | 2007-06-13 20:44:40 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | llvm::Value *&DMEntry = LocalDeclMap[&D]; |
| 1071 | assert(DMEntry == 0 && "Decl already exists in localdeclmap!"); |
| 1072 | DMEntry = DeclPtr; |
Sanjiv Gupta | 18de624 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 1073 | |
| 1074 | // Emit debug info for param declaration. |
Devang Patel | 3028a43 | 2009-10-09 22:06:15 +0000 | [diff] [blame] | 1075 | if (CGDebugInfo *DI = getDebugInfo()) { |
| 1076 | DI->setLocation(D.getLocation()); |
Devang Patel | 68a1525 | 2011-03-03 20:13:15 +0000 | [diff] [blame] | 1077 | DI->EmitDeclareOfArgVariable(&D, DeclPtr, ArgNo, Builder); |
Devang Patel | 3028a43 | 2009-10-09 22:06:15 +0000 | [diff] [blame] | 1078 | } |
Chris Lattner | 53621a5 | 2007-06-13 20:44:40 +0000 | [diff] [blame] | 1079 | } |