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