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