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