| 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: | 
| Devang Patel | 12e6d83 | 2011-04-05 20:28:21 +0000 | [diff] [blame] | 72 | assert(0 && "Declaration should not be in declstmts!"); | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 73 | case Decl::Function:  // void X(); | 
| Argyrios Kyrtzidis | 35bc082 | 2008-10-15 00:42:39 +0000 | [diff] [blame] | 74 | case Decl::Record:    // struct/union/class X; | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 75 | case Decl::Enum:      // enum X; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 76 | case Decl::EnumConstant: // enum ? { X = ? } | 
| Argyrios Kyrtzidis | 35bc082 | 2008-10-15 00:42:39 +0000 | [diff] [blame] | 77 | case Decl::CXXRecord: // struct/union/class X; [C++] | 
| Daniel Dunbar | fa133a1 | 2009-11-23 00:07:06 +0000 | [diff] [blame] | 78 | case Decl::Using:          // using X; [C++] | 
|  | 79 | case Decl::UsingShadow: | 
|  | 80 | case Decl::UsingDirective: // using namespace X; [C++] | 
| Douglas Gregor | 08688ac | 2010-04-23 02:02:43 +0000 | [diff] [blame] | 81 | case Decl::NamespaceAlias: | 
| Anders Carlsson | 7b0ca3f | 2009-12-03 17:26:31 +0000 | [diff] [blame] | 82 | case Decl::StaticAssert: // static_assert(X, ""); [C++0x] | 
| Chris Lattner | 4ae493c | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 83 | case Decl::Label:        // __label__ x; | 
| 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 |  | 
| Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame^] | 94 | case Decl::Typedef:      // typedef int X; | 
|  | 95 | case Decl::TypeAlias: {  // using X = int; [C++0x] | 
|  | 96 | const TypedefNameDecl &TD = cast<TypedefNameDecl>(D); | 
| Anders Carlsson | fcdbb93 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 97 | QualType Ty = TD.getUnderlyingType(); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 98 |  | 
| Anders Carlsson | fcdbb93 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 99 | if (Ty->isVariablyModifiedType()) | 
|  | 100 | EmitVLASize(Ty); | 
|  | 101 | } | 
| Daniel Dunbar | 662174c8 | 2008-08-29 17:28:43 +0000 | [diff] [blame] | 102 | } | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 103 | } | 
|  | 104 |  | 
| John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 105 | /// EmitVarDecl - This method handles emission of any variable declaration | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 106 | /// inside a function, including static vars etc. | 
| John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 107 | void CodeGenFunction::EmitVarDecl(const VarDecl &D) { | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 108 | switch (D.getStorageClass()) { | 
| John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 109 | case SC_None: | 
|  | 110 | case SC_Auto: | 
|  | 111 | case SC_Register: | 
| John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 112 | return EmitAutoVarDecl(D); | 
| John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 113 | case SC_Static: { | 
| Anders Carlsson | f6b89a1 | 2010-02-07 02:03:08 +0000 | [diff] [blame] | 114 | llvm::GlobalValue::LinkageTypes Linkage = | 
|  | 115 | llvm::GlobalValue::InternalLinkage; | 
|  | 116 |  | 
| John McCall | 8b24233 | 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 | f6b89a1 | 2010-02-07 02:03:08 +0000 | [diff] [blame] | 125 |  | 
| John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 126 | return EmitStaticVarDecl(D, Linkage); | 
| Anders Carlsson | f6b89a1 | 2010-02-07 02:03:08 +0000 | [diff] [blame] | 127 | } | 
| John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 128 | case SC_Extern: | 
|  | 129 | case SC_PrivateExtern: | 
| Lauro Ramos Venancio | fea90b8 | 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; | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 132 | } | 
| Daniel Dunbar | 5466c7b | 2009-04-14 02:25:56 +0000 | [diff] [blame] | 133 |  | 
|  | 134 | assert(0 && "Unknown storage class"); | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 135 | } | 
|  | 136 |  | 
| Chris Lattner | 761acc1 | 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 | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 140 | if (CGF.getContext().getLangOptions().CPlusPlus) { | 
| Anders Carlsson | 9a20d55 | 2010-06-22 16:16:50 +0000 | [diff] [blame] | 141 | llvm::StringRef Name = CGM.getMangledName(&D); | 
|  | 142 | return Name.str(); | 
| John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 143 | } | 
| Chris Lattner | 761acc1 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 144 |  | 
|  | 145 | std::string ContextName; | 
| Fariborz Jahanian | faa5bfc | 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 | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 152 | CGM.getBlockMangledName(GlobalDecl(), Name, BD); | 
| Fariborz Jahanian | faa5bfc | 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 | 9a20d55 | 2010-06-22 16:16:50 +0000 | [diff] [blame] | 158 | llvm::StringRef Name = CGM.getMangledName(FD); | 
|  | 159 | ContextName = Name.str(); | 
| John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 160 | } else if (isa<ObjCMethodDecl>(CGF.CurFuncDecl)) | 
| Chris Lattner | 761acc1 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 161 | ContextName = CGF.CurFn->getName(); | 
|  | 162 | else | 
| Fariborz Jahanian | faa5bfc | 2010-11-30 23:07:14 +0000 | [diff] [blame] | 163 | assert(0 && "Unknown context for static var decl"); | 
| Chris Lattner | 761acc1 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 164 |  | 
|  | 165 | return ContextName + Separator + D.getNameAsString(); | 
|  | 166 | } | 
|  | 167 |  | 
| Daniel Dunbar | 0096acf | 2009-02-25 19:24:29 +0000 | [diff] [blame] | 168 | llvm::GlobalVariable * | 
| John McCall | b6bbcc9 | 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 | 8bcfc5b | 2008-04-06 23:10:54 +0000 | [diff] [blame] | 172 | QualType Ty = D.getType(); | 
| Eli Friedman | 3c2b317 | 2008-02-15 12:20:59 +0000 | [diff] [blame] | 173 | assert(Ty->isConstantSizeType() && "VLAs can't be static"); | 
| Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 174 |  | 
| Chris Lattner | 761acc1 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 175 | std::string Name = GetStaticDeclName(*this, D, Separator); | 
| Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 176 |  | 
| Daniel Dunbar | 0096acf | 2009-02-25 19:24:29 +0000 | [diff] [blame] | 177 | const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty); | 
| Anders Carlsson | 41f8a13 | 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 | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 182 | D.isThreadSpecified(), | 
|  | 183 | CGM.getContext().getTargetAddressSpace(Ty)); | 
| Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 184 | GV->setAlignment(getContext().getDeclAlign(&D).getQuantity()); | 
| John McCall | 112c967 | 2010-11-02 21:04:24 +0000 | [diff] [blame] | 185 | if (Linkage != llvm::GlobalValue::InternalLinkage) | 
|  | 186 | GV->setVisibility(CurFn->getVisibility()); | 
| Anders Carlsson | 41f8a13 | 2009-09-26 18:16:06 +0000 | [diff] [blame] | 187 | return GV; | 
| Daniel Dunbar | 0096acf | 2009-02-25 19:24:29 +0000 | [diff] [blame] | 188 | } | 
|  | 189 |  | 
| John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 190 | /// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the | 
| Chris Lattner | 761acc1 | 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 | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 195 | CodeGenFunction::AddInitializerToStaticVarDecl(const VarDecl &D, | 
|  | 196 | llvm::GlobalVariable *GV) { | 
| Chris Lattner | 761acc1 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 197 | llvm::Constant *Init = CGM.EmitConstantExpr(D.getInit(), D.getType(), this); | 
| John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 198 |  | 
| Chris Lattner | 761acc1 | 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 | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 204 | else if (Builder.GetInsertBlock()) { | 
| Anders Carlsson | 071c810 | 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 | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 208 |  | 
| John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 209 | EmitCXXGuardedInit(D, GV); | 
| Anders Carlsson | 071c810 | 2010-01-26 04:02:23 +0000 | [diff] [blame] | 210 | } | 
| Chris Lattner | 761acc1 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 211 | return GV; | 
|  | 212 | } | 
| John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 213 |  | 
| Chris Lattner | 761acc1 | 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 | 9c20fa9 | 2010-09-03 18:58:50 +0000 | [diff] [blame] | 218 | if (GV->getType()->getElementType() != Init->getType()) { | 
| Chris Lattner | 761acc1 | 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 | 112c967 | 2010-11-02 21:04:24 +0000 | [diff] [blame] | 224 | /*InsertBefore*/ OldGV, | 
|  | 225 | D.isThreadSpecified(), | 
| Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 226 | CGM.getContext().getTargetAddressSpace(D.getType())); | 
| John McCall | 112c967 | 2010-11-02 21:04:24 +0000 | [diff] [blame] | 227 | GV->setVisibility(OldGV->getVisibility()); | 
| Chris Lattner | 761acc1 | 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 | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 245 | void CodeGenFunction::EmitStaticVarDecl(const VarDecl &D, | 
| Anders Carlsson | f6b89a1 | 2010-02-07 02:03:08 +0000 | [diff] [blame] | 246 | llvm::GlobalValue::LinkageTypes Linkage) { | 
| Daniel Dunbar | a985b31 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 249 |  | 
| John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 250 | llvm::GlobalVariable *GV = CreateStaticVarDecl(D, ".", Linkage); | 
| Daniel Dunbar | a985b31 | 2009-02-25 19:45:19 +0000 | [diff] [blame] | 251 |  | 
| Daniel Dunbar | e5731f8 | 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 | fe67f3b | 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 | c62aad8 | 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 | 0934914 | 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 | c62aad8 | 2009-04-20 03:54:15 +0000 | [diff] [blame] | 265 |  | 
| Chris Lattner | 761acc1 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 266 | // If this value has an initializer, emit it. | 
|  | 267 | if (D.getInit()) | 
| John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 268 | GV = AddInitializerToStaticVarDecl(D, GV); | 
| Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 269 |  | 
| Chris Lattner | 0af9523 | 2010-03-10 23:59:59 +0000 | [diff] [blame] | 270 | GV->setAlignment(getContext().getDeclAlign(&D).getQuantity()); | 
|  | 271 |  | 
| Daniel Dunbar | 30510ab | 2009-02-12 23:32:54 +0000 | [diff] [blame] | 272 | // FIXME: Merge attribute handling. | 
| Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 273 | if (const AnnotateAttr *AA = D.getAttr<AnnotateAttr>()) { | 
| Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 274 | SourceManager &SM = CGM.getContext().getSourceManager(); | 
|  | 275 | llvm::Constant *Ann = | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 276 | CGM.EmitAnnotateAttr(GV, AA, | 
| Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 277 | SM.getInstantiationLineNumber(D.getLocation())); | 
| Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 278 | CGM.AddAnnotation(Ann); | 
|  | 279 | } | 
|  | 280 |  | 
| Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 281 | if (const SectionAttr *SA = D.getAttr<SectionAttr>()) | 
| Daniel Dunbar | 30510ab | 2009-02-12 23:32:54 +0000 | [diff] [blame] | 282 | GV->setSection(SA->getName()); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 283 |  | 
| Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 284 | if (D.hasAttr<UsedAttr>()) | 
| Daniel Dunbar | 5c61d97 | 2009-02-13 22:08:43 +0000 | [diff] [blame] | 285 | CGM.AddUsedGlobal(GV); | 
|  | 286 |  | 
| Daniel Dunbar | e5731f8 | 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 | 2659052 | 2008-06-08 01:23:18 +0000 | [diff] [blame] | 292 | const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(D.getType()); | 
| Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 293 | const llvm::Type *LPtrTy = | 
|  | 294 | LTy->getPointerTo(CGM.getContext().getTargetAddressSpace(D.getType())); | 
| Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 295 | DMEntry = llvm::ConstantExpr::getBitCast(GV, LPtrTy); | 
| Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 296 |  | 
|  | 297 | // Emit global variable debug descriptor for static vars. | 
| Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 298 | CGDebugInfo *DI = getDebugInfo(); | 
| Mike Stump | 4451bd9 | 2009-02-20 00:19:45 +0000 | [diff] [blame] | 299 | if (DI) { | 
| Daniel Dunbar | 66031a5 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 300 | DI->setLocation(D.getLocation()); | 
| Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 301 | DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(GV), &D); | 
|  | 302 | } | 
| Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 303 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 304 |  | 
| John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 305 | namespace { | 
| John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 306 | struct CallArrayDtor : EHScopeStack::Cleanup { | 
| John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 307 | CallArrayDtor(const CXXDestructorDecl *Dtor, | 
|  | 308 | const ConstantArrayType *Type, | 
|  | 309 | llvm::Value *Loc) | 
|  | 310 | : Dtor(Dtor), Type(Type), Loc(Loc) {} | 
|  | 311 |  | 
|  | 312 | const CXXDestructorDecl *Dtor; | 
|  | 313 | const ConstantArrayType *Type; | 
|  | 314 | llvm::Value *Loc; | 
|  | 315 |  | 
|  | 316 | void Emit(CodeGenFunction &CGF, bool IsForEH) { | 
|  | 317 | QualType BaseElementTy = CGF.getContext().getBaseElementType(Type); | 
|  | 318 | const llvm::Type *BasePtr = CGF.ConvertType(BaseElementTy); | 
|  | 319 | BasePtr = llvm::PointerType::getUnqual(BasePtr); | 
|  | 320 | llvm::Value *BaseAddrPtr = CGF.Builder.CreateBitCast(Loc, BasePtr); | 
|  | 321 | CGF.EmitCXXAggrDestructorCall(Dtor, Type, BaseAddrPtr); | 
|  | 322 | } | 
|  | 323 | }; | 
|  | 324 |  | 
| John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 325 | struct CallVarDtor : EHScopeStack::Cleanup { | 
| John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 326 | CallVarDtor(const CXXDestructorDecl *Dtor, | 
|  | 327 | llvm::Value *NRVOFlag, | 
|  | 328 | llvm::Value *Loc) | 
|  | 329 | : Dtor(Dtor), NRVOFlag(NRVOFlag), Loc(Loc) {} | 
|  | 330 |  | 
|  | 331 | const CXXDestructorDecl *Dtor; | 
|  | 332 | llvm::Value *NRVOFlag; | 
|  | 333 | llvm::Value *Loc; | 
|  | 334 |  | 
|  | 335 | void Emit(CodeGenFunction &CGF, bool IsForEH) { | 
|  | 336 | // Along the exceptions path we always execute the dtor. | 
|  | 337 | bool NRVO = !IsForEH && NRVOFlag; | 
|  | 338 |  | 
|  | 339 | llvm::BasicBlock *SkipDtorBB = 0; | 
|  | 340 | if (NRVO) { | 
|  | 341 | // If we exited via NRVO, we skip the destructor call. | 
|  | 342 | llvm::BasicBlock *RunDtorBB = CGF.createBasicBlock("nrvo.unused"); | 
|  | 343 | SkipDtorBB = CGF.createBasicBlock("nrvo.skipdtor"); | 
|  | 344 | llvm::Value *DidNRVO = CGF.Builder.CreateLoad(NRVOFlag, "nrvo.val"); | 
|  | 345 | CGF.Builder.CreateCondBr(DidNRVO, SkipDtorBB, RunDtorBB); | 
|  | 346 | CGF.EmitBlock(RunDtorBB); | 
|  | 347 | } | 
|  | 348 |  | 
|  | 349 | CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, | 
|  | 350 | /*ForVirtualBase=*/false, Loc); | 
|  | 351 |  | 
|  | 352 | if (NRVO) CGF.EmitBlock(SkipDtorBB); | 
|  | 353 | } | 
|  | 354 | }; | 
|  | 355 | } | 
|  | 356 |  | 
| John McCall | d871509 | 2010-07-21 06:13:08 +0000 | [diff] [blame] | 357 | namespace { | 
| John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 358 | struct CallStackRestore : EHScopeStack::Cleanup { | 
| John McCall | d871509 | 2010-07-21 06:13:08 +0000 | [diff] [blame] | 359 | llvm::Value *Stack; | 
|  | 360 | CallStackRestore(llvm::Value *Stack) : Stack(Stack) {} | 
|  | 361 | void Emit(CodeGenFunction &CGF, bool IsForEH) { | 
|  | 362 | llvm::Value *V = CGF.Builder.CreateLoad(Stack, "tmp"); | 
|  | 363 | llvm::Value *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stackrestore); | 
|  | 364 | CGF.Builder.CreateCall(F, V); | 
|  | 365 | } | 
|  | 366 | }; | 
|  | 367 |  | 
| John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 368 | struct CallCleanupFunction : EHScopeStack::Cleanup { | 
| John McCall | d871509 | 2010-07-21 06:13:08 +0000 | [diff] [blame] | 369 | llvm::Constant *CleanupFn; | 
|  | 370 | const CGFunctionInfo &FnInfo; | 
| John McCall | d871509 | 2010-07-21 06:13:08 +0000 | [diff] [blame] | 371 | const VarDecl &Var; | 
|  | 372 |  | 
|  | 373 | CallCleanupFunction(llvm::Constant *CleanupFn, const CGFunctionInfo *Info, | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 374 | const VarDecl *Var) | 
|  | 375 | : CleanupFn(CleanupFn), FnInfo(*Info), Var(*Var) {} | 
| John McCall | d871509 | 2010-07-21 06:13:08 +0000 | [diff] [blame] | 376 |  | 
|  | 377 | void Emit(CodeGenFunction &CGF, bool IsForEH) { | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 378 | DeclRefExpr DRE(const_cast<VarDecl*>(&Var), Var.getType(), VK_LValue, | 
|  | 379 | SourceLocation()); | 
|  | 380 | // Compute the address of the local variable, in case it's a byref | 
|  | 381 | // or something. | 
|  | 382 | llvm::Value *Addr = CGF.EmitDeclRefLValue(&DRE).getAddress(); | 
|  | 383 |  | 
| John McCall | d871509 | 2010-07-21 06:13:08 +0000 | [diff] [blame] | 384 | // In some cases, the type of the function argument will be different from | 
|  | 385 | // the type of the pointer. An example of this is | 
|  | 386 | // void f(void* arg); | 
|  | 387 | // __attribute__((cleanup(f))) void *g; | 
|  | 388 | // | 
|  | 389 | // To fix this we insert a bitcast here. | 
|  | 390 | QualType ArgTy = FnInfo.arg_begin()->type; | 
|  | 391 | llvm::Value *Arg = | 
|  | 392 | CGF.Builder.CreateBitCast(Addr, CGF.ConvertType(ArgTy)); | 
|  | 393 |  | 
|  | 394 | CallArgList Args; | 
|  | 395 | Args.push_back(std::make_pair(RValue::get(Arg), | 
|  | 396 | CGF.getContext().getPointerType(Var.getType()))); | 
|  | 397 | CGF.EmitCall(FnInfo, CleanupFn, ReturnValueSlot(), Args); | 
|  | 398 | } | 
|  | 399 | }; | 
| John McCall | d871509 | 2010-07-21 06:13:08 +0000 | [diff] [blame] | 400 | } | 
|  | 401 |  | 
| Chris Lattner | 94cd011 | 2010-12-01 02:05:19 +0000 | [diff] [blame] | 402 |  | 
|  | 403 | /// canEmitInitWithFewStoresAfterMemset - Decide whether we can emit the | 
|  | 404 | /// non-zero parts of the specified initializer with equal or fewer than | 
|  | 405 | /// NumStores scalar stores. | 
|  | 406 | static bool canEmitInitWithFewStoresAfterMemset(llvm::Constant *Init, | 
|  | 407 | unsigned &NumStores) { | 
| Chris Lattner | 70b0294 | 2010-12-02 01:58:41 +0000 | [diff] [blame] | 408 | // Zero and Undef never requires any extra stores. | 
|  | 409 | if (isa<llvm::ConstantAggregateZero>(Init) || | 
|  | 410 | isa<llvm::ConstantPointerNull>(Init) || | 
|  | 411 | isa<llvm::UndefValue>(Init)) | 
|  | 412 | return true; | 
|  | 413 | if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) || | 
|  | 414 | isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) || | 
|  | 415 | isa<llvm::ConstantExpr>(Init)) | 
|  | 416 | return Init->isNullValue() || NumStores--; | 
|  | 417 |  | 
|  | 418 | // See if we can emit each element. | 
|  | 419 | if (isa<llvm::ConstantArray>(Init) || isa<llvm::ConstantStruct>(Init)) { | 
|  | 420 | for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) { | 
|  | 421 | llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i)); | 
|  | 422 | if (!canEmitInitWithFewStoresAfterMemset(Elt, NumStores)) | 
|  | 423 | return false; | 
|  | 424 | } | 
|  | 425 | return true; | 
|  | 426 | } | 
| Chris Lattner | 94cd011 | 2010-12-01 02:05:19 +0000 | [diff] [blame] | 427 |  | 
|  | 428 | // Anything else is hard and scary. | 
|  | 429 | return false; | 
|  | 430 | } | 
|  | 431 |  | 
|  | 432 | /// emitStoresForInitAfterMemset - For inits that | 
|  | 433 | /// canEmitInitWithFewStoresAfterMemset returned true for, emit the scalar | 
|  | 434 | /// stores that would be required. | 
|  | 435 | static void emitStoresForInitAfterMemset(llvm::Constant *Init, llvm::Value *Loc, | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 436 | bool isVolatile, CGBuilderTy &Builder) { | 
| Chris Lattner | 94cd011 | 2010-12-01 02:05:19 +0000 | [diff] [blame] | 437 | // Zero doesn't require any stores. | 
| Chris Lattner | 70b0294 | 2010-12-02 01:58:41 +0000 | [diff] [blame] | 438 | if (isa<llvm::ConstantAggregateZero>(Init) || | 
|  | 439 | isa<llvm::ConstantPointerNull>(Init) || | 
|  | 440 | isa<llvm::UndefValue>(Init)) | 
|  | 441 | return; | 
| Chris Lattner | 94cd011 | 2010-12-01 02:05:19 +0000 | [diff] [blame] | 442 |  | 
| Chris Lattner | 70b0294 | 2010-12-02 01:58:41 +0000 | [diff] [blame] | 443 | if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) || | 
|  | 444 | isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) || | 
|  | 445 | isa<llvm::ConstantExpr>(Init)) { | 
|  | 446 | if (!Init->isNullValue()) | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 447 | Builder.CreateStore(Init, Loc, isVolatile); | 
| Chris Lattner | 70b0294 | 2010-12-02 01:58:41 +0000 | [diff] [blame] | 448 | return; | 
|  | 449 | } | 
| Chris Lattner | 94cd011 | 2010-12-01 02:05:19 +0000 | [diff] [blame] | 450 |  | 
| Chris Lattner | 70b0294 | 2010-12-02 01:58:41 +0000 | [diff] [blame] | 451 | assert((isa<llvm::ConstantStruct>(Init) || isa<llvm::ConstantArray>(Init)) && | 
|  | 452 | "Unknown value type!"); | 
|  | 453 |  | 
|  | 454 | for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) { | 
|  | 455 | llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i)); | 
|  | 456 | if (Elt->isNullValue()) continue; | 
|  | 457 |  | 
|  | 458 | // Otherwise, get a pointer to the element and emit it. | 
|  | 459 | emitStoresForInitAfterMemset(Elt, Builder.CreateConstGEP2_32(Loc, 0, i), | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 460 | isVolatile, Builder); | 
| Chris Lattner | 70b0294 | 2010-12-02 01:58:41 +0000 | [diff] [blame] | 461 | } | 
| Chris Lattner | 94cd011 | 2010-12-01 02:05:19 +0000 | [diff] [blame] | 462 | } | 
|  | 463 |  | 
|  | 464 |  | 
|  | 465 | /// shouldUseMemSetPlusStoresToInitialize - Decide whether we should use memset | 
|  | 466 | /// plus some stores to initialize a local variable instead of using a memcpy | 
|  | 467 | /// from a constant global.  It is beneficial to use memset if the global is all | 
|  | 468 | /// zeros, or mostly zeros and large. | 
|  | 469 | static bool shouldUseMemSetPlusStoresToInitialize(llvm::Constant *Init, | 
|  | 470 | uint64_t GlobalSize) { | 
|  | 471 | // If a global is all zeros, always use a memset. | 
|  | 472 | if (isa<llvm::ConstantAggregateZero>(Init)) return true; | 
|  | 473 |  | 
|  | 474 |  | 
|  | 475 | // If a non-zero global is <= 32 bytes, always use a memcpy.  If it is large, | 
|  | 476 | // do it if it will require 6 or fewer scalar stores. | 
|  | 477 | // TODO: Should budget depends on the size?  Avoiding a large global warrants | 
|  | 478 | // plopping in more stores. | 
|  | 479 | unsigned StoreBudget = 6; | 
|  | 480 | uint64_t SizeLimit = 32; | 
|  | 481 |  | 
|  | 482 | return GlobalSize > SizeLimit && | 
|  | 483 | canEmitInitWithFewStoresAfterMemset(Init, StoreBudget); | 
|  | 484 | } | 
|  | 485 |  | 
|  | 486 |  | 
| Nick Lewycky | a9de3fa | 2010-12-30 20:21:55 +0000 | [diff] [blame] | 487 | /// EmitAutoVarDecl - Emit code and set up an entry in LocalDeclMap for a | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 488 | /// variable declaration with auto, register, or no storage class specifier. | 
| Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 489 | /// These turn into simple stack objects, or GlobalValues depending on target. | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 490 | void CodeGenFunction::EmitAutoVarDecl(const VarDecl &D) { | 
|  | 491 | AutoVarEmission emission = EmitAutoVarAlloca(D); | 
|  | 492 | EmitAutoVarInit(emission); | 
|  | 493 | EmitAutoVarCleanups(emission); | 
|  | 494 | } | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 495 |  | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 496 | /// EmitAutoVarAlloca - Emit the alloca and debug information for a | 
|  | 497 | /// local variable.  Does not emit initalization or destruction. | 
|  | 498 | CodeGenFunction::AutoVarEmission | 
|  | 499 | CodeGenFunction::EmitAutoVarAlloca(const VarDecl &D) { | 
|  | 500 | QualType Ty = D.getType(); | 
|  | 501 |  | 
|  | 502 | AutoVarEmission emission(D); | 
|  | 503 |  | 
|  | 504 | bool isByRef = D.hasAttr<BlocksAttr>(); | 
|  | 505 | emission.IsByRef = isByRef; | 
|  | 506 |  | 
|  | 507 | CharUnits alignment = getContext().getDeclAlign(&D); | 
|  | 508 | emission.Alignment = alignment; | 
|  | 509 |  | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 510 | llvm::Value *DeclPtr; | 
| Eli Friedman | 3c2b317 | 2008-02-15 12:20:59 +0000 | [diff] [blame] | 511 | if (Ty->isConstantSizeType()) { | 
| Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 512 | if (!Target.useGlobalsForAutomaticVariables()) { | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 513 | bool NRVO = getContext().getLangOptions().ElideConstructors && | 
|  | 514 | D.isNRVOVariable(); | 
|  | 515 |  | 
|  | 516 | // If this value is a POD array or struct with a statically | 
|  | 517 | // determinable constant initializer, there are optimizations we | 
|  | 518 | // can do. | 
|  | 519 | // TODO: we can potentially constant-evaluate non-POD structs and | 
|  | 520 | // arrays as long as the initialization is trivial (e.g. if they | 
|  | 521 | // have a non-trivial destructor, but not a non-trivial constructor). | 
|  | 522 | if (D.getInit() && | 
|  | 523 | (Ty->isArrayType() || Ty->isRecordType()) && Ty->isPODType() && | 
| John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 524 | D.getInit()->isConstantInitializer(getContext(), false)) { | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 525 |  | 
|  | 526 | // If the variable's a const type, and it's neither an NRVO | 
|  | 527 | // candidate nor a __block variable, emit it as a global instead. | 
|  | 528 | if (CGM.getCodeGenOpts().MergeAllConstants && Ty.isConstQualified() && | 
|  | 529 | !NRVO && !isByRef) { | 
| Douglas Gregor | 4707b9a | 2011-03-06 23:28:21 +0000 | [diff] [blame] | 530 | EmitStaticVarDecl(D, llvm::GlobalValue::InternalLinkage); | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 531 |  | 
|  | 532 | emission.Address = 0; // signal this condition to later callbacks | 
|  | 533 | assert(emission.wasEmittedAsGlobal()); | 
|  | 534 | return emission; | 
| Tanya Lattner | 59876c2 | 2009-11-04 01:18:09 +0000 | [diff] [blame] | 535 | } | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 536 |  | 
|  | 537 | // Otherwise, tell the initialization code that we're in this case. | 
|  | 538 | emission.IsConstantAggregate = true; | 
| Tanya Lattner | 59876c2 | 2009-11-04 01:18:09 +0000 | [diff] [blame] | 539 | } | 
|  | 540 |  | 
| Douglas Gregor | d86c477 | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 541 | // A normal fixed sized variable becomes an alloca in the entry block, | 
|  | 542 | // unless it's an NRVO variable. | 
| Eli Friedman | a3460ac | 2009-03-04 04:25:14 +0000 | [diff] [blame] | 543 | const llvm::Type *LTy = ConvertTypeForMem(Ty); | 
| Douglas Gregor | d86c477 | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 544 |  | 
|  | 545 | if (NRVO) { | 
|  | 546 | // The named return value optimization: allocate this variable in the | 
|  | 547 | // return slot, so that we can elide the copy when returning this | 
|  | 548 | // variable (C++0x [class.copy]p34). | 
|  | 549 | DeclPtr = ReturnValue; | 
| Douglas Gregor | 3d91bbc | 2010-05-17 15:52:46 +0000 | [diff] [blame] | 550 |  | 
|  | 551 | if (const RecordType *RecordTy = Ty->getAs<RecordType>()) { | 
|  | 552 | if (!cast<CXXRecordDecl>(RecordTy->getDecl())->hasTrivialDestructor()) { | 
|  | 553 | // Create a flag that is used to indicate when the NRVO was applied | 
|  | 554 | // to this variable. Set it to zero to indicate that NRVO was not | 
|  | 555 | // applied. | 
| Chris Lattner | 4c53dc1 | 2010-12-01 01:47:15 +0000 | [diff] [blame] | 556 | llvm::Value *Zero = Builder.getFalse(); | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 557 | llvm::Value *NRVOFlag = CreateTempAlloca(Zero->getType(), "nrvo"); | 
| Nick Lewycky | a03733b | 2011-02-16 23:59:08 +0000 | [diff] [blame] | 558 | EnsureInsertPoint(); | 
| Douglas Gregor | 3d91bbc | 2010-05-17 15:52:46 +0000 | [diff] [blame] | 559 | Builder.CreateStore(Zero, NRVOFlag); | 
|  | 560 |  | 
|  | 561 | // Record the NRVO flag for this variable. | 
|  | 562 | NRVOFlags[&D] = NRVOFlag; | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 563 | emission.NRVOFlag = NRVOFlag; | 
| Douglas Gregor | 3d91bbc | 2010-05-17 15:52:46 +0000 | [diff] [blame] | 564 | } | 
|  | 565 | } | 
| Douglas Gregor | d86c477 | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 566 | } else { | 
|  | 567 | if (isByRef) | 
|  | 568 | LTy = BuildByRefType(&D); | 
|  | 569 |  | 
|  | 570 | llvm::AllocaInst *Alloc = CreateTempAlloca(LTy); | 
|  | 571 | Alloc->setName(D.getNameAsString()); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 572 |  | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 573 | CharUnits allocaAlignment = alignment; | 
| Douglas Gregor | d86c477 | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 574 | if (isByRef) | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 575 | allocaAlignment = std::max(allocaAlignment, | 
| Ken Dyck | 06f486e | 2011-01-18 02:01:14 +0000 | [diff] [blame] | 576 | getContext().toCharUnitsFromBits(Target.getPointerAlign(0))); | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 577 | Alloc->setAlignment(allocaAlignment.getQuantity()); | 
| Douglas Gregor | d86c477 | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 578 | DeclPtr = Alloc; | 
|  | 579 | } | 
| Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 580 | } else { | 
|  | 581 | // Targets that don't support recursion emit locals as globals. | 
|  | 582 | const char *Class = | 
| John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 583 | D.getStorageClass() == SC_Register ? ".reg." : ".auto."; | 
| John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 584 | DeclPtr = CreateStaticVarDecl(D, Class, | 
|  | 585 | llvm::GlobalValue::InternalLinkage); | 
| Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 586 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 587 |  | 
| Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 588 | // FIXME: Can this happen? | 
| Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 589 | if (Ty->isVariablyModifiedType()) | 
|  | 590 | EmitVLASize(Ty); | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 591 | } else { | 
| Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 592 | EnsureInsertPoint(); | 
|  | 593 |  | 
| Anders Carlsson | 5ecb1b9 | 2009-02-09 20:41:50 +0000 | [diff] [blame] | 594 | if (!DidCallStackSave) { | 
| Anders Carlsson | 5d46315 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 595 | // Save the stack. | 
| John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 596 | llvm::Value *Stack = CreateTempAlloca(Int8PtrTy, "saved_stack"); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 597 |  | 
| Anders Carlsson | 5d46315 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 598 | llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave); | 
|  | 599 | llvm::Value *V = Builder.CreateCall(F); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 600 |  | 
| Anders Carlsson | 5d46315 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 601 | Builder.CreateStore(V, Stack); | 
| Anders Carlsson | 5ecb1b9 | 2009-02-09 20:41:50 +0000 | [diff] [blame] | 602 |  | 
|  | 603 | DidCallStackSave = true; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 604 |  | 
| John McCall | d871509 | 2010-07-21 06:13:08 +0000 | [diff] [blame] | 605 | // Push a cleanup block and restore the stack there. | 
| John McCall | 3ad32c8 | 2011-01-28 08:37:24 +0000 | [diff] [blame] | 606 | // FIXME: in general circumstances, this should be an EH cleanup. | 
| John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 607 | EHStack.pushCleanup<CallStackRestore>(NormalCleanup, Stack); | 
| Anders Carlsson | 5d46315 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 608 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 609 |  | 
| Anders Carlsson | 5d46315 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 610 | // Get the element type. | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 611 | const llvm::Type *LElemTy = ConvertTypeForMem(Ty); | 
| Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 612 | const llvm::Type *LElemPtrTy = | 
|  | 613 | LElemTy->getPointerTo(CGM.getContext().getTargetAddressSpace(Ty)); | 
| Anders Carlsson | 5d46315 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 614 |  | 
| Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 615 | llvm::Value *VLASize = EmitVLASize(Ty); | 
| Anders Carlsson | 5d46315 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 616 |  | 
|  | 617 | // Allocate memory for the array. | 
| Anders Carlsson | 41f8a13 | 2009-09-26 18:16:06 +0000 | [diff] [blame] | 618 | llvm::AllocaInst *VLA = | 
| John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 619 | Builder.CreateAlloca(llvm::Type::getInt8Ty(getLLVMContext()), VLASize, "vla"); | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 620 | VLA->setAlignment(alignment.getQuantity()); | 
| Anders Carlsson | 41f8a13 | 2009-09-26 18:16:06 +0000 | [diff] [blame] | 621 |  | 
| Eli Friedman | 8f39f5e | 2008-12-20 23:11:59 +0000 | [diff] [blame] | 622 | DeclPtr = Builder.CreateBitCast(VLA, LElemPtrTy, "tmp"); | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 623 | } | 
| Eli Friedman | 8f39f5e | 2008-12-20 23:11:59 +0000 | [diff] [blame] | 624 |  | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 625 | llvm::Value *&DMEntry = LocalDeclMap[&D]; | 
|  | 626 | assert(DMEntry == 0 && "Decl already exists in localdeclmap!"); | 
|  | 627 | DMEntry = DeclPtr; | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 628 | emission.Address = DeclPtr; | 
| Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 629 |  | 
|  | 630 | // Emit debug info for local var declaration. | 
| Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 631 | if (CGDebugInfo *DI = getDebugInfo()) { | 
| Daniel Dunbar | 25b6ebf | 2009-07-19 07:03:11 +0000 | [diff] [blame] | 632 | assert(HaveInsertPoint() && "Unexpected unreachable point!"); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 633 |  | 
| Daniel Dunbar | 66031a5 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 634 | DI->setLocation(D.getLocation()); | 
| Sanjiv Gupta | 4381d4b | 2009-05-22 13:54:25 +0000 | [diff] [blame] | 635 | if (Target.useGlobalsForAutomaticVariables()) { | 
|  | 636 | DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(DeclPtr), &D); | 
| Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 637 | } else | 
|  | 638 | DI->EmitDeclareOfAutoVariable(&D, DeclPtr, Builder); | 
| Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 639 | } | 
|  | 640 |  | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 641 | return emission; | 
|  | 642 | } | 
|  | 643 |  | 
|  | 644 | /// Determines whether the given __block variable is potentially | 
|  | 645 | /// captured by the given expression. | 
|  | 646 | static bool isCapturedBy(const VarDecl &var, const Expr *e) { | 
|  | 647 | // Skip the most common kinds of expressions that make | 
|  | 648 | // hierarchy-walking expensive. | 
|  | 649 | e = e->IgnoreParenCasts(); | 
|  | 650 |  | 
|  | 651 | if (const BlockExpr *be = dyn_cast<BlockExpr>(e)) { | 
|  | 652 | const BlockDecl *block = be->getBlockDecl(); | 
|  | 653 | for (BlockDecl::capture_const_iterator i = block->capture_begin(), | 
|  | 654 | e = block->capture_end(); i != e; ++i) { | 
|  | 655 | if (i->getVariable() == &var) | 
|  | 656 | return true; | 
|  | 657 | } | 
|  | 658 |  | 
|  | 659 | // No need to walk into the subexpressions. | 
|  | 660 | return false; | 
|  | 661 | } | 
|  | 662 |  | 
|  | 663 | for (Stmt::const_child_range children = e->children(); children; ++children) | 
|  | 664 | if (isCapturedBy(var, cast<Expr>(*children))) | 
|  | 665 | return true; | 
|  | 666 |  | 
|  | 667 | return false; | 
|  | 668 | } | 
|  | 669 |  | 
|  | 670 | void CodeGenFunction::EmitAutoVarInit(const AutoVarEmission &emission) { | 
| John McCall | 57b3b6a | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 671 | assert(emission.Variable && "emission was not valid!"); | 
|  | 672 |  | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 673 | // If this was emitted as a global constant, we're done. | 
|  | 674 | if (emission.wasEmittedAsGlobal()) return; | 
|  | 675 |  | 
| John McCall | 57b3b6a | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 676 | const VarDecl &D = *emission.Variable; | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 677 | QualType type = D.getType(); | 
|  | 678 |  | 
| Chris Lattner | 1978596 | 2007-07-12 00:39:48 +0000 | [diff] [blame] | 679 | // If this local has an initializer, emit it now. | 
| Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 680 | const Expr *Init = D.getInit(); | 
|  | 681 |  | 
|  | 682 | // If we are at an unreachable point, we don't need to emit the initializer | 
|  | 683 | // unless it contains a label. | 
|  | 684 | if (!HaveInsertPoint()) { | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 685 | if (!Init || !ContainsLabel(Init)) return; | 
|  | 686 | EnsureInsertPoint(); | 
| Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 687 | } | 
|  | 688 |  | 
| John McCall | 5af02db | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 689 | // Initialize the structure of a __block variable. | 
|  | 690 | if (emission.IsByRef) | 
|  | 691 | emitByrefStructureInit(emission); | 
| Anders Carlsson | 69c68b7 | 2009-02-07 23:51:38 +0000 | [diff] [blame] | 692 |  | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 693 | if (!Init) return; | 
| Mon P Wang | 3ecd785 | 2010-04-04 03:10:52 +0000 | [diff] [blame] | 694 |  | 
| John McCall | 5af02db | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 695 | CharUnits alignment = emission.Alignment; | 
|  | 696 |  | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 697 | // Check whether this is a byref variable that's potentially | 
|  | 698 | // captured and moved by its own initializer.  If so, we'll need to | 
|  | 699 | // emit the initializer first, then copy into the variable. | 
|  | 700 | bool capturedByInit = emission.IsByRef && isCapturedBy(D, Init); | 
|  | 701 |  | 
|  | 702 | llvm::Value *Loc = | 
|  | 703 | capturedByInit ? emission.Address : emission.getObjectAddress(*this); | 
|  | 704 |  | 
| John McCall | 60d3365 | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 705 | if (!emission.IsConstantAggregate) | 
|  | 706 | return EmitExprAsInit(Init, &D, Loc, alignment, capturedByInit); | 
|  | 707 |  | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 708 | // If this is a simple aggregate initialization, we can optimize it | 
|  | 709 | // in various ways. | 
| John McCall | 60d3365 | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 710 | assert(!capturedByInit && "constant init contains a capturing block?"); | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 711 |  | 
| John McCall | 60d3365 | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 712 | bool isVolatile = type.isVolatileQualified(); | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 713 |  | 
| John McCall | 60d3365 | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 714 | llvm::Constant *constant = CGM.EmitConstantExpr(D.getInit(), type, this); | 
|  | 715 | assert(constant != 0 && "Wasn't a simple constant init?"); | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 716 |  | 
| John McCall | 60d3365 | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 717 | llvm::Value *SizeVal = | 
|  | 718 | llvm::ConstantInt::get(IntPtrTy, | 
|  | 719 | getContext().getTypeSizeInChars(type).getQuantity()); | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 720 |  | 
| John McCall | 60d3365 | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 721 | const llvm::Type *BP = Int8PtrTy; | 
|  | 722 | if (Loc->getType() != BP) | 
|  | 723 | Loc = Builder.CreateBitCast(Loc, BP, "tmp"); | 
| Mon P Wang | 3ecd785 | 2010-04-04 03:10:52 +0000 | [diff] [blame] | 724 |  | 
| John McCall | 60d3365 | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 725 | // If the initializer is all or mostly zeros, codegen with memset then do | 
|  | 726 | // a few stores afterward. | 
|  | 727 | if (shouldUseMemSetPlusStoresToInitialize(constant, | 
|  | 728 | CGM.getTargetData().getTypeAllocSize(constant->getType()))) { | 
|  | 729 | Builder.CreateMemSet(Loc, llvm::ConstantInt::get(Int8Ty, 0), SizeVal, | 
|  | 730 | alignment.getQuantity(), isVolatile); | 
|  | 731 | if (!constant->isNullValue()) { | 
|  | 732 | Loc = Builder.CreateBitCast(Loc, constant->getType()->getPointerTo()); | 
|  | 733 | emitStoresForInitAfterMemset(constant, Loc, isVolatile, Builder); | 
| Fariborz Jahanian | 20e1c7e | 2010-03-12 21:40:43 +0000 | [diff] [blame] | 734 | } | 
| John McCall | 60d3365 | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 735 | } else { | 
|  | 736 | // Otherwise, create a temporary global with the initializer then | 
|  | 737 | // memcpy from the global to the alloca. | 
|  | 738 | std::string Name = GetStaticDeclName(*this, D, "."); | 
|  | 739 | llvm::GlobalVariable *GV = | 
|  | 740 | new llvm::GlobalVariable(CGM.getModule(), constant->getType(), true, | 
|  | 741 | llvm::GlobalValue::InternalLinkage, | 
|  | 742 | constant, Name, 0, false, 0); | 
|  | 743 | GV->setAlignment(alignment.getQuantity()); | 
|  | 744 |  | 
|  | 745 | llvm::Value *SrcPtr = GV; | 
|  | 746 | if (SrcPtr->getType() != BP) | 
|  | 747 | SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp"); | 
|  | 748 |  | 
|  | 749 | Builder.CreateMemCpy(Loc, SrcPtr, SizeVal, alignment.getQuantity(), | 
|  | 750 | isVolatile); | 
|  | 751 | } | 
|  | 752 | } | 
|  | 753 |  | 
|  | 754 | /// Emit an expression as an initializer for a variable at the given | 
|  | 755 | /// location.  The expression is not necessarily the normal | 
|  | 756 | /// initializer for the variable, and the address is not necessarily | 
|  | 757 | /// its normal location. | 
|  | 758 | /// | 
|  | 759 | /// \param init the initializing expression | 
|  | 760 | /// \param var the variable to act as if we're initializing | 
|  | 761 | /// \param loc the address to initialize; its type is a pointer | 
|  | 762 | ///   to the LLVM mapping of the variable's type | 
|  | 763 | /// \param alignment the alignment of the address | 
|  | 764 | /// \param capturedByInit true if the variable is a __block variable | 
|  | 765 | ///   whose address is potentially changed by the initializer | 
|  | 766 | void CodeGenFunction::EmitExprAsInit(const Expr *init, | 
|  | 767 | const VarDecl *var, | 
|  | 768 | llvm::Value *loc, | 
|  | 769 | CharUnits alignment, | 
|  | 770 | bool capturedByInit) { | 
|  | 771 | QualType type = var->getType(); | 
|  | 772 | bool isVolatile = type.isVolatileQualified(); | 
|  | 773 |  | 
|  | 774 | if (type->isReferenceType()) { | 
|  | 775 | RValue RV = EmitReferenceBindingToExpr(init, var); | 
|  | 776 | if (capturedByInit) loc = BuildBlockByrefAddress(loc, var); | 
|  | 777 | EmitStoreOfScalar(RV.getScalarVal(), loc, false, | 
|  | 778 | alignment.getQuantity(), type); | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 779 | } else if (!hasAggregateLLVMType(type)) { | 
| John McCall | 60d3365 | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 780 | llvm::Value *V = EmitScalarExpr(init); | 
|  | 781 | if (capturedByInit) loc = BuildBlockByrefAddress(loc, var); | 
|  | 782 | EmitStoreOfScalar(V, loc, isVolatile, alignment.getQuantity(), type); | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 783 | } else if (type->isAnyComplexType()) { | 
| John McCall | 60d3365 | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 784 | ComplexPairTy complex = EmitComplexExpr(init); | 
|  | 785 | if (capturedByInit) loc = BuildBlockByrefAddress(loc, var); | 
|  | 786 | StoreComplexToAddr(complex, loc, isVolatile); | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 787 | } else { | 
|  | 788 | // TODO: how can we delay here if D is captured by its initializer? | 
| John McCall | 60d3365 | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 789 | EmitAggExpr(init, AggValueSlot::forAddr(loc, isVolatile, true, false)); | 
| Fariborz Jahanian | 20e1c7e | 2010-03-12 21:40:43 +0000 | [diff] [blame] | 790 | } | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 791 | } | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 792 |  | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 793 | void CodeGenFunction::EmitAutoVarCleanups(const AutoVarEmission &emission) { | 
| John McCall | 57b3b6a | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 794 | assert(emission.Variable && "emission was not valid!"); | 
|  | 795 |  | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 796 | // If this was emitted as a global constant, we're done. | 
|  | 797 | if (emission.wasEmittedAsGlobal()) return; | 
|  | 798 |  | 
| John McCall | 57b3b6a | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 799 | const VarDecl &D = *emission.Variable; | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 800 |  | 
|  | 801 | // Handle C++ destruction of variables. | 
|  | 802 | if (getLangOptions().CPlusPlus) { | 
|  | 803 | QualType type = D.getType(); | 
|  | 804 | QualType baseType = getContext().getBaseElementType(type); | 
|  | 805 | if (const RecordType *RT = baseType->getAs<RecordType>()) { | 
|  | 806 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RT->getDecl()); | 
| Douglas Gregor | b5b30b9 | 2010-05-15 16:39:56 +0000 | [diff] [blame] | 807 | if (!ClassDecl->hasTrivialDestructor()) { | 
| Douglas Gregor | 3d91bbc | 2010-05-17 15:52:46 +0000 | [diff] [blame] | 808 | // Note: We suppress the destructor call when the corresponding NRVO | 
|  | 809 | // flag has been set. | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 810 |  | 
|  | 811 | // Note that for __block variables, we want to destroy the | 
|  | 812 | // original stack object, not the possible forwarded object. | 
|  | 813 | llvm::Value *Loc = emission.getObjectAddress(*this); | 
| Douglas Gregor | d86c477 | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 814 |  | 
| Douglas Gregor | 1d110e0 | 2010-07-01 14:13:13 +0000 | [diff] [blame] | 815 | const CXXDestructorDecl *D = ClassDecl->getDestructor(); | 
| Fariborz Jahanian | 6ca0b32 | 2009-08-03 20:20:07 +0000 | [diff] [blame] | 816 | assert(D && "EmitLocalBlockVarDecl - destructor is nul"); | 
| Fariborz Jahanian | 6fba746 | 2009-10-29 16:22:54 +0000 | [diff] [blame] | 817 |  | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 818 | if (type != baseType) { | 
|  | 819 | const ConstantArrayType *Array = | 
|  | 820 | getContext().getAsConstantArrayType(type); | 
|  | 821 | assert(Array && "types changed without array?"); | 
| John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 822 | EHStack.pushCleanup<CallArrayDtor>(NormalAndEHCleanup, | 
|  | 823 | D, Array, Loc); | 
| Fariborz Jahanian | 7799621 | 2009-11-04 17:57:40 +0000 | [diff] [blame] | 824 | } else { | 
| John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 825 | EHStack.pushCleanup<CallVarDtor>(NormalAndEHCleanup, | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 826 | D, emission.NRVOFlag, Loc); | 
| Fariborz Jahanian | 7799621 | 2009-11-04 17:57:40 +0000 | [diff] [blame] | 827 | } | 
| Fariborz Jahanian | 6ca0b32 | 2009-08-03 20:20:07 +0000 | [diff] [blame] | 828 | } | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 829 | } | 
| Fariborz Jahanian | 6ca0b32 | 2009-08-03 20:20:07 +0000 | [diff] [blame] | 830 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 831 |  | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 832 | // Handle the cleanup attribute. | 
| Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 833 | if (const CleanupAttr *CA = D.getAttr<CleanupAttr>()) { | 
| Anders Carlsson | 69c68b7 | 2009-02-07 23:51:38 +0000 | [diff] [blame] | 834 | const FunctionDecl *FD = CA->getFunctionDecl(); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 835 |  | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 836 | llvm::Constant *F = CGM.GetAddrOfFunction(FD); | 
| Anders Carlsson | 69c68b7 | 2009-02-07 23:51:38 +0000 | [diff] [blame] | 837 | assert(F && "Could not find function!"); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 838 |  | 
| Anders Carlsson | cabec03 | 2009-04-26 00:34:20 +0000 | [diff] [blame] | 839 | const CGFunctionInfo &Info = CGM.getTypes().getFunctionInfo(FD); | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 840 | EHStack.pushCleanup<CallCleanupFunction>(NormalAndEHCleanup, F, &Info, &D); | 
| Anders Carlsson | 69c68b7 | 2009-02-07 23:51:38 +0000 | [diff] [blame] | 841 | } | 
| Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 842 |  | 
| John McCall | 3469585 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 843 | // If this is a block variable, call _Block_object_destroy | 
|  | 844 | // (on the unforwarded address). | 
| John McCall | 5af02db | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 845 | if (emission.IsByRef) | 
|  | 846 | enterByrefCleanup(emission); | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 847 | } | 
|  | 848 |  | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 849 | /// Emit an alloca (or GlobalValue depending on target) | 
| Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 850 | /// for the specified parameter and set up LocalDeclMap. | 
| Devang Patel | 093ac46 | 2011-03-03 20:13:15 +0000 | [diff] [blame] | 851 | void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg, | 
|  | 852 | unsigned ArgNo) { | 
| Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 853 | // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl? | 
| Sanjiv Gupta | 31fc07d | 2008-10-31 09:52:39 +0000 | [diff] [blame] | 854 | assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) && | 
| Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 855 | "Invalid argument to EmitParmDecl"); | 
| John McCall | 8178df3 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 856 |  | 
|  | 857 | Arg->setName(D.getName()); | 
|  | 858 |  | 
|  | 859 | // Use better IR generation for certain implicit parameters. | 
|  | 860 | if (isa<ImplicitParamDecl>(D)) { | 
|  | 861 | // The only implicit argument a block has is its literal. | 
|  | 862 | if (BlockInfo) { | 
|  | 863 | LocalDeclMap[&D] = Arg; | 
|  | 864 |  | 
|  | 865 | if (CGDebugInfo *DI = getDebugInfo()) { | 
|  | 866 | DI->setLocation(D.getLocation()); | 
|  | 867 | DI->EmitDeclareOfBlockLiteralArgVariable(*BlockInfo, Arg, Builder); | 
|  | 868 | } | 
|  | 869 |  | 
|  | 870 | return; | 
|  | 871 | } | 
|  | 872 | } | 
|  | 873 |  | 
| Chris Lattner | 8bcfc5b | 2008-04-06 23:10:54 +0000 | [diff] [blame] | 874 | QualType Ty = D.getType(); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 875 |  | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 876 | llvm::Value *DeclPtr; | 
| Daniel Dunbar | e86bcf0 | 2010-02-08 22:53:07 +0000 | [diff] [blame] | 877 | // If this is an aggregate or variable sized value, reuse the input pointer. | 
|  | 878 | if (!Ty->isConstantSizeType() || | 
|  | 879 | CodeGenFunction::hasAggregateLLVMType(Ty)) { | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 880 | DeclPtr = Arg; | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 881 | } else { | 
| Daniel Dunbar | e86bcf0 | 2010-02-08 22:53:07 +0000 | [diff] [blame] | 882 | // Otherwise, create a temporary to hold the value. | 
| Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 883 | DeclPtr = CreateMemTemp(Ty, D.getName() + ".addr"); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 884 |  | 
| Daniel Dunbar | e86bcf0 | 2010-02-08 22:53:07 +0000 | [diff] [blame] | 885 | // Store the initial value into the alloca. | 
| John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 886 | EmitStoreOfScalar(Arg, DeclPtr, Ty.isVolatileQualified(), | 
|  | 887 | getContext().getDeclAlign(&D).getQuantity(), Ty, | 
|  | 888 | CGM.getTBAAInfo(Ty)); | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 889 | } | 
|  | 890 |  | 
|  | 891 | llvm::Value *&DMEntry = LocalDeclMap[&D]; | 
|  | 892 | assert(DMEntry == 0 && "Decl already exists in localdeclmap!"); | 
|  | 893 | DMEntry = DeclPtr; | 
| Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 894 |  | 
|  | 895 | // Emit debug info for param declaration. | 
| Devang Patel | fee53aa | 2009-10-09 22:06:15 +0000 | [diff] [blame] | 896 | if (CGDebugInfo *DI = getDebugInfo()) { | 
|  | 897 | DI->setLocation(D.getLocation()); | 
| Devang Patel | 093ac46 | 2011-03-03 20:13:15 +0000 | [diff] [blame] | 898 | DI->EmitDeclareOfArgVariable(&D, DeclPtr, ArgNo, Builder); | 
| Devang Patel | fee53aa | 2009-10-09 22:06:15 +0000 | [diff] [blame] | 899 | } | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 900 | } |