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