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