Chris Lattner | 84915fa | 2007-06-02 04:16:21 +0000 | [diff] [blame] | 1 | //===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Chris Lattner | 84915fa | 2007-06-02 04:16:21 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This contains code to emit Decl nodes as LLVM code. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 13 | #include "CGBlocks.h" |
Reid Kleckner | e5a321b | 2016-09-07 18:21:30 +0000 | [diff] [blame] | 14 | #include "CGCXXABI.h" |
David Majnemer | dc012fa | 2015-04-22 21:38:15 +0000 | [diff] [blame] | 15 | #include "CGCleanup.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "CGDebugInfo.h" |
Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 17 | #include "CGOpenCLRuntime.h" |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 18 | #include "CGOpenMPRuntime.h" |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 19 | #include "CodeGenFunction.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "CodeGenModule.h" |
John McCall | de0fe07 | 2017-08-15 21:42:52 +0000 | [diff] [blame] | 21 | #include "ConstantEmitter.h" |
JF Bastien | ef202c3 | 2019-04-12 00:11:27 +0000 | [diff] [blame] | 22 | #include "PatternInit.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" |
Richard Trieu | 6368818 | 2018-12-11 03:18:39 +0000 | [diff] [blame] | 29 | #include "clang/Basic/CodeGenOptions.h" |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 30 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | b781dc79 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 31 | #include "clang/Basic/TargetInfo.h" |
Mark Lacey | a8e7df3 | 2013-10-30 21:53:58 +0000 | [diff] [blame] | 32 | #include "clang/CodeGen/CGFunctionInfo.h" |
JF Bastien | 2f0582f | 2018-09-21 13:54:09 +0000 | [diff] [blame] | 33 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 34 | #include "llvm/IR/DataLayout.h" |
| 35 | #include "llvm/IR/GlobalVariable.h" |
| 36 | #include "llvm/IR/Intrinsics.h" |
| 37 | #include "llvm/IR/Type.h" |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 84915fa | 2007-06-02 04:16:21 +0000 | [diff] [blame] | 39 | using namespace clang; |
| 40 | using namespace CodeGen; |
| 41 | |
Chris Lattner | 1ad38f8 | 2007-06-09 01:20:56 +0000 | [diff] [blame] | 42 | void CodeGenFunction::EmitDecl(const Decl &D) { |
Chris Lattner | 1ad38f8 | 2007-06-09 01:20:56 +0000 | [diff] [blame] | 43 | switch (D.getKind()) { |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 44 | case Decl::BuiltinTemplate: |
Douglas Gregor | 19043f0 | 2010-04-23 02:02:43 +0000 | [diff] [blame] | 45 | case Decl::TranslationUnit: |
Richard Smith | f19e127 | 2015-03-07 00:04:49 +0000 | [diff] [blame] | 46 | case Decl::ExternCContext: |
Douglas Gregor | 19043f0 | 2010-04-23 02:02:43 +0000 | [diff] [blame] | 47 | case Decl::Namespace: |
| 48 | case Decl::UnresolvedUsingTypename: |
| 49 | case Decl::ClassTemplateSpecialization: |
| 50 | case Decl::ClassTemplatePartialSpecialization: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 51 | case Decl::VarTemplateSpecialization: |
| 52 | case Decl::VarTemplatePartialSpecialization: |
Douglas Gregor | 19043f0 | 2010-04-23 02:02:43 +0000 | [diff] [blame] | 53 | case Decl::TemplateTypeParm: |
| 54 | case Decl::UnresolvedUsingValue: |
Alexis Hunt | ed05325 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 55 | case Decl::NonTypeTemplateParm: |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 56 | case Decl::CXXDeductionGuide: |
Douglas Gregor | 19043f0 | 2010-04-23 02:02:43 +0000 | [diff] [blame] | 57 | case Decl::CXXMethod: |
| 58 | case Decl::CXXConstructor: |
| 59 | case Decl::CXXDestructor: |
| 60 | case Decl::CXXConversion: |
| 61 | case Decl::Field: |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 62 | case Decl::MSProperty: |
Francois Pichet | df946c3 | 2010-11-21 06:49:41 +0000 | [diff] [blame] | 63 | case Decl::IndirectField: |
Douglas Gregor | 19043f0 | 2010-04-23 02:02:43 +0000 | [diff] [blame] | 64 | case Decl::ObjCIvar: |
Eric Christopher | 31ab130 | 2011-08-23 22:38:00 +0000 | [diff] [blame] | 65 | case Decl::ObjCAtDefsField: |
Chris Lattner | ba9dddb | 2007-10-08 21:37:32 +0000 | [diff] [blame] | 66 | case Decl::ParmVar: |
Douglas Gregor | 19043f0 | 2010-04-23 02:02:43 +0000 | [diff] [blame] | 67 | case Decl::ImplicitParam: |
| 68 | case Decl::ClassTemplate: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 69 | case Decl::VarTemplate: |
Douglas Gregor | 19043f0 | 2010-04-23 02:02:43 +0000 | [diff] [blame] | 70 | case Decl::FunctionTemplate: |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 71 | case Decl::TypeAliasTemplate: |
Douglas Gregor | 19043f0 | 2010-04-23 02:02:43 +0000 | [diff] [blame] | 72 | case Decl::TemplateTemplateParm: |
| 73 | case Decl::ObjCMethod: |
| 74 | case Decl::ObjCCategory: |
| 75 | case Decl::ObjCProtocol: |
| 76 | case Decl::ObjCInterface: |
| 77 | case Decl::ObjCCategoryImpl: |
| 78 | case Decl::ObjCImplementation: |
| 79 | case Decl::ObjCProperty: |
| 80 | case Decl::ObjCCompatibleAlias: |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 81 | case Decl::PragmaComment: |
Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 82 | case Decl::PragmaDetectMismatch: |
Abramo Bagnara | d734058 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 83 | case Decl::AccessSpec: |
Douglas Gregor | 19043f0 | 2010-04-23 02:02:43 +0000 | [diff] [blame] | 84 | case Decl::LinkageSpec: |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 85 | case Decl::Export: |
Douglas Gregor | 19043f0 | 2010-04-23 02:02:43 +0000 | [diff] [blame] | 86 | case Decl::ObjCPropertyImpl: |
Douglas Gregor | 19043f0 | 2010-04-23 02:02:43 +0000 | [diff] [blame] | 87 | case Decl::FileScopeAsm: |
| 88 | case Decl::Friend: |
| 89 | case Decl::FriendTemplate: |
| 90 | case Decl::Block: |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 91 | case Decl::Captured: |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 92 | case Decl::ClassScopeFunctionSpecialization: |
David Blaikie | bd48376 | 2013-05-20 04:58:53 +0000 | [diff] [blame] | 93 | case Decl::UsingShadow: |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 94 | case Decl::ConstructorUsingShadow: |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 95 | case Decl::ObjCTypeParam: |
Richard Smith | da38363 | 2016-08-15 01:33:41 +0000 | [diff] [blame] | 96 | case Decl::Binding: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 97 | llvm_unreachable("Declaration should not be in declstmts!"); |
Amjad Aboud | dc4531e | 2016-04-30 01:44:38 +0000 | [diff] [blame] | 98 | case Decl::Function: // void X(); |
| 99 | case Decl::Record: // struct/union/class X; |
| 100 | case Decl::Enum: // enum X; |
| 101 | case Decl::EnumConstant: // enum ? { X = ? } |
| 102 | case Decl::CXXRecord: // struct/union/class X; [C++] |
Anders Carlsson | ce2cd01 | 2009-12-03 17:26:31 +0000 | [diff] [blame] | 103 | case Decl::StaticAssert: // static_assert(X, ""); [C++0x] |
Chris Lattner | 43e7f31 | 2011-02-18 02:08:43 +0000 | [diff] [blame] | 104 | case Decl::Label: // __label__ x; |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 105 | case Decl::Import: |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 106 | case Decl::OMPThreadPrivate: |
Alexey Bataev | 25ed0c0 | 2019-03-07 17:54:44 +0000 | [diff] [blame] | 107 | case Decl::OMPAllocate: |
Alexey Bataev | 4244be2 | 2016-02-11 05:35:55 +0000 | [diff] [blame] | 108 | case Decl::OMPCapturedExpr: |
Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 109 | case Decl::OMPRequires: |
Michael Han | 8432435 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 110 | case Decl::Empty: |
Saar Raz | d7aae33 | 2019-07-10 21:25:49 +0000 | [diff] [blame] | 111 | case Decl::Concept: |
Chris Lattner | 84915fa | 2007-06-02 04:16:21 +0000 | [diff] [blame] | 112 | // None of these decls require codegen support. |
| 113 | return; |
David Blaikie | b7d1e1f | 2013-02-02 00:39:32 +0000 | [diff] [blame] | 114 | |
David Blaikie | f121b93 | 2013-05-20 22:50:41 +0000 | [diff] [blame] | 115 | case Decl::NamespaceAlias: |
| 116 | if (CGDebugInfo *DI = getDebugInfo()) |
Amjad Aboud | dc4531e | 2016-04-30 01:44:38 +0000 | [diff] [blame] | 117 | DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(D)); |
David Blaikie | f121b93 | 2013-05-20 22:50:41 +0000 | [diff] [blame] | 118 | return; |
David Blaikie | bd48376 | 2013-05-20 04:58:53 +0000 | [diff] [blame] | 119 | case Decl::Using: // using X; [C++] |
| 120 | if (CGDebugInfo *DI = getDebugInfo()) |
Amjad Aboud | dc4531e | 2016-04-30 01:44:38 +0000 | [diff] [blame] | 121 | DI->EmitUsingDecl(cast<UsingDecl>(D)); |
David Blaikie | bd48376 | 2013-05-20 04:58:53 +0000 | [diff] [blame] | 122 | return; |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 123 | case Decl::UsingPack: |
| 124 | for (auto *Using : cast<UsingPackDecl>(D).expansions()) |
| 125 | EmitDecl(*Using); |
| 126 | return; |
David Blaikie | 9f88fe8 | 2013-04-22 06:13:21 +0000 | [diff] [blame] | 127 | case Decl::UsingDirective: // using namespace X; [C++] |
| 128 | if (CGDebugInfo *DI = getDebugInfo()) |
| 129 | DI->EmitUsingDirective(cast<UsingDirectiveDecl>(D)); |
| 130 | return; |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 131 | case Decl::Var: |
| 132 | case Decl::Decomposition: { |
Daniel Dunbar | a799807 | 2008-08-29 17:28:43 +0000 | [diff] [blame] | 133 | const VarDecl &VD = cast<VarDecl>(D); |
John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 134 | assert(VD.isLocalVarDecl() && |
Daniel Dunbar | a799807 | 2008-08-29 17:28:43 +0000 | [diff] [blame] | 135 | "Should not see file-scope variables inside a function!"); |
Richard Smith | da38363 | 2016-08-15 01:33:41 +0000 | [diff] [blame] | 136 | EmitVarDecl(VD); |
| 137 | if (auto *DD = dyn_cast<DecompositionDecl>(&VD)) |
| 138 | for (auto *B : DD->bindings()) |
| 139 | if (auto *HD = B->getHoldingVar()) |
| 140 | EmitVarDecl(*HD); |
| 141 | return; |
Chris Lattner | 84915fa | 2007-06-02 04:16:21 +0000 | [diff] [blame] | 142 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 144 | case Decl::OMPDeclareReduction: |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 145 | return CGM.EmitOMPDeclareReduction(cast<OMPDeclareReductionDecl>(&D), this); |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 146 | |
Michael Kruse | 251e148 | 2019-02-01 20:25:04 +0000 | [diff] [blame] | 147 | case Decl::OMPDeclareMapper: |
| 148 | return CGM.EmitOMPDeclareMapper(cast<OMPDeclareMapperDecl>(&D), this); |
| 149 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 150 | case Decl::Typedef: // typedef int X; |
| 151 | case Decl::TypeAlias: { // using X = int; [C++0x] |
| 152 | const TypedefNameDecl &TD = cast<TypedefNameDecl>(D); |
Anders Carlsson | 5d985f5 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 153 | QualType Ty = TD.getUnderlyingType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 154 | |
Anders Carlsson | 5d985f5 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 155 | if (Ty->isVariablyModifiedType()) |
John McCall | 23c29fe | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 156 | EmitVariablyModifiedType(Ty); |
Hamza Sood | d14003d | 2019-07-05 14:36:08 +0000 | [diff] [blame] | 157 | |
| 158 | return; |
Anders Carlsson | 5d985f5 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 159 | } |
Daniel Dunbar | a799807 | 2008-08-29 17:28:43 +0000 | [diff] [blame] | 160 | } |
Chris Lattner | 84915fa | 2007-06-02 04:16:21 +0000 | [diff] [blame] | 161 | } |
Chris Lattner | 03df122 | 2007-06-02 04:53:11 +0000 | [diff] [blame] | 162 | |
John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 163 | /// EmitVarDecl - This method handles emission of any variable declaration |
Chris Lattner | 03df122 | 2007-06-02 04:53:11 +0000 | [diff] [blame] | 164 | /// inside a function, including static vars etc. |
John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 165 | void CodeGenFunction::EmitVarDecl(const VarDecl &D) { |
Yaxun Liu | 4f33b3d | 2017-05-15 14:47:47 +0000 | [diff] [blame] | 166 | if (D.hasExternalStorage()) |
| 167 | // Don't emit it now, allow it to be emitted lazily on its first use. |
| 168 | return; |
| 169 | |
| 170 | // Some function-scope variable does not have static storage but still |
| 171 | // needs to be emitted like a static variable, e.g. a function-scope |
| 172 | // variable in constant address space in OpenCL. |
| 173 | if (D.getStorageDuration() != SD_Automatic) { |
Alexey Bader | bed4009 | 2017-11-15 11:38:17 +0000 | [diff] [blame] | 174 | // Static sampler variables translated to function calls. |
| 175 | if (D.getType()->isSamplerT()) |
| 176 | return; |
| 177 | |
Eric Christopher | 31ab130 | 2011-08-23 22:38:00 +0000 | [diff] [blame] | 178 | llvm::GlobalValue::LinkageTypes Linkage = |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 179 | CGM.getLLVMLinkageVarDefinition(&D, /*IsConstant=*/false); |
Anders Carlsson | cee2d2f | 2010-02-07 02:03:08 +0000 | [diff] [blame] | 180 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 181 | // FIXME: We need to force the emission/use of a guard variable for |
| 182 | // some variables even if we can constant-evaluate them because |
| 183 | // we can't guarantee every translation unit will constant-evaluate them. |
Eric Christopher | 31ab130 | 2011-08-23 22:38:00 +0000 | [diff] [blame] | 184 | |
John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 185 | return EmitStaticVarDecl(D, Linkage); |
Anders Carlsson | cee2d2f | 2010-02-07 02:03:08 +0000 | [diff] [blame] | 186 | } |
Enea Zaffanella | cf51a8a | 2013-05-16 11:27:56 +0000 | [diff] [blame] | 187 | |
Anastasia Stulova | bcea696 | 2015-09-30 14:08:20 +0000 | [diff] [blame] | 188 | if (D.getType().getAddressSpace() == LangAS::opencl_local) |
Enea Zaffanella | cf51a8a | 2013-05-16 11:27:56 +0000 | [diff] [blame] | 189 | return CGM.getOpenCLRuntime().EmitWorkGroupLocalVarDecl(*this, D); |
| 190 | |
| 191 | assert(D.hasLocalStorage()); |
| 192 | return EmitAutoVarDecl(D); |
Chris Lattner | 03df122 | 2007-06-02 04:53:11 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Reid Kleckner | 453e056 | 2014-10-08 01:07:54 +0000 | [diff] [blame] | 195 | static std::string getStaticDeclName(CodeGenModule &CGM, const VarDecl &D) { |
| 196 | if (CGM.getLangOpts().CPlusPlus) |
Alp Toker | fb8d02b | 2014-06-05 22:10:59 +0000 | [diff] [blame] | 197 | return CGM.getMangledName(&D).str(); |
| 198 | |
Reid Kleckner | 453e056 | 2014-10-08 01:07:54 +0000 | [diff] [blame] | 199 | // If this isn't C++, we don't need a mangled name, just a pretty one. |
| 200 | assert(!D.isExternallyVisible() && "name shouldn't matter"); |
| 201 | std::string ContextName; |
| 202 | const DeclContext *DC = D.getDeclContext(); |
Alexey Bataev | 43f7439 | 2015-05-07 06:28:46 +0000 | [diff] [blame] | 203 | if (auto *CD = dyn_cast<CapturedDecl>(DC)) |
| 204 | DC = cast<DeclContext>(CD->getNonClosureContext()); |
Reid Kleckner | 453e056 | 2014-10-08 01:07:54 +0000 | [diff] [blame] | 205 | if (const auto *FD = dyn_cast<FunctionDecl>(DC)) |
Alp Toker | fb8d02b | 2014-06-05 22:10:59 +0000 | [diff] [blame] | 206 | ContextName = CGM.getMangledName(FD); |
Reid Kleckner | 453e056 | 2014-10-08 01:07:54 +0000 | [diff] [blame] | 207 | else if (const auto *BD = dyn_cast<BlockDecl>(DC)) |
| 208 | ContextName = CGM.getBlockMangledName(GlobalDecl(), BD); |
| 209 | else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(DC)) |
| 210 | ContextName = OMD->getSelector().getAsString(); |
Chris Lattner | e99c110 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 211 | else |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 212 | llvm_unreachable("Unknown context for static var decl"); |
Eric Christopher | 31ab130 | 2011-08-23 22:38:00 +0000 | [diff] [blame] | 213 | |
Reid Kleckner | 453e056 | 2014-10-08 01:07:54 +0000 | [diff] [blame] | 214 | ContextName += "." + D.getNameAsString(); |
| 215 | return ContextName; |
Chris Lattner | e99c110 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Reid Kleckner | 453e056 | 2014-10-08 01:07:54 +0000 | [diff] [blame] | 218 | llvm::Constant *CodeGenModule::getOrCreateStaticVarDecl( |
| 219 | const VarDecl &D, llvm::GlobalValue::LinkageTypes Linkage) { |
| 220 | // In general, we don't always emit static var decls once before we reference |
| 221 | // them. It is possible to reference them before emitting the function that |
| 222 | // contains them, and it is possible to emit the containing function multiple |
| 223 | // times. |
| 224 | if (llvm::Constant *ExistingGV = StaticLocalDeclMap[&D]) |
| 225 | return ExistingGV; |
| 226 | |
Chris Lattner | fc4379f | 2008-04-06 23:10:54 +0000 | [diff] [blame] | 227 | QualType Ty = D.getType(); |
Eli Friedman | a682d39 | 2008-02-15 12:20:59 +0000 | [diff] [blame] | 228 | assert(Ty->isConstantSizeType() && "VLAs can't be static"); |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 229 | |
Benjamin Kramer | ddbb2b8 | 2011-11-20 21:05:04 +0000 | [diff] [blame] | 230 | // Use the label if the variable is renamed with the asm-label extension. |
| 231 | std::string Name; |
Benjamin Kramer | 5642e19 | 2011-11-21 15:47:23 +0000 | [diff] [blame] | 232 | if (D.hasAttr<AsmLabelAttr>()) |
Reid Kleckner | 453e056 | 2014-10-08 01:07:54 +0000 | [diff] [blame] | 233 | Name = getMangledName(&D); |
Benjamin Kramer | 5642e19 | 2011-11-21 15:47:23 +0000 | [diff] [blame] | 234 | else |
Reid Kleckner | 453e056 | 2014-10-08 01:07:54 +0000 | [diff] [blame] | 235 | Name = getStaticDeclName(*this, D); |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 236 | |
Reid Kleckner | 453e056 | 2014-10-08 01:07:54 +0000 | [diff] [blame] | 237 | llvm::Type *LTy = getTypes().ConvertTypeForMem(Ty); |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 238 | LangAS AS = GetGlobalVarAddressSpace(&D); |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 239 | unsigned TargetAS = getContext().getTargetAddressSpace(AS); |
Matt Arsenault | 3f6469b | 2014-11-03 16:51:53 +0000 | [diff] [blame] | 240 | |
Yaxun Liu | a64a491 | 2018-04-02 17:38:24 +0000 | [diff] [blame] | 241 | // OpenCL variables in local address space and CUDA shared |
| 242 | // variables cannot have an initializer. |
Matt Arsenault | 3f6469b | 2014-11-03 16:51:53 +0000 | [diff] [blame] | 243 | llvm::Constant *Init = nullptr; |
Yaxun Liu | a64a491 | 2018-04-02 17:38:24 +0000 | [diff] [blame] | 244 | if (Ty.getAddressSpace() == LangAS::opencl_local || |
| 245 | D.hasAttr<CUDASharedAttr>()) |
Matt Arsenault | 3f6469b | 2014-11-03 16:51:53 +0000 | [diff] [blame] | 246 | Init = llvm::UndefValue::get(LTy); |
Yaxun Liu | a64a491 | 2018-04-02 17:38:24 +0000 | [diff] [blame] | 247 | else |
| 248 | Init = EmitNullConstant(Ty); |
Matt Arsenault | 3f6469b | 2014-11-03 16:51:53 +0000 | [diff] [blame] | 249 | |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 250 | llvm::GlobalVariable *GV = new llvm::GlobalVariable( |
| 251 | getModule(), LTy, Ty.isConstant(getContext()), Linkage, Init, Name, |
| 252 | nullptr, llvm::GlobalVariable::NotThreadLocal, TargetAS); |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 253 | GV->setAlignment(getContext().getDeclAlign(&D).getQuantity()); |
Hans Wennborg | f60f6af | 2012-06-28 08:01:44 +0000 | [diff] [blame] | 254 | |
NAKAMURA Takumi | c7da6da | 2015-05-09 21:10:07 +0000 | [diff] [blame] | 255 | if (supportsCOMDAT() && GV->isWeakForLinker()) |
| 256 | GV->setComdat(TheModule.getOrInsertComdat(GV->getName())); |
Rafael Espindola | 0d4fb98 | 2015-01-12 22:13:53 +0000 | [diff] [blame] | 257 | |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 258 | if (D.getTLSKind()) |
Reid Kleckner | 453e056 | 2014-10-08 01:07:54 +0000 | [diff] [blame] | 259 | setTLSMode(GV, D); |
Hans Wennborg | f60f6af | 2012-06-28 08:01:44 +0000 | [diff] [blame] | 260 | |
Rafael Espindola | 922f2aa | 2018-02-23 19:30:48 +0000 | [diff] [blame] | 261 | setGVProperties(GV, &D); |
| 262 | |
Eli Bendersky | cb39943 | 2014-03-24 22:05:38 +0000 | [diff] [blame] | 263 | // Make sure the result is of the correct type. |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 264 | LangAS ExpectedAS = Ty.getAddressSpace(); |
Reid Kleckner | 453e056 | 2014-10-08 01:07:54 +0000 | [diff] [blame] | 265 | llvm::Constant *Addr = GV; |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 266 | if (AS != ExpectedAS) { |
| 267 | Addr = getTargetCodeGenInfo().performAddrSpaceCast( |
| 268 | *this, GV, AS, ExpectedAS, |
| 269 | LTy->getPointerTo(getContext().getTargetAddressSpace(ExpectedAS))); |
Eli Bendersky | cb39943 | 2014-03-24 22:05:38 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Reid Kleckner | 453e056 | 2014-10-08 01:07:54 +0000 | [diff] [blame] | 272 | setStaticLocalDeclAddress(&D, Addr); |
| 273 | |
| 274 | // Ensure that the static local gets initialized by making sure the parent |
| 275 | // function gets emitted eventually. |
| 276 | const Decl *DC = cast<Decl>(D.getDeclContext()); |
| 277 | |
| 278 | // We can't name blocks or captured statements directly, so try to emit their |
| 279 | // parents. |
| 280 | if (isa<BlockDecl>(DC) || isa<CapturedDecl>(DC)) { |
| 281 | DC = DC->getNonClosureContext(); |
| 282 | // FIXME: Ensure that global blocks get emitted. |
| 283 | if (!DC) |
| 284 | return Addr; |
| 285 | } |
| 286 | |
| 287 | GlobalDecl GD; |
| 288 | if (const auto *CD = dyn_cast<CXXConstructorDecl>(DC)) |
| 289 | GD = GlobalDecl(CD, Ctor_Base); |
| 290 | else if (const auto *DD = dyn_cast<CXXDestructorDecl>(DC)) |
| 291 | GD = GlobalDecl(DD, Dtor_Base); |
| 292 | else if (const auto *FD = dyn_cast<FunctionDecl>(DC)) |
| 293 | GD = GlobalDecl(FD); |
| 294 | else { |
| 295 | // Don't do anything for Obj-C method decls or global closures. We should |
| 296 | // never defer them. |
| 297 | assert(isa<ObjCMethodDecl>(DC) && "unexpected parent code decl"); |
| 298 | } |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 299 | if (GD.getDecl()) { |
| 300 | // Disable emission of the parent function for the OpenMP device codegen. |
| 301 | CGOpenMPRuntime::DisableAutoDeclareTargetRAII NoDeclTarget(*this); |
Reid Kleckner | 453e056 | 2014-10-08 01:07:54 +0000 | [diff] [blame] | 302 | (void)GetAddrOfGlobal(GD); |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 303 | } |
Reid Kleckner | 453e056 | 2014-10-08 01:07:54 +0000 | [diff] [blame] | 304 | |
| 305 | return Addr; |
Daniel Dunbar | 22a87f9 | 2009-02-25 19:24:29 +0000 | [diff] [blame] | 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()); |
Rafael Espindola | 699f5d6 | 2018-02-07 22:15:33 +0000 | [diff] [blame] | 347 | GV->setDSOLocal(OldGV->isDSOLocal()); |
Reid Kleckner | eab97d3 | 2015-07-20 20:35:30 +0000 | [diff] [blame] | 348 | GV->setComdat(OldGV->getComdat()); |
Eric Christopher | 31ab130 | 2011-08-23 22:38:00 +0000 | [diff] [blame] | 349 | |
Chris Lattner | e99c110 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 350 | // Steal the name of the old global |
Chandler Carruth | 8453795 | 2012-03-30 19:44:53 +0000 | [diff] [blame] | 351 | GV->takeName(OldGV); |
Eric Christopher | 31ab130 | 2011-08-23 22:38:00 +0000 | [diff] [blame] | 352 | |
Chris Lattner | e99c110 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 353 | // Replace all uses of the old global with the new global |
Chandler Carruth | 8453795 | 2012-03-30 19:44:53 +0000 | [diff] [blame] | 354 | llvm::Constant *NewPtrForOldDecl = |
| 355 | llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); |
| 356 | OldGV->replaceAllUsesWith(NewPtrForOldDecl); |
Eric Christopher | 31ab130 | 2011-08-23 22:38:00 +0000 | [diff] [blame] | 357 | |
Chris Lattner | e99c110 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 358 | // Erase the old global, since it is no longer used. |
Chandler Carruth | 8453795 | 2012-03-30 19:44:53 +0000 | [diff] [blame] | 359 | OldGV->eraseFromParent(); |
Chris Lattner | e99c110 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 360 | } |
Eric Christopher | 31ab130 | 2011-08-23 22:38:00 +0000 | [diff] [blame] | 361 | |
Chandler Carruth | 8453795 | 2012-03-30 19:44:53 +0000 | [diff] [blame] | 362 | GV->setConstant(CGM.isTypeConstant(D.getType(), true)); |
| 363 | GV->setInitializer(Init); |
Richard Smith | 6331c40 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 364 | |
John McCall | de0fe07 | 2017-08-15 21:42:52 +0000 | [diff] [blame] | 365 | emitter.finalize(GV); |
| 366 | |
Richard Smith | 2b4fa53 | 2019-09-29 05:08:46 +0000 | [diff] [blame] | 367 | if (D.needsDestruction(getContext()) && HaveInsertPoint()) { |
Richard Smith | 6331c40 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 368 | // We have a constant initializer, but a nontrivial destructor. We still |
| 369 | // need to perform a guarded "initialization" in order to register the |
Richard Smith | e070fd2 | 2012-02-17 06:48:11 +0000 | [diff] [blame] | 370 | // destructor. |
Chandler Carruth | 8453795 | 2012-03-30 19:44:53 +0000 | [diff] [blame] | 371 | EmitCXXGuardedInit(D, GV, /*PerformInit*/false); |
Richard Smith | 6331c40 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Chandler Carruth | 8453795 | 2012-03-30 19:44:53 +0000 | [diff] [blame] | 374 | return GV; |
Chris Lattner | e99c110 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 375 | } |
| 376 | |
John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 377 | void CodeGenFunction::EmitStaticVarDecl(const VarDecl &D, |
Anders Carlsson | cee2d2f | 2010-02-07 02:03:08 +0000 | [diff] [blame] | 378 | llvm::GlobalValue::LinkageTypes Linkage) { |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 379 | // Check to see if we already have a global variable for this |
| 380 | // declaration. This can happen when double-emitting function |
| 381 | // bodies, e.g. with complete and base constructors. |
Reid Kleckner | 453e056 | 2014-10-08 01:07:54 +0000 | [diff] [blame] | 382 | llvm::Constant *addr = CGM.getOrCreateStaticVarDecl(D, Linkage); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 383 | CharUnits alignment = getContext().getDeclAlign(&D); |
Daniel Dunbar | a374e60 | 2009-02-25 19:45:19 +0000 | [diff] [blame] | 384 | |
Daniel Dunbar | 1cdbc54 | 2009-02-25 20:08:33 +0000 | [diff] [blame] | 385 | // Store into LocalDeclMap before generating initializer to handle |
| 386 | // circular references. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 387 | setAddrOfLocalVar(&D, Address(addr, alignment)); |
Daniel Dunbar | 1cdbc54 | 2009-02-25 20:08:33 +0000 | [diff] [blame] | 388 | |
John McCall | 4a39ab8 | 2010-05-04 20:45:42 +0000 | [diff] [blame] | 389 | // We can't have a VLA here, but we can have a pointer to a VLA, |
| 390 | // even though that doesn't really make any sense. |
Eli Friedman | bc633be | 2009-04-20 03:54:15 +0000 | [diff] [blame] | 391 | // Make sure to evaluate VLA bounds now so that we have them for later. |
| 392 | if (D.getType()->isVariablyModifiedType()) |
John McCall | 23c29fe | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 393 | EmitVariablyModifiedType(D.getType()); |
Eric Christopher | 31ab130 | 2011-08-23 22:38:00 +0000 | [diff] [blame] | 394 | |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 395 | // Save the type in case adding the initializer forces a type change. |
| 396 | llvm::Type *expectedType = addr->getType(); |
Eli Friedman | bc633be | 2009-04-20 03:54:15 +0000 | [diff] [blame] | 397 | |
Eli Bendersky | cb39943 | 2014-03-24 22:05:38 +0000 | [diff] [blame] | 398 | llvm::GlobalVariable *var = |
| 399 | cast<llvm::GlobalVariable>(addr->stripPointerCasts()); |
Artem Belevich | 4d430ba | 2016-05-09 22:09:56 +0000 | [diff] [blame] | 400 | |
| 401 | // CUDA's local and local static __shared__ variables should not |
| 402 | // have any non-empty initializers. This is ensured by Sema. |
| 403 | // Whatever initializer such variable may have when it gets here is |
| 404 | // a no-op and should not be emitted. |
| 405 | bool isCudaSharedVar = getLangOpts().CUDA && getLangOpts().CUDAIsDevice && |
| 406 | D.hasAttr<CUDASharedAttr>(); |
Chris Lattner | e99c110 | 2009-12-05 08:22:11 +0000 | [diff] [blame] | 407 | // If this value has an initializer, emit it. |
Artem Belevich | 4d430ba | 2016-05-09 22:09:56 +0000 | [diff] [blame] | 408 | if (D.getInit() && !isCudaSharedVar) |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 409 | var = AddInitializerToStaticVarDecl(D, var); |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 410 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 411 | var->setAlignment(alignment.getQuantity()); |
Chris Lattner | 4d94109 | 2010-03-10 23:59:59 +0000 | [diff] [blame] | 412 | |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 413 | if (D.hasAttr<AnnotateAttr>()) |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 414 | CGM.AddGlobalAnnotations(&D, var); |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 415 | |
Javed Absar | 2a67c9e | 2017-06-05 10:11:57 +0000 | [diff] [blame] | 416 | if (auto *SA = D.getAttr<PragmaClangBSSSectionAttr>()) |
| 417 | var->addAttribute("bss-section", SA->getName()); |
| 418 | if (auto *SA = D.getAttr<PragmaClangDataSectionAttr>()) |
| 419 | var->addAttribute("data-section", SA->getName()); |
| 420 | if (auto *SA = D.getAttr<PragmaClangRodataSectionAttr>()) |
| 421 | var->addAttribute("rodata-section", SA->getName()); |
| 422 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 423 | if (const SectionAttr *SA = D.getAttr<SectionAttr>()) |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 424 | var->setSection(SA->getName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 425 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 426 | if (D.hasAttr<UsedAttr>()) |
Rafael Espindola | 060062a | 2014-03-06 22:15:10 +0000 | [diff] [blame] | 427 | CGM.addUsedGlobal(var); |
Daniel Dunbar | 128a138 | 2009-02-13 22:08:43 +0000 | [diff] [blame] | 428 | |
Chandler Carruth | 8453795 | 2012-03-30 19:44:53 +0000 | [diff] [blame] | 429 | // We may have to cast the constant because of the initializer |
| 430 | // mismatch above. |
| 431 | // |
| 432 | // FIXME: It is really dangerous to store this in the map; if anyone |
| 433 | // RAUW's the GV uses of this constant will be invalid. |
Eli Bendersky | cb39943 | 2014-03-24 22:05:38 +0000 | [diff] [blame] | 434 | llvm::Constant *castedAddr = |
| 435 | llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(var, expectedType); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 436 | if (var != castedAddr) |
| 437 | LocalDeclMap.find(&D)->second = Address(castedAddr, alignment); |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 438 | CGM.setStaticLocalDeclAddress(&D, castedAddr); |
Sanjiv Gupta | 158143a | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 439 | |
Alexey Samsonov | 4b8de11 | 2014-08-01 21:35:28 +0000 | [diff] [blame] | 440 | CGM.getSanitizerMetadata()->reportGlobalToASan(var, D); |
Alexey Samsonov | 4f319cc | 2014-07-02 16:54:41 +0000 | [diff] [blame] | 441 | |
Sanjiv Gupta | 158143a | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 442 | // Emit global variable debug descriptor for static vars. |
Anders Carlsson | 63784f4 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 443 | CGDebugInfo *DI = getDebugInfo(); |
Alexey Samsonov | 74a3868 | 2012-05-04 07:39:27 +0000 | [diff] [blame] | 444 | if (DI && |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 445 | CGM.getCodeGenOpts().getDebugInfo() >= codegenoptions::LimitedDebugInfo) { |
Daniel Dunbar | b9fd902 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 446 | DI->setLocation(D.getLocation()); |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 447 | DI->EmitGlobalVariable(var, &D); |
Sanjiv Gupta | 158143a | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 448 | } |
Anders Carlsson | f94cd1f | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 449 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 450 | |
John McCall | 2b7fc38 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 451 | namespace { |
David Blaikie | 7e70d68 | 2015-08-18 22:40:54 +0000 | [diff] [blame] | 452 | struct DestroyObject final : EHScopeStack::Cleanup { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 453 | DestroyObject(Address addr, QualType type, |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 454 | CodeGenFunction::Destroyer *destroyer, |
| 455 | bool useEHCleanupForArray) |
Peter Collingbourne | 1425b45 | 2012-01-26 03:33:36 +0000 | [diff] [blame] | 456 | : addr(addr), type(type), destroyer(destroyer), |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 457 | useEHCleanupForArray(useEHCleanupForArray) {} |
John McCall | 2b7fc38 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 458 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 459 | Address addr; |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 460 | QualType type; |
Peter Collingbourne | 1425b45 | 2012-01-26 03:33:36 +0000 | [diff] [blame] | 461 | CodeGenFunction::Destroyer *destroyer; |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 462 | bool useEHCleanupForArray; |
John McCall | 2b7fc38 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 463 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 464 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 465 | // Don't use an EH cleanup recursively from an EH cleanup. |
John McCall | 30317fd | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 466 | bool useEHCleanupForArray = |
| 467 | flags.isForNormalCleanup() && this->useEHCleanupForArray; |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 468 | |
| 469 | CGF.emitDestroy(addr, type, destroyer, useEHCleanupForArray); |
John McCall | 2b7fc38 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 470 | } |
| 471 | }; |
| 472 | |
Akira Hatanaka | 673af7a | 2018-03-29 17:56:24 +0000 | [diff] [blame] | 473 | template <class Derived> |
| 474 | struct DestroyNRVOVariable : EHScopeStack::Cleanup { |
Marco Antognini | 8855963 | 2019-07-22 09:39:13 +0000 | [diff] [blame] | 475 | DestroyNRVOVariable(Address addr, QualType type, llvm::Value *NRVOFlag) |
| 476 | : NRVOFlag(NRVOFlag), Loc(addr), Ty(type) {} |
John McCall | 2b7fc38 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 477 | |
John McCall | 2b7fc38 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 478 | llvm::Value *NRVOFlag; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 479 | Address Loc; |
Marco Antognini | 8855963 | 2019-07-22 09:39:13 +0000 | [diff] [blame] | 480 | QualType Ty; |
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 | |
Akira Hatanaka | 673af7a | 2018-03-29 17:56:24 +0000 | [diff] [blame] | 497 | static_cast<Derived *>(this)->emitDestructorCall(CGF); |
John McCall | 2b7fc38 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 498 | |
| 499 | if (NRVO) CGF.EmitBlock(SkipDtorBB); |
| 500 | } |
Akira Hatanaka | 673af7a | 2018-03-29 17:56:24 +0000 | [diff] [blame] | 501 | |
| 502 | virtual ~DestroyNRVOVariable() = default; |
| 503 | }; |
| 504 | |
| 505 | struct DestroyNRVOVariableCXX final |
| 506 | : DestroyNRVOVariable<DestroyNRVOVariableCXX> { |
Marco Antognini | 8855963 | 2019-07-22 09:39:13 +0000 | [diff] [blame] | 507 | DestroyNRVOVariableCXX(Address addr, QualType type, |
| 508 | const CXXDestructorDecl *Dtor, llvm::Value *NRVOFlag) |
| 509 | : DestroyNRVOVariable<DestroyNRVOVariableCXX>(addr, type, NRVOFlag), |
| 510 | Dtor(Dtor) {} |
Akira Hatanaka | 673af7a | 2018-03-29 17:56:24 +0000 | [diff] [blame] | 511 | |
| 512 | const CXXDestructorDecl *Dtor; |
| 513 | |
| 514 | void emitDestructorCall(CodeGenFunction &CGF) { |
| 515 | CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, |
| 516 | /*ForVirtualBase=*/false, |
Marco Antognini | 8855963 | 2019-07-22 09:39:13 +0000 | [diff] [blame] | 517 | /*Delegating=*/false, Loc, Ty); |
Akira Hatanaka | 673af7a | 2018-03-29 17:56:24 +0000 | [diff] [blame] | 518 | } |
| 519 | }; |
| 520 | |
| 521 | struct DestroyNRVOVariableC final |
| 522 | : DestroyNRVOVariable<DestroyNRVOVariableC> { |
| 523 | DestroyNRVOVariableC(Address addr, llvm::Value *NRVOFlag, QualType Ty) |
Marco Antognini | 8855963 | 2019-07-22 09:39:13 +0000 | [diff] [blame] | 524 | : DestroyNRVOVariable<DestroyNRVOVariableC>(addr, Ty, NRVOFlag) {} |
Akira Hatanaka | 673af7a | 2018-03-29 17:56:24 +0000 | [diff] [blame] | 525 | |
| 526 | void emitDestructorCall(CodeGenFunction &CGF) { |
| 527 | CGF.destroyNonTrivialCStruct(CGF, Loc, Ty); |
| 528 | } |
John McCall | 2b7fc38 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 529 | }; |
John McCall | 2b7fc38 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 530 | |
David Blaikie | 7e70d68 | 2015-08-18 22:40:54 +0000 | [diff] [blame] | 531 | struct CallStackRestore final : EHScopeStack::Cleanup { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 532 | Address Stack; |
| 533 | CallStackRestore(Address Stack) : Stack(Stack) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 534 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 535 | llvm::Value *V = CGF.Builder.CreateLoad(Stack); |
James Y Knight | 8799cae | 2019-02-03 21:53:49 +0000 | [diff] [blame] | 536 | llvm::Function *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stackrestore); |
John McCall | a464ff9 | 2010-07-21 06:13:08 +0000 | [diff] [blame] | 537 | CGF.Builder.CreateCall(F, V); |
| 538 | } |
| 539 | }; |
| 540 | |
David Blaikie | 7e70d68 | 2015-08-18 22:40:54 +0000 | [diff] [blame] | 541 | struct ExtendGCLifetime final : EHScopeStack::Cleanup { |
John McCall | 1bd2556 | 2011-06-24 23:21:27 +0000 | [diff] [blame] | 542 | const VarDecl &Var; |
| 543 | ExtendGCLifetime(const VarDecl *var) : Var(*var) {} |
| 544 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 545 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
John McCall | 1bd2556 | 2011-06-24 23:21:27 +0000 | [diff] [blame] | 546 | // Compute the address of the local variable, in case it's a |
| 547 | // byref or something. |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 548 | DeclRefExpr DRE(CGF.getContext(), const_cast<VarDecl *>(&Var), false, |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 549 | Var.getType(), VK_LValue, SourceLocation()); |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 550 | llvm::Value *value = CGF.EmitLoadOfScalar(CGF.EmitDeclRefLValue(&DRE), |
| 551 | SourceLocation()); |
John McCall | 1bd2556 | 2011-06-24 23:21:27 +0000 | [diff] [blame] | 552 | CGF.EmitExtendGCLifetime(value); |
| 553 | } |
| 554 | }; |
| 555 | |
David Blaikie | 7e70d68 | 2015-08-18 22:40:54 +0000 | [diff] [blame] | 556 | struct CallCleanupFunction final : EHScopeStack::Cleanup { |
John McCall | a464ff9 | 2010-07-21 06:13:08 +0000 | [diff] [blame] | 557 | llvm::Constant *CleanupFn; |
| 558 | const CGFunctionInfo &FnInfo; |
John McCall | a464ff9 | 2010-07-21 06:13:08 +0000 | [diff] [blame] | 559 | const VarDecl &Var; |
Eric Christopher | 31ab130 | 2011-08-23 22:38:00 +0000 | [diff] [blame] | 560 | |
John McCall | a464ff9 | 2010-07-21 06:13:08 +0000 | [diff] [blame] | 561 | CallCleanupFunction(llvm::Constant *CleanupFn, const CGFunctionInfo *Info, |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 562 | const VarDecl *Var) |
| 563 | : CleanupFn(CleanupFn), FnInfo(*Info), Var(*Var) {} |
John McCall | a464ff9 | 2010-07-21 06:13:08 +0000 | [diff] [blame] | 564 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 565 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 566 | DeclRefExpr DRE(CGF.getContext(), const_cast<VarDecl *>(&Var), false, |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 567 | Var.getType(), VK_LValue, SourceLocation()); |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 568 | // Compute the address of the local variable, in case it's a byref |
| 569 | // or something. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 570 | llvm::Value *Addr = CGF.EmitDeclRefLValue(&DRE).getPointer(); |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 571 | |
John McCall | a464ff9 | 2010-07-21 06:13:08 +0000 | [diff] [blame] | 572 | // In some cases, the type of the function argument will be different from |
| 573 | // the type of the pointer. An example of this is |
| 574 | // void f(void* arg); |
| 575 | // __attribute__((cleanup(f))) void *g; |
| 576 | // |
| 577 | // To fix this we insert a bitcast here. |
| 578 | QualType ArgTy = FnInfo.arg_begin()->type; |
| 579 | llvm::Value *Arg = |
| 580 | CGF.Builder.CreateBitCast(Addr, CGF.ConvertType(ArgTy)); |
| 581 | |
| 582 | CallArgList Args; |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 583 | Args.add(RValue::get(Arg), |
| 584 | CGF.getContext().getPointerType(Var.getType())); |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 585 | auto Callee = CGCallee::forDirect(CleanupFn); |
| 586 | CGF.EmitCall(FnInfo, Callee, ReturnValueSlot(), Args); |
John McCall | a464ff9 | 2010-07-21 06:13:08 +0000 | [diff] [blame] | 587 | } |
| 588 | }; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 589 | } // end anonymous namespace |
John McCall | a464ff9 | 2010-07-21 06:13:08 +0000 | [diff] [blame] | 590 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 591 | /// EmitAutoVarWithLifetime - Does the setup required for an automatic |
| 592 | /// variable with lifetime. |
| 593 | static void EmitAutoVarWithLifetime(CodeGenFunction &CGF, const VarDecl &var, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 594 | Address addr, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 595 | Qualifiers::ObjCLifetime lifetime) { |
| 596 | switch (lifetime) { |
| 597 | case Qualifiers::OCL_None: |
| 598 | llvm_unreachable("present but none"); |
| 599 | |
| 600 | case Qualifiers::OCL_ExplicitNone: |
| 601 | // nothing to do |
| 602 | break; |
| 603 | |
| 604 | case Qualifiers::OCL_Strong: { |
Peter Collingbourne | 1425b45 | 2012-01-26 03:33:36 +0000 | [diff] [blame] | 605 | CodeGenFunction::Destroyer *destroyer = |
John McCall | 4bd0fb1 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 606 | (var.hasAttr<ObjCPreciseLifetimeAttr>() |
| 607 | ? CodeGenFunction::destroyARCStrongPrecise |
| 608 | : CodeGenFunction::destroyARCStrongImprecise); |
| 609 | |
| 610 | CleanupKind cleanupKind = CGF.getARCCleanupKind(); |
| 611 | CGF.pushDestroy(cleanupKind, addr, var.getType(), destroyer, |
| 612 | cleanupKind & EHCleanup); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 613 | break; |
| 614 | } |
| 615 | case Qualifiers::OCL_Autoreleasing: |
| 616 | // nothing to do |
| 617 | break; |
Eric Christopher | 31ab130 | 2011-08-23 22:38:00 +0000 | [diff] [blame] | 618 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 619 | case Qualifiers::OCL_Weak: |
| 620 | // __weak objects always get EH cleanups; otherwise, exceptions |
| 621 | // could cause really nasty crashes instead of mere leaks. |
John McCall | 4bd0fb1 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 622 | CGF.pushDestroy(NormalAndEHCleanup, addr, var.getType(), |
| 623 | CodeGenFunction::destroyARCWeak, |
| 624 | /*useEHCleanup*/ true); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 625 | break; |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | static bool isAccessedBy(const VarDecl &var, const Stmt *s) { |
| 630 | if (const Expr *e = dyn_cast<Expr>(s)) { |
| 631 | // Skip the most common kinds of expressions that make |
| 632 | // hierarchy-walking expensive. |
| 633 | s = e = e->IgnoreParenCasts(); |
| 634 | |
| 635 | if (const DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) |
| 636 | return (ref->getDecl() == &var); |
Fariborz Jahanian | a36cbeb | 2012-06-19 20:53:26 +0000 | [diff] [blame] | 637 | if (const BlockExpr *be = dyn_cast<BlockExpr>(e)) { |
| 638 | const BlockDecl *block = be->getBlockDecl(); |
Aaron Ballman | 9371dd2 | 2014-03-14 18:34:04 +0000 | [diff] [blame] | 639 | for (const auto &I : block->captures()) { |
| 640 | if (I.getVariable() == &var) |
Fariborz Jahanian | a36cbeb | 2012-06-19 20:53:26 +0000 | [diff] [blame] | 641 | return true; |
| 642 | } |
| 643 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 646 | for (const Stmt *SubStmt : s->children()) |
| 647 | // SubStmt might be null; as in missing decl or conditional of an if-stmt. |
| 648 | if (SubStmt && isAccessedBy(var, SubStmt)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 649 | return true; |
| 650 | |
| 651 | return false; |
| 652 | } |
| 653 | |
| 654 | static bool isAccessedBy(const ValueDecl *decl, const Expr *e) { |
| 655 | if (!decl) return false; |
| 656 | if (!isa<VarDecl>(decl)) return false; |
| 657 | const VarDecl *var = cast<VarDecl>(decl); |
| 658 | return isAccessedBy(*var, e); |
| 659 | } |
| 660 | |
John McCall | f8a9b66 | 2015-10-21 18:06:31 +0000 | [diff] [blame] | 661 | static bool tryEmitARCCopyWeakInit(CodeGenFunction &CGF, |
| 662 | const LValue &destLV, const Expr *init) { |
John McCall | 0204e34 | 2015-11-16 22:11:41 +0000 | [diff] [blame] | 663 | bool needsCast = false; |
| 664 | |
John McCall | f8a9b66 | 2015-10-21 18:06:31 +0000 | [diff] [blame] | 665 | while (auto castExpr = dyn_cast<CastExpr>(init->IgnoreParens())) { |
| 666 | switch (castExpr->getCastKind()) { |
| 667 | // Look through casts that don't require representation changes. |
| 668 | case CK_NoOp: |
| 669 | case CK_BitCast: |
| 670 | case CK_BlockPointerToObjCPointerCast: |
John McCall | 0204e34 | 2015-11-16 22:11:41 +0000 | [diff] [blame] | 671 | needsCast = true; |
John McCall | f8a9b66 | 2015-10-21 18:06:31 +0000 | [diff] [blame] | 672 | break; |
| 673 | |
| 674 | // If we find an l-value to r-value cast from a __weak variable, |
| 675 | // emit this operation as a copy or move. |
| 676 | case CK_LValueToRValue: { |
| 677 | const Expr *srcExpr = castExpr->getSubExpr(); |
| 678 | if (srcExpr->getType().getObjCLifetime() != Qualifiers::OCL_Weak) |
| 679 | return false; |
| 680 | |
| 681 | // Emit the source l-value. |
| 682 | LValue srcLV = CGF.EmitLValue(srcExpr); |
| 683 | |
John McCall | 0204e34 | 2015-11-16 22:11:41 +0000 | [diff] [blame] | 684 | // Handle a formal type change to avoid asserting. |
| 685 | auto srcAddr = srcLV.getAddress(); |
| 686 | if (needsCast) { |
| 687 | srcAddr = CGF.Builder.CreateElementBitCast(srcAddr, |
| 688 | destLV.getAddress().getElementType()); |
| 689 | } |
| 690 | |
John McCall | f8a9b66 | 2015-10-21 18:06:31 +0000 | [diff] [blame] | 691 | // If it was an l-value, use objc_copyWeak. |
| 692 | if (srcExpr->getValueKind() == VK_LValue) { |
John McCall | 0204e34 | 2015-11-16 22:11:41 +0000 | [diff] [blame] | 693 | CGF.EmitARCCopyWeak(destLV.getAddress(), srcAddr); |
John McCall | f8a9b66 | 2015-10-21 18:06:31 +0000 | [diff] [blame] | 694 | } else { |
| 695 | assert(srcExpr->getValueKind() == VK_XValue); |
John McCall | 0204e34 | 2015-11-16 22:11:41 +0000 | [diff] [blame] | 696 | CGF.EmitARCMoveWeak(destLV.getAddress(), srcAddr); |
John McCall | f8a9b66 | 2015-10-21 18:06:31 +0000 | [diff] [blame] | 697 | } |
| 698 | return true; |
| 699 | } |
| 700 | |
| 701 | // Stop at anything else. |
| 702 | default: |
| 703 | return false; |
| 704 | } |
| 705 | |
| 706 | init = castExpr->getSubExpr(); |
John McCall | f8a9b66 | 2015-10-21 18:06:31 +0000 | [diff] [blame] | 707 | } |
| 708 | return false; |
| 709 | } |
| 710 | |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 711 | static void drillIntoBlockVariable(CodeGenFunction &CGF, |
| 712 | LValue &lvalue, |
| 713 | const VarDecl *var) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 714 | lvalue.setAddress(CGF.emitBlockByrefAddress(lvalue.getAddress(), var)); |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Vedant Kumar | 42c17ec | 2017-03-14 01:56:34 +0000 | [diff] [blame] | 717 | void CodeGenFunction::EmitNullabilityCheck(LValue LHS, llvm::Value *RHS, |
| 718 | SourceLocation Loc) { |
| 719 | if (!SanOpts.has(SanitizerKind::NullabilityAssign)) |
| 720 | return; |
| 721 | |
| 722 | auto Nullability = LHS.getType()->getNullability(getContext()); |
| 723 | if (!Nullability || *Nullability != NullabilityKind::NonNull) |
| 724 | return; |
| 725 | |
| 726 | // Check if the right hand side of the assignment is nonnull, if the left |
| 727 | // hand side must be nonnull. |
| 728 | SanitizerScope SanScope(this); |
| 729 | llvm::Value *IsNotNull = Builder.CreateIsNotNull(RHS); |
Vedant Kumar | 42c17ec | 2017-03-14 01:56:34 +0000 | [diff] [blame] | 730 | llvm::Constant *StaticData[] = { |
| 731 | EmitCheckSourceLocation(Loc), EmitCheckTypeDescriptor(LHS.getType()), |
Simon Pilgrim | 2df1998 | 2017-03-14 21:43:52 +0000 | [diff] [blame] | 732 | llvm::ConstantInt::get(Int8Ty, 0), // The LogAlignment info is unused. |
Vedant Kumar | 2b9f48a | 2017-03-14 16:48:29 +0000 | [diff] [blame] | 733 | llvm::ConstantInt::get(Int8Ty, TCK_NonnullAssign)}; |
Vedant Kumar | 42c17ec | 2017-03-14 01:56:34 +0000 | [diff] [blame] | 734 | EmitCheck({{IsNotNull, SanitizerKind::NullabilityAssign}}, |
| 735 | SanitizerHandler::TypeMismatch, StaticData, RHS); |
| 736 | } |
| 737 | |
David Blaikie | 73ca569 | 2014-12-09 00:32:22 +0000 | [diff] [blame] | 738 | void CodeGenFunction::EmitScalarInit(const Expr *init, const ValueDecl *D, |
David Blaikie | 66e4197 | 2015-01-14 07:38:27 +0000 | [diff] [blame] | 739 | LValue lvalue, bool capturedByInit) { |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 740 | Qualifiers::ObjCLifetime lifetime = lvalue.getObjCLifetime(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 741 | if (!lifetime) { |
| 742 | llvm::Value *value = EmitScalarExpr(init); |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 743 | if (capturedByInit) |
| 744 | drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D)); |
Vedant Kumar | 42c17ec | 2017-03-14 01:56:34 +0000 | [diff] [blame] | 745 | EmitNullabilityCheck(lvalue, value, init->getExprLoc()); |
David Blaikie | 66e4197 | 2015-01-14 07:38:27 +0000 | [diff] [blame] | 746 | EmitStoreThroughLValue(RValue::get(value), lvalue, true); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 747 | return; |
| 748 | } |
Vitaly Buka | 9d4eb6f | 2016-06-02 00:24:20 +0000 | [diff] [blame] | 749 | |
Fariborz Jahanian | a5a469e | 2014-03-14 15:40:54 +0000 | [diff] [blame] | 750 | if (const CXXDefaultInitExpr *DIE = dyn_cast<CXXDefaultInitExpr>(init)) |
| 751 | init = DIE->getExpr(); |
Vitaly Buka | 9d4eb6f | 2016-06-02 00:24:20 +0000 | [diff] [blame] | 752 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 753 | // If we're emitting a value with lifetime, we have to do the |
| 754 | // initialization *before* we leave the cleanup scopes. |
Bill Wendling | 7c44da2 | 2018-10-31 03:48:47 +0000 | [diff] [blame] | 755 | if (const FullExpr *fe = dyn_cast<FullExpr>(init)) { |
| 756 | enterFullExpression(fe); |
| 757 | init = fe->getSubExpr(); |
John McCall | 08ef466 | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 758 | } |
| 759 | CodeGenFunction::RunCleanupsScope Scope(*this); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 760 | |
| 761 | // We have to maintain the illusion that the variable is |
| 762 | // zero-initialized. If the variable might be accessed in its |
| 763 | // initializer, zero-initialize before running the initializer, then |
| 764 | // actually perform the initialization with an assign. |
| 765 | bool accessedByInit = false; |
| 766 | if (lifetime != Qualifiers::OCL_ExplicitNone) |
John McCall | b726a55 | 2011-07-28 07:23:35 +0000 | [diff] [blame] | 767 | accessedByInit = (capturedByInit || isAccessedBy(D, init)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 768 | if (accessedByInit) { |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 769 | LValue tempLV = lvalue; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 770 | // Drill down to the __block object if necessary. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 771 | if (capturedByInit) { |
| 772 | // We can use a simple GEP for this because it can't have been |
| 773 | // moved yet. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 774 | tempLV.setAddress(emitBlockByrefAddress(tempLV.getAddress(), |
| 775 | cast<VarDecl>(D), |
| 776 | /*follow*/ false)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 777 | } |
| 778 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 779 | auto ty = cast<llvm::PointerType>(tempLV.getAddress().getElementType()); |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 780 | llvm::Value *zero = CGM.getNullPointer(ty, tempLV.getType()); |
Eric Christopher | 31ab130 | 2011-08-23 22:38:00 +0000 | [diff] [blame] | 781 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 782 | // If __weak, we want to use a barrier under certain conditions. |
| 783 | if (lifetime == Qualifiers::OCL_Weak) |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 784 | EmitARCInitWeak(tempLV.getAddress(), zero); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 785 | |
| 786 | // Otherwise just do a simple store. |
| 787 | else |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 788 | EmitStoreOfScalar(zero, tempLV, /* isInitialization */ true); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | // Emit the initializer. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 792 | llvm::Value *value = nullptr; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 793 | |
| 794 | switch (lifetime) { |
| 795 | case Qualifiers::OCL_None: |
| 796 | llvm_unreachable("present but none"); |
| 797 | |
Erik Pilkington | 1e36882 | 2019-01-04 18:33:06 +0000 | [diff] [blame] | 798 | case Qualifiers::OCL_Strong: { |
| 799 | if (!D || !isa<VarDecl>(D) || !cast<VarDecl>(D)->isARCPseudoStrong()) { |
| 800 | value = EmitARCRetainScalarExpr(init); |
| 801 | break; |
| 802 | } |
| 803 | // If D is pseudo-strong, treat it like __unsafe_unretained here. This means |
| 804 | // that we omit the retain, and causes non-autoreleased return values to be |
| 805 | // immediately released. |
| 806 | LLVM_FALLTHROUGH; |
| 807 | } |
| 808 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 809 | case Qualifiers::OCL_ExplicitNone: |
John McCall | e399e5b | 2016-01-27 18:32:30 +0000 | [diff] [blame] | 810 | value = EmitARCUnsafeUnretainedScalarExpr(init); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 811 | break; |
| 812 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 813 | case Qualifiers::OCL_Weak: { |
John McCall | f8a9b66 | 2015-10-21 18:06:31 +0000 | [diff] [blame] | 814 | // If it's not accessed by the initializer, try to emit the |
| 815 | // initialization with a copy or move. |
| 816 | if (!accessedByInit && tryEmitARCCopyWeakInit(*this, lvalue, init)) { |
| 817 | return; |
| 818 | } |
| 819 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 820 | // No way to optimize a producing initializer into this. It's not |
| 821 | // worth optimizing for, because the value will immediately |
| 822 | // disappear in the common case. |
| 823 | value = EmitScalarExpr(init); |
| 824 | |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 825 | if (capturedByInit) drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 826 | if (accessedByInit) |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 827 | EmitARCStoreWeak(lvalue.getAddress(), value, /*ignored*/ true); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 828 | else |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 829 | EmitARCInitWeak(lvalue.getAddress(), value); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 830 | return; |
| 831 | } |
| 832 | |
| 833 | case Qualifiers::OCL_Autoreleasing: |
| 834 | value = EmitARCRetainAutoreleaseScalarExpr(init); |
| 835 | break; |
| 836 | } |
| 837 | |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 838 | if (capturedByInit) drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 839 | |
Vedant Kumar | 42c17ec | 2017-03-14 01:56:34 +0000 | [diff] [blame] | 840 | EmitNullabilityCheck(lvalue, value, init->getExprLoc()); |
| 841 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 842 | // If the variable might have been accessed by its initializer, we |
| 843 | // might have to initialize with a barrier. We have to do this for |
| 844 | // both __weak and __strong, but __weak got filtered out above. |
| 845 | if (accessedByInit && lifetime == Qualifiers::OCL_Strong) { |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 846 | llvm::Value *oldValue = EmitLoadOfScalar(lvalue, init->getExprLoc()); |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 847 | EmitStoreOfScalar(value, lvalue, /* isInitialization */ true); |
John McCall | cdda29c | 2013-03-13 03:10:54 +0000 | [diff] [blame] | 848 | EmitARCRelease(oldValue, ARCImpreciseLifetime); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 849 | return; |
| 850 | } |
| 851 | |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 852 | EmitStoreOfScalar(value, lvalue, /* isInitialization */ true); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 853 | } |
Chris Lattner | b85025f | 2010-12-01 02:05:19 +0000 | [diff] [blame] | 854 | |
JF Bastien | ed92f60 | 2018-07-20 23:37:12 +0000 | [diff] [blame] | 855 | /// Decide whether we can emit the non-zero parts of the specified initializer |
| 856 | /// with equal or fewer than NumStores scalar stores. |
| 857 | static bool canEmitInitWithFewStoresAfterBZero(llvm::Constant *Init, |
| 858 | unsigned &NumStores) { |
Chris Lattner | e6af886 | 2010-12-02 01:58:41 +0000 | [diff] [blame] | 859 | // Zero and Undef never requires any extra stores. |
| 860 | if (isa<llvm::ConstantAggregateZero>(Init) || |
| 861 | isa<llvm::ConstantPointerNull>(Init) || |
| 862 | isa<llvm::UndefValue>(Init)) |
| 863 | return true; |
| 864 | if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) || |
| 865 | isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) || |
| 866 | isa<llvm::ConstantExpr>(Init)) |
| 867 | return Init->isNullValue() || NumStores--; |
| 868 | |
| 869 | // See if we can emit each element. |
| 870 | if (isa<llvm::ConstantArray>(Init) || isa<llvm::ConstantStruct>(Init)) { |
| 871 | for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) { |
| 872 | llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i)); |
JF Bastien | ed92f60 | 2018-07-20 23:37:12 +0000 | [diff] [blame] | 873 | if (!canEmitInitWithFewStoresAfterBZero(Elt, NumStores)) |
Chris Lattner | e6af886 | 2010-12-02 01:58:41 +0000 | [diff] [blame] | 874 | return false; |
| 875 | } |
| 876 | return true; |
| 877 | } |
Vitaly Buka | 9d4eb6f | 2016-06-02 00:24:20 +0000 | [diff] [blame] | 878 | |
Chris Lattner | 236b357 | 2012-01-31 04:36:19 +0000 | [diff] [blame] | 879 | if (llvm::ConstantDataSequential *CDS = |
| 880 | dyn_cast<llvm::ConstantDataSequential>(Init)) { |
| 881 | for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) { |
| 882 | llvm::Constant *Elt = CDS->getElementAsConstant(i); |
JF Bastien | ed92f60 | 2018-07-20 23:37:12 +0000 | [diff] [blame] | 883 | if (!canEmitInitWithFewStoresAfterBZero(Elt, NumStores)) |
Chris Lattner | 236b357 | 2012-01-31 04:36:19 +0000 | [diff] [blame] | 884 | return false; |
| 885 | } |
| 886 | return true; |
| 887 | } |
Eric Christopher | 31ab130 | 2011-08-23 22:38:00 +0000 | [diff] [blame] | 888 | |
Chris Lattner | b85025f | 2010-12-01 02:05:19 +0000 | [diff] [blame] | 889 | // Anything else is hard and scary. |
| 890 | return false; |
| 891 | } |
| 892 | |
JF Bastien | ed92f60 | 2018-07-20 23:37:12 +0000 | [diff] [blame] | 893 | /// For inits that canEmitInitWithFewStoresAfterBZero returned true for, emit |
| 894 | /// the scalar stores that would be required. |
| 895 | static void emitStoresForInitAfterBZero(CodeGenModule &CGM, |
| 896 | llvm::Constant *Init, Address Loc, |
| 897 | bool isVolatile, CGBuilderTy &Builder) { |
Benjamin Kramer | 29f9a00 | 2012-08-27 22:07:02 +0000 | [diff] [blame] | 898 | assert(!Init->isNullValue() && !isa<llvm::UndefValue>(Init) && |
JF Bastien | ed92f60 | 2018-07-20 23:37:12 +0000 | [diff] [blame] | 899 | "called emitStoresForInitAfterBZero for zero or undef value."); |
Eric Christopher | 31ab130 | 2011-08-23 22:38:00 +0000 | [diff] [blame] | 900 | |
Chris Lattner | e6af886 | 2010-12-02 01:58:41 +0000 | [diff] [blame] | 901 | if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) || |
| 902 | isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) || |
| 903 | isa<llvm::ConstantExpr>(Init)) { |
JF Bastien | 9aab85a | 2018-07-13 20:33:23 +0000 | [diff] [blame] | 904 | Builder.CreateStore(Init, Loc, isVolatile); |
Chris Lattner | 236b357 | 2012-01-31 04:36:19 +0000 | [diff] [blame] | 905 | return; |
| 906 | } |
Vitaly Buka | 9d4eb6f | 2016-06-02 00:24:20 +0000 | [diff] [blame] | 907 | |
| 908 | if (llvm::ConstantDataSequential *CDS = |
| 909 | dyn_cast<llvm::ConstantDataSequential>(Init)) { |
Chris Lattner | 236b357 | 2012-01-31 04:36:19 +0000 | [diff] [blame] | 910 | for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) { |
| 911 | llvm::Constant *Elt = CDS->getElementAsConstant(i); |
Benjamin Kramer | 46cbe77 | 2012-08-27 21:35:58 +0000 | [diff] [blame] | 912 | |
| 913 | // If necessary, get a pointer to the element and emit it. |
| 914 | if (!Elt->isNullValue() && !isa<llvm::UndefValue>(Elt)) |
JF Bastien | ed92f60 | 2018-07-20 23:37:12 +0000 | [diff] [blame] | 915 | emitStoresForInitAfterBZero( |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 916 | CGM, Elt, Builder.CreateConstInBoundsGEP2_32(Loc, 0, i), isVolatile, |
| 917 | Builder); |
Chris Lattner | 236b357 | 2012-01-31 04:36:19 +0000 | [diff] [blame] | 918 | } |
Chris Lattner | e6af886 | 2010-12-02 01:58:41 +0000 | [diff] [blame] | 919 | return; |
| 920 | } |
Eric Christopher | 31ab130 | 2011-08-23 22:38:00 +0000 | [diff] [blame] | 921 | |
Chris Lattner | e6af886 | 2010-12-02 01:58:41 +0000 | [diff] [blame] | 922 | assert((isa<llvm::ConstantStruct>(Init) || isa<llvm::ConstantArray>(Init)) && |
| 923 | "Unknown value type!"); |
Eric Christopher | 31ab130 | 2011-08-23 22:38:00 +0000 | [diff] [blame] | 924 | |
Chris Lattner | e6af886 | 2010-12-02 01:58:41 +0000 | [diff] [blame] | 925 | for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) { |
| 926 | llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i)); |
Benjamin Kramer | 46cbe77 | 2012-08-27 21:35:58 +0000 | [diff] [blame] | 927 | |
| 928 | // If necessary, get a pointer to the element and emit it. |
| 929 | if (!Elt->isNullValue() && !isa<llvm::UndefValue>(Elt)) |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 930 | emitStoresForInitAfterBZero(CGM, Elt, |
| 931 | Builder.CreateConstInBoundsGEP2_32(Loc, 0, i), |
| 932 | isVolatile, Builder); |
Chris Lattner | e6af886 | 2010-12-02 01:58:41 +0000 | [diff] [blame] | 933 | } |
Chris Lattner | b85025f | 2010-12-01 02:05:19 +0000 | [diff] [blame] | 934 | } |
| 935 | |
JF Bastien | ed92f60 | 2018-07-20 23:37:12 +0000 | [diff] [blame] | 936 | /// Decide whether we should use bzero plus some stores to initialize a local |
| 937 | /// variable instead of using a memcpy from a constant global. It is beneficial |
| 938 | /// to use bzero if the global is all zeros, or mostly zeros and large. |
| 939 | static bool shouldUseBZeroPlusStoresToInitialize(llvm::Constant *Init, |
| 940 | uint64_t GlobalSize) { |
| 941 | // If a global is all zeros, always use a bzero. |
Chris Lattner | b85025f | 2010-12-01 02:05:19 +0000 | [diff] [blame] | 942 | if (isa<llvm::ConstantAggregateZero>(Init)) return true; |
| 943 | |
Chris Lattner | b85025f | 2010-12-01 02:05:19 +0000 | [diff] [blame] | 944 | // If a non-zero global is <= 32 bytes, always use a memcpy. If it is large, |
| 945 | // do it if it will require 6 or fewer scalar stores. |
| 946 | // TODO: Should budget depends on the size? Avoiding a large global warrants |
| 947 | // plopping in more stores. |
| 948 | unsigned StoreBudget = 6; |
| 949 | uint64_t SizeLimit = 32; |
Eric Christopher | 31ab130 | 2011-08-23 22:38:00 +0000 | [diff] [blame] | 950 | |
| 951 | return GlobalSize > SizeLimit && |
JF Bastien | ed92f60 | 2018-07-20 23:37:12 +0000 | [diff] [blame] | 952 | canEmitInitWithFewStoresAfterBZero(Init, StoreBudget); |
Chris Lattner | b85025f | 2010-12-01 02:05:19 +0000 | [diff] [blame] | 953 | } |
| 954 | |
JF Bastien | 6508929 | 2018-07-25 04:29:03 +0000 | [diff] [blame] | 955 | /// Decide whether we should use memset to initialize a local variable instead |
| 956 | /// of using a memcpy from a constant global. Assumes we've already decided to |
| 957 | /// not user bzero. |
| 958 | /// FIXME We could be more clever, as we are for bzero above, and generate |
| 959 | /// memset followed by stores. It's unclear that's worth the effort. |
JF Bastien | 2f0582f | 2018-09-21 13:54:09 +0000 | [diff] [blame] | 960 | static llvm::Value *shouldUseMemSetToInitialize(llvm::Constant *Init, |
Vitaly Buka | d03bd1d | 2019-07-10 22:53:52 +0000 | [diff] [blame] | 961 | uint64_t GlobalSize, |
| 962 | const llvm::DataLayout &DL) { |
JF Bastien | 6508929 | 2018-07-25 04:29:03 +0000 | [diff] [blame] | 963 | uint64_t SizeLimit = 32; |
| 964 | if (GlobalSize <= SizeLimit) |
JF Bastien | 2f0582f | 2018-09-21 13:54:09 +0000 | [diff] [blame] | 965 | return nullptr; |
Vitaly Buka | d03bd1d | 2019-07-10 22:53:52 +0000 | [diff] [blame] | 966 | return llvm::isBytewiseValue(Init, DL); |
JF Bastien | 6508929 | 2018-07-25 04:29:03 +0000 | [diff] [blame] | 967 | } |
| 968 | |
JF Bastien | b5e5bc7 | 2019-03-08 01:26:49 +0000 | [diff] [blame] | 969 | /// Decide whether we want to split a constant structure or array store into a |
| 970 | /// sequence of its fields' stores. This may cost us code size and compilation |
| 971 | /// speed, but plays better with store optimizations. |
| 972 | static bool shouldSplitConstantStore(CodeGenModule &CGM, |
| 973 | uint64_t GlobalByteSize) { |
| 974 | // Don't break things that occupy more than one cacheline. |
Alexander Potapenko | fa61ddd | 2019-03-01 09:00:41 +0000 | [diff] [blame] | 975 | uint64_t ByteSizeLimit = 64; |
| 976 | if (CGM.getCodeGenOpts().OptimizationLevel == 0) |
| 977 | return false; |
| 978 | if (GlobalByteSize <= ByteSizeLimit) |
| 979 | return true; |
| 980 | return false; |
| 981 | } |
| 982 | |
Alexander Potapenko | 4f7bc0e | 2019-02-26 10:46:21 +0000 | [diff] [blame] | 983 | enum class IsPattern { No, Yes }; |
| 984 | |
| 985 | /// Generate a constant filled with either a pattern or zeroes. |
| 986 | static llvm::Constant *patternOrZeroFor(CodeGenModule &CGM, IsPattern isPattern, |
| 987 | llvm::Type *Ty) { |
| 988 | if (isPattern == IsPattern::Yes) |
JF Bastien | ef202c3 | 2019-04-12 00:11:27 +0000 | [diff] [blame] | 989 | return initializationPatternFor(CGM, Ty); |
Alexander Potapenko | 4f7bc0e | 2019-02-26 10:46:21 +0000 | [diff] [blame] | 990 | else |
| 991 | return llvm::Constant::getNullValue(Ty); |
| 992 | } |
| 993 | |
| 994 | static llvm::Constant *constWithPadding(CodeGenModule &CGM, IsPattern isPattern, |
| 995 | llvm::Constant *constant); |
| 996 | |
| 997 | /// Helper function for constWithPadding() to deal with padding in structures. |
| 998 | static llvm::Constant *constStructWithPadding(CodeGenModule &CGM, |
| 999 | IsPattern isPattern, |
| 1000 | llvm::StructType *STy, |
| 1001 | llvm::Constant *constant) { |
| 1002 | const llvm::DataLayout &DL = CGM.getDataLayout(); |
| 1003 | const llvm::StructLayout *Layout = DL.getStructLayout(STy); |
| 1004 | llvm::Type *Int8Ty = llvm::IntegerType::getInt8Ty(CGM.getLLVMContext()); |
| 1005 | unsigned SizeSoFar = 0; |
| 1006 | SmallVector<llvm::Constant *, 8> Values; |
| 1007 | bool NestedIntact = true; |
| 1008 | for (unsigned i = 0, e = STy->getNumElements(); i != e; i++) { |
| 1009 | unsigned CurOff = Layout->getElementOffset(i); |
| 1010 | if (SizeSoFar < CurOff) { |
| 1011 | assert(!STy->isPacked()); |
| 1012 | auto *PadTy = llvm::ArrayType::get(Int8Ty, CurOff - SizeSoFar); |
| 1013 | Values.push_back(patternOrZeroFor(CGM, isPattern, PadTy)); |
| 1014 | } |
| 1015 | llvm::Constant *CurOp; |
| 1016 | if (constant->isZeroValue()) |
| 1017 | CurOp = llvm::Constant::getNullValue(STy->getElementType(i)); |
| 1018 | else |
| 1019 | CurOp = cast<llvm::Constant>(constant->getAggregateElement(i)); |
| 1020 | auto *NewOp = constWithPadding(CGM, isPattern, CurOp); |
| 1021 | if (CurOp != NewOp) |
| 1022 | NestedIntact = false; |
| 1023 | Values.push_back(NewOp); |
| 1024 | SizeSoFar = CurOff + DL.getTypeAllocSize(CurOp->getType()); |
| 1025 | } |
| 1026 | unsigned TotalSize = Layout->getSizeInBytes(); |
| 1027 | if (SizeSoFar < TotalSize) { |
| 1028 | auto *PadTy = llvm::ArrayType::get(Int8Ty, TotalSize - SizeSoFar); |
| 1029 | Values.push_back(patternOrZeroFor(CGM, isPattern, PadTy)); |
| 1030 | } |
| 1031 | if (NestedIntact && Values.size() == STy->getNumElements()) |
| 1032 | return constant; |
Peter Collingbourne | 68b4673 | 2019-03-16 19:25:39 +0000 | [diff] [blame] | 1033 | return llvm::ConstantStruct::getAnon(Values, STy->isPacked()); |
Alexander Potapenko | 4f7bc0e | 2019-02-26 10:46:21 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | /// Replace all padding bytes in a given constant with either a pattern byte or |
| 1037 | /// 0x00. |
| 1038 | static llvm::Constant *constWithPadding(CodeGenModule &CGM, IsPattern isPattern, |
| 1039 | llvm::Constant *constant) { |
| 1040 | llvm::Type *OrigTy = constant->getType(); |
| 1041 | if (const auto STy = dyn_cast<llvm::StructType>(OrigTy)) |
| 1042 | return constStructWithPadding(CGM, isPattern, STy, constant); |
| 1043 | if (auto *STy = dyn_cast<llvm::SequentialType>(OrigTy)) { |
| 1044 | llvm::SmallVector<llvm::Constant *, 8> Values; |
| 1045 | unsigned Size = STy->getNumElements(); |
| 1046 | if (!Size) |
| 1047 | return constant; |
| 1048 | llvm::Type *ElemTy = STy->getElementType(); |
| 1049 | bool ZeroInitializer = constant->isZeroValue(); |
| 1050 | llvm::Constant *OpValue, *PaddedOp; |
| 1051 | if (ZeroInitializer) { |
| 1052 | OpValue = llvm::Constant::getNullValue(ElemTy); |
| 1053 | PaddedOp = constWithPadding(CGM, isPattern, OpValue); |
| 1054 | } |
| 1055 | for (unsigned Op = 0; Op != Size; ++Op) { |
| 1056 | if (!ZeroInitializer) { |
| 1057 | OpValue = constant->getAggregateElement(Op); |
| 1058 | PaddedOp = constWithPadding(CGM, isPattern, OpValue); |
| 1059 | } |
| 1060 | Values.push_back(PaddedOp); |
| 1061 | } |
| 1062 | auto *NewElemTy = Values[0]->getType(); |
| 1063 | if (NewElemTy == ElemTy) |
| 1064 | return constant; |
| 1065 | if (OrigTy->isArrayTy()) { |
| 1066 | auto *ArrayTy = llvm::ArrayType::get(NewElemTy, Size); |
| 1067 | return llvm::ConstantArray::get(ArrayTy, Values); |
| 1068 | } else { |
| 1069 | return llvm::ConstantVector::get(Values); |
| 1070 | } |
| 1071 | } |
| 1072 | return constant; |
| 1073 | } |
| 1074 | |
Richard Smith | 24cdcad | 2019-06-14 17:46:37 +0000 | [diff] [blame] | 1075 | Address CodeGenModule::createUnnamedGlobalFrom(const VarDecl &D, |
| 1076 | llvm::Constant *Constant, |
| 1077 | CharUnits Align) { |
JF Bastien | 3a881e6 | 2018-11-15 00:19:18 +0000 | [diff] [blame] | 1078 | auto FunctionName = [&](const DeclContext *DC) -> std::string { |
| 1079 | if (const auto *FD = dyn_cast<FunctionDecl>(DC)) { |
| 1080 | if (const auto *CC = dyn_cast<CXXConstructorDecl>(FD)) |
| 1081 | return CC->getNameAsString(); |
| 1082 | if (const auto *CD = dyn_cast<CXXDestructorDecl>(FD)) |
| 1083 | return CD->getNameAsString(); |
Richard Smith | 24cdcad | 2019-06-14 17:46:37 +0000 | [diff] [blame] | 1084 | return getMangledName(FD); |
JF Bastien | 3a881e6 | 2018-11-15 00:19:18 +0000 | [diff] [blame] | 1085 | } else if (const auto *OM = dyn_cast<ObjCMethodDecl>(DC)) { |
| 1086 | return OM->getNameAsString(); |
Mikael Holmen | ee84676 | 2018-11-15 13:01:54 +0000 | [diff] [blame] | 1087 | } else if (isa<BlockDecl>(DC)) { |
JF Bastien | 3a881e6 | 2018-11-15 00:19:18 +0000 | [diff] [blame] | 1088 | return "<block>"; |
Mikael Holmen | ee84676 | 2018-11-15 13:01:54 +0000 | [diff] [blame] | 1089 | } else if (isa<CapturedDecl>(DC)) { |
JF Bastien | 3a881e6 | 2018-11-15 00:19:18 +0000 | [diff] [blame] | 1090 | return "<captured>"; |
| 1091 | } else { |
Richard Smith | 24cdcad | 2019-06-14 17:46:37 +0000 | [diff] [blame] | 1092 | llvm_unreachable("expected a function or method"); |
JF Bastien | 3a881e6 | 2018-11-15 00:19:18 +0000 | [diff] [blame] | 1093 | } |
| 1094 | }; |
| 1095 | |
Richard Smith | 24cdcad | 2019-06-14 17:46:37 +0000 | [diff] [blame] | 1096 | // Form a simple per-variable cache of these values in case we find we |
| 1097 | // want to reuse them. |
| 1098 | llvm::GlobalVariable *&CacheEntry = InitializerConstants[&D]; |
| 1099 | if (!CacheEntry || CacheEntry->getInitializer() != Constant) { |
| 1100 | auto *Ty = Constant->getType(); |
| 1101 | bool isConstant = true; |
| 1102 | llvm::GlobalVariable *InsertBefore = nullptr; |
| 1103 | unsigned AS = |
| 1104 | getContext().getTargetAddressSpace(getStringLiteralAddressSpace()); |
| 1105 | std::string Name; |
| 1106 | if (D.hasGlobalStorage()) |
| 1107 | Name = getMangledName(&D).str() + ".const"; |
| 1108 | else if (const DeclContext *DC = D.getParentFunctionOrMethod()) |
| 1109 | Name = ("__const." + FunctionName(DC) + "." + D.getName()).str(); |
| 1110 | else |
| 1111 | llvm_unreachable("local variable has no parent function or method"); |
| 1112 | llvm::GlobalVariable *GV = new llvm::GlobalVariable( |
| 1113 | getModule(), Ty, isConstant, llvm::GlobalValue::PrivateLinkage, |
| 1114 | Constant, Name, InsertBefore, llvm::GlobalValue::NotThreadLocal, AS); |
| 1115 | GV->setAlignment(Align.getQuantity()); |
| 1116 | GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
| 1117 | CacheEntry = GV; |
| 1118 | } else if (CacheEntry->getAlignment() < Align.getQuantity()) { |
| 1119 | CacheEntry->setAlignment(Align.getQuantity()); |
| 1120 | } |
JF Bastien | 3a881e6 | 2018-11-15 00:19:18 +0000 | [diff] [blame] | 1121 | |
Richard Smith | 24cdcad | 2019-06-14 17:46:37 +0000 | [diff] [blame] | 1122 | return Address(CacheEntry, Align); |
| 1123 | } |
| 1124 | |
| 1125 | static Address createUnnamedGlobalForMemcpyFrom(CodeGenModule &CGM, |
| 1126 | const VarDecl &D, |
| 1127 | CGBuilderTy &Builder, |
| 1128 | llvm::Constant *Constant, |
| 1129 | CharUnits Align) { |
| 1130 | Address SrcPtr = CGM.createUnnamedGlobalFrom(D, Constant, Align); |
| 1131 | llvm::Type *BP = llvm::PointerType::getInt8PtrTy(CGM.getLLVMContext(), |
| 1132 | SrcPtr.getAddressSpace()); |
JF Bastien | 3a881e6 | 2018-11-15 00:19:18 +0000 | [diff] [blame] | 1133 | if (SrcPtr.getType() != BP) |
| 1134 | SrcPtr = Builder.CreateBitCast(SrcPtr, BP); |
| 1135 | return SrcPtr; |
| 1136 | } |
| 1137 | |
JF Bastien | b9cc1fc | 2018-08-07 21:55:13 +0000 | [diff] [blame] | 1138 | static void emitStoresForConstant(CodeGenModule &CGM, const VarDecl &D, |
| 1139 | Address Loc, bool isVolatile, |
| 1140 | CGBuilderTy &Builder, |
| 1141 | llvm::Constant *constant) { |
JF Bastien | 14daa20 | 2018-12-18 05:12:21 +0000 | [diff] [blame] | 1142 | auto *Ty = constant->getType(); |
Vitaly Buka | f35a345 | 2019-06-27 02:08:15 +0000 | [diff] [blame] | 1143 | uint64_t ConstantSize = CGM.getDataLayout().getTypeAllocSize(Ty); |
| 1144 | if (!ConstantSize) |
| 1145 | return; |
| 1146 | |
JF Bastien | b5e5bc7 | 2019-03-08 01:26:49 +0000 | [diff] [blame] | 1147 | bool canDoSingleStore = Ty->isIntOrIntVectorTy() || |
| 1148 | Ty->isPtrOrPtrVectorTy() || Ty->isFPOrFPVectorTy(); |
| 1149 | if (canDoSingleStore) { |
JF Bastien | 14daa20 | 2018-12-18 05:12:21 +0000 | [diff] [blame] | 1150 | Builder.CreateStore(constant, Loc, isVolatile); |
| 1151 | return; |
| 1152 | } |
| 1153 | |
Vitaly Buka | 96ff25a | 2019-06-27 01:34:21 +0000 | [diff] [blame] | 1154 | auto *SizeVal = llvm::ConstantInt::get(CGM.IntPtrTy, ConstantSize); |
JF Bastien | b5e5bc7 | 2019-03-08 01:26:49 +0000 | [diff] [blame] | 1155 | |
| 1156 | // If the initializer is all or mostly the same, codegen with bzero / memset |
| 1157 | // then do a few stores afterward. |
JF Bastien | b9cc1fc | 2018-08-07 21:55:13 +0000 | [diff] [blame] | 1158 | if (shouldUseBZeroPlusStoresToInitialize(constant, ConstantSize)) { |
Vitaly Buka | 96ff25a | 2019-06-27 01:34:21 +0000 | [diff] [blame] | 1159 | Builder.CreateMemSet(Loc, llvm::ConstantInt::get(CGM.Int8Ty, 0), SizeVal, |
JF Bastien | b9cc1fc | 2018-08-07 21:55:13 +0000 | [diff] [blame] | 1160 | isVolatile); |
| 1161 | |
| 1162 | bool valueAlreadyCorrect = |
| 1163 | constant->isNullValue() || isa<llvm::UndefValue>(constant); |
| 1164 | if (!valueAlreadyCorrect) { |
JF Bastien | 14daa20 | 2018-12-18 05:12:21 +0000 | [diff] [blame] | 1165 | Loc = Builder.CreateBitCast(Loc, Ty->getPointerTo(Loc.getAddressSpace())); |
JF Bastien | b9cc1fc | 2018-08-07 21:55:13 +0000 | [diff] [blame] | 1166 | emitStoresForInitAfterBZero(CGM, constant, Loc, isVolatile, Builder); |
| 1167 | } |
| 1168 | return; |
| 1169 | } |
| 1170 | |
JF Bastien | b5e5bc7 | 2019-03-08 01:26:49 +0000 | [diff] [blame] | 1171 | // If the initializer is a repeated byte pattern, use memset. |
Vitaly Buka | d03bd1d | 2019-07-10 22:53:52 +0000 | [diff] [blame] | 1172 | llvm::Value *Pattern = |
| 1173 | shouldUseMemSetToInitialize(constant, ConstantSize, CGM.getDataLayout()); |
JF Bastien | 2f0582f | 2018-09-21 13:54:09 +0000 | [diff] [blame] | 1174 | if (Pattern) { |
| 1175 | uint64_t Value = 0x00; |
| 1176 | if (!isa<llvm::UndefValue>(Pattern)) { |
| 1177 | const llvm::APInt &AP = cast<llvm::ConstantInt>(Pattern)->getValue(); |
| 1178 | assert(AP.getBitWidth() <= 8); |
| 1179 | Value = AP.getLimitedValue(); |
| 1180 | } |
Vitaly Buka | 96ff25a | 2019-06-27 01:34:21 +0000 | [diff] [blame] | 1181 | Builder.CreateMemSet(Loc, llvm::ConstantInt::get(CGM.Int8Ty, Value), SizeVal, |
JF Bastien | b9cc1fc | 2018-08-07 21:55:13 +0000 | [diff] [blame] | 1182 | isVolatile); |
| 1183 | return; |
| 1184 | } |
| 1185 | |
JF Bastien | b5e5bc7 | 2019-03-08 01:26:49 +0000 | [diff] [blame] | 1186 | // If the initializer is small, use a handful of stores. |
| 1187 | if (shouldSplitConstantStore(CGM, ConstantSize)) { |
| 1188 | if (auto *STy = dyn_cast<llvm::StructType>(Ty)) { |
| 1189 | // FIXME: handle the case when STy != Loc.getElementType(). |
| 1190 | if (STy == Loc.getElementType()) { |
| 1191 | for (unsigned i = 0; i != constant->getNumOperands(); i++) { |
| 1192 | Address EltPtr = Builder.CreateStructGEP(Loc, i); |
| 1193 | emitStoresForConstant( |
| 1194 | CGM, D, EltPtr, isVolatile, Builder, |
| 1195 | cast<llvm::Constant>(Builder.CreateExtractValue(constant, i))); |
| 1196 | } |
| 1197 | return; |
| 1198 | } |
| 1199 | } else if (auto *ATy = dyn_cast<llvm::ArrayType>(Ty)) { |
| 1200 | // FIXME: handle the case when ATy != Loc.getElementType(). |
| 1201 | if (ATy == Loc.getElementType()) { |
| 1202 | for (unsigned i = 0; i != ATy->getNumElements(); i++) { |
| 1203 | Address EltPtr = Builder.CreateConstArrayGEP(Loc, i); |
| 1204 | emitStoresForConstant( |
| 1205 | CGM, D, EltPtr, isVolatile, Builder, |
| 1206 | cast<llvm::Constant>(Builder.CreateExtractValue(constant, i))); |
| 1207 | } |
| 1208 | return; |
| 1209 | } |
Alexander Potapenko | fa61ddd | 2019-03-01 09:00:41 +0000 | [diff] [blame] | 1210 | } |
Alexander Potapenko | fa61ddd | 2019-03-01 09:00:41 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
JF Bastien | b5e5bc7 | 2019-03-08 01:26:49 +0000 | [diff] [blame] | 1213 | // Copy from a global. |
Richard Smith | 24cdcad | 2019-06-14 17:46:37 +0000 | [diff] [blame] | 1214 | Builder.CreateMemCpy(Loc, |
| 1215 | createUnnamedGlobalForMemcpyFrom( |
| 1216 | CGM, D, Builder, constant, Loc.getAlignment()), |
| 1217 | SizeVal, isVolatile); |
JF Bastien | b9cc1fc | 2018-08-07 21:55:13 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
JF Bastien | 14daa20 | 2018-12-18 05:12:21 +0000 | [diff] [blame] | 1220 | static void emitStoresForZeroInit(CodeGenModule &CGM, const VarDecl &D, |
| 1221 | Address Loc, bool isVolatile, |
| 1222 | CGBuilderTy &Builder) { |
| 1223 | llvm::Type *ElTy = Loc.getElementType(); |
Alexander Potapenko | 4f7bc0e | 2019-02-26 10:46:21 +0000 | [diff] [blame] | 1224 | llvm::Constant *constant = |
| 1225 | constWithPadding(CGM, IsPattern::No, llvm::Constant::getNullValue(ElTy)); |
JF Bastien | 14daa20 | 2018-12-18 05:12:21 +0000 | [diff] [blame] | 1226 | emitStoresForConstant(CGM, D, Loc, isVolatile, Builder, constant); |
| 1227 | } |
| 1228 | |
| 1229 | static void emitStoresForPatternInit(CodeGenModule &CGM, const VarDecl &D, |
| 1230 | Address Loc, bool isVolatile, |
| 1231 | CGBuilderTy &Builder) { |
| 1232 | llvm::Type *ElTy = Loc.getElementType(); |
JF Bastien | ef202c3 | 2019-04-12 00:11:27 +0000 | [diff] [blame] | 1233 | llvm::Constant *constant = constWithPadding( |
| 1234 | CGM, IsPattern::Yes, initializationPatternFor(CGM, ElTy)); |
JF Bastien | 14daa20 | 2018-12-18 05:12:21 +0000 | [diff] [blame] | 1235 | assert(!isa<llvm::UndefValue>(constant)); |
| 1236 | emitStoresForConstant(CGM, D, Loc, isVolatile, Builder, constant); |
| 1237 | } |
| 1238 | |
| 1239 | static bool containsUndef(llvm::Constant *constant) { |
| 1240 | auto *Ty = constant->getType(); |
| 1241 | if (isa<llvm::UndefValue>(constant)) |
| 1242 | return true; |
| 1243 | if (Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy()) |
| 1244 | for (llvm::Use &Op : constant->operands()) |
| 1245 | if (containsUndef(cast<llvm::Constant>(Op))) |
| 1246 | return true; |
| 1247 | return false; |
| 1248 | } |
| 1249 | |
Alexander Potapenko | 4f7bc0e | 2019-02-26 10:46:21 +0000 | [diff] [blame] | 1250 | static llvm::Constant *replaceUndef(CodeGenModule &CGM, IsPattern isPattern, |
| 1251 | llvm::Constant *constant) { |
JF Bastien | 14daa20 | 2018-12-18 05:12:21 +0000 | [diff] [blame] | 1252 | auto *Ty = constant->getType(); |
| 1253 | if (isa<llvm::UndefValue>(constant)) |
Alexander Potapenko | 4f7bc0e | 2019-02-26 10:46:21 +0000 | [diff] [blame] | 1254 | return patternOrZeroFor(CGM, isPattern, Ty); |
JF Bastien | 14daa20 | 2018-12-18 05:12:21 +0000 | [diff] [blame] | 1255 | if (!(Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy())) |
| 1256 | return constant; |
| 1257 | if (!containsUndef(constant)) |
| 1258 | return constant; |
| 1259 | llvm::SmallVector<llvm::Constant *, 8> Values(constant->getNumOperands()); |
| 1260 | for (unsigned Op = 0, NumOp = constant->getNumOperands(); Op != NumOp; ++Op) { |
| 1261 | auto *OpValue = cast<llvm::Constant>(constant->getOperand(Op)); |
Alexander Potapenko | 4f7bc0e | 2019-02-26 10:46:21 +0000 | [diff] [blame] | 1262 | Values[Op] = replaceUndef(CGM, isPattern, OpValue); |
JF Bastien | 14daa20 | 2018-12-18 05:12:21 +0000 | [diff] [blame] | 1263 | } |
| 1264 | if (Ty->isStructTy()) |
| 1265 | return llvm::ConstantStruct::get(cast<llvm::StructType>(Ty), Values); |
| 1266 | if (Ty->isArrayTy()) |
| 1267 | return llvm::ConstantArray::get(cast<llvm::ArrayType>(Ty), Values); |
| 1268 | assert(Ty->isVectorTy()); |
| 1269 | return llvm::ConstantVector::get(Values); |
| 1270 | } |
| 1271 | |
Nick Lewycky | 8a7d90b | 2010-12-30 20:21:55 +0000 | [diff] [blame] | 1272 | /// EmitAutoVarDecl - Emit code and set up an entry in LocalDeclMap for a |
Chris Lattner | 03df122 | 2007-06-02 04:53:11 +0000 | [diff] [blame] | 1273 | /// variable declaration with auto, register, or no storage class specifier. |
Chris Lattner | b781dc79 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 1274 | /// These turn into simple stack objects, or GlobalValues depending on target. |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1275 | void CodeGenFunction::EmitAutoVarDecl(const VarDecl &D) { |
| 1276 | AutoVarEmission emission = EmitAutoVarAlloca(D); |
| 1277 | EmitAutoVarInit(emission); |
| 1278 | EmitAutoVarCleanups(emission); |
| 1279 | } |
Chris Lattner | 03df122 | 2007-06-02 04:53:11 +0000 | [diff] [blame] | 1280 | |
David Majnemer | dc012fa | 2015-04-22 21:38:15 +0000 | [diff] [blame] | 1281 | /// Emit a lifetime.begin marker if some criteria are satisfied. |
| 1282 | /// \return a pointer to the temporary size Value if a marker was emitted, null |
| 1283 | /// otherwise |
| 1284 | llvm::Value *CodeGenFunction::EmitLifetimeStart(uint64_t Size, |
| 1285 | llvm::Value *Addr) { |
Vitaly Buka | 1c94332 | 2016-10-26 01:59:57 +0000 | [diff] [blame] | 1286 | if (!ShouldEmitLifetimeMarkers) |
Reid Kleckner | 1ef4921 | 2015-04-23 18:07:13 +0000 | [diff] [blame] | 1287 | return nullptr; |
| 1288 | |
Yaxun Liu | a2a9cfa | 2018-05-17 11:16:35 +0000 | [diff] [blame] | 1289 | assert(Addr->getType()->getPointerAddressSpace() == |
| 1290 | CGM.getDataLayout().getAllocaAddrSpace() && |
| 1291 | "Pointer should be in alloca address space"); |
David Majnemer | dc012fa | 2015-04-22 21:38:15 +0000 | [diff] [blame] | 1292 | llvm::Value *SizeV = llvm::ConstantInt::get(Int64Ty, Size); |
Yaxun Liu | 7f7f323 | 2017-04-17 20:03:11 +0000 | [diff] [blame] | 1293 | Addr = Builder.CreateBitCast(Addr, AllocaInt8PtrTy); |
Alexey Samsonov | d918ff6 | 2015-06-12 22:31:32 +0000 | [diff] [blame] | 1294 | llvm::CallInst *C = |
| 1295 | Builder.CreateCall(CGM.getLLVMLifetimeStartFn(), {SizeV, Addr}); |
David Majnemer | dc012fa | 2015-04-22 21:38:15 +0000 | [diff] [blame] | 1296 | C->setDoesNotThrow(); |
| 1297 | return SizeV; |
| 1298 | } |
| 1299 | |
| 1300 | void CodeGenFunction::EmitLifetimeEnd(llvm::Value *Size, llvm::Value *Addr) { |
Yaxun Liu | a2a9cfa | 2018-05-17 11:16:35 +0000 | [diff] [blame] | 1301 | assert(Addr->getType()->getPointerAddressSpace() == |
| 1302 | CGM.getDataLayout().getAllocaAddrSpace() && |
| 1303 | "Pointer should be in alloca address space"); |
Yaxun Liu | 7f7f323 | 2017-04-17 20:03:11 +0000 | [diff] [blame] | 1304 | Addr = Builder.CreateBitCast(Addr, AllocaInt8PtrTy); |
Alexey Samsonov | d918ff6 | 2015-06-12 22:31:32 +0000 | [diff] [blame] | 1305 | llvm::CallInst *C = |
| 1306 | Builder.CreateCall(CGM.getLLVMLifetimeEndFn(), {Size, Addr}); |
David Majnemer | dc012fa | 2015-04-22 21:38:15 +0000 | [diff] [blame] | 1307 | C->setDoesNotThrow(); |
| 1308 | } |
| 1309 | |
Sander de Smalen | 891af03a | 2018-02-03 13:55:59 +0000 | [diff] [blame] | 1310 | void CodeGenFunction::EmitAndRegisterVariableArrayDimensions( |
| 1311 | CGDebugInfo *DI, const VarDecl &D, bool EmitDebugInfo) { |
| 1312 | // For each dimension stores its QualType and corresponding |
| 1313 | // size-expression Value. |
| 1314 | SmallVector<CodeGenFunction::VlaSizePair, 4> Dimensions; |
Adrian Prantl | a40cce8 | 2018-11-09 19:17:56 +0000 | [diff] [blame] | 1315 | SmallVector<IdentifierInfo *, 4> VLAExprNames; |
Sander de Smalen | 891af03a | 2018-02-03 13:55:59 +0000 | [diff] [blame] | 1316 | |
| 1317 | // Break down the array into individual dimensions. |
| 1318 | QualType Type1D = D.getType(); |
| 1319 | while (getContext().getAsVariableArrayType(Type1D)) { |
| 1320 | auto VlaSize = getVLAElements1D(Type1D); |
| 1321 | if (auto *C = dyn_cast<llvm::ConstantInt>(VlaSize.NumElts)) |
| 1322 | Dimensions.emplace_back(C, Type1D.getUnqualifiedType()); |
| 1323 | else { |
Adrian Prantl | a40cce8 | 2018-11-09 19:17:56 +0000 | [diff] [blame] | 1324 | // Generate a locally unique name for the size expression. |
| 1325 | Twine Name = Twine("__vla_expr") + Twine(VLAExprCounter++); |
| 1326 | SmallString<12> Buffer; |
| 1327 | StringRef NameRef = Name.toStringRef(Buffer); |
| 1328 | auto &Ident = getContext().Idents.getOwn(NameRef); |
| 1329 | VLAExprNames.push_back(&Ident); |
| 1330 | auto SizeExprAddr = |
| 1331 | CreateDefaultAlignTempAlloca(VlaSize.NumElts->getType(), NameRef); |
Sander de Smalen | 891af03a | 2018-02-03 13:55:59 +0000 | [diff] [blame] | 1332 | Builder.CreateStore(VlaSize.NumElts, SizeExprAddr); |
| 1333 | Dimensions.emplace_back(SizeExprAddr.getPointer(), |
| 1334 | Type1D.getUnqualifiedType()); |
| 1335 | } |
| 1336 | Type1D = VlaSize.Type; |
| 1337 | } |
| 1338 | |
| 1339 | if (!EmitDebugInfo) |
| 1340 | return; |
| 1341 | |
| 1342 | // Register each dimension's size-expression with a DILocalVariable, |
| 1343 | // so that it can be used by CGDebugInfo when instantiating a DISubrange |
| 1344 | // to describe this array. |
Adrian Prantl | a40cce8 | 2018-11-09 19:17:56 +0000 | [diff] [blame] | 1345 | unsigned NameIdx = 0; |
Sander de Smalen | 891af03a | 2018-02-03 13:55:59 +0000 | [diff] [blame] | 1346 | for (auto &VlaSize : Dimensions) { |
| 1347 | llvm::Metadata *MD; |
| 1348 | if (auto *C = dyn_cast<llvm::ConstantInt>(VlaSize.NumElts)) |
| 1349 | MD = llvm::ConstantAsMetadata::get(C); |
| 1350 | else { |
| 1351 | // Create an artificial VarDecl to generate debug info for. |
Adrian Prantl | a40cce8 | 2018-11-09 19:17:56 +0000 | [diff] [blame] | 1352 | IdentifierInfo *NameIdent = VLAExprNames[NameIdx++]; |
Sander de Smalen | 891af03a | 2018-02-03 13:55:59 +0000 | [diff] [blame] | 1353 | auto VlaExprTy = VlaSize.NumElts->getType()->getPointerElementType(); |
| 1354 | auto QT = getContext().getIntTypeForBitwidth( |
| 1355 | VlaExprTy->getScalarSizeInBits(), false); |
| 1356 | auto *ArtificialDecl = VarDecl::Create( |
| 1357 | getContext(), const_cast<DeclContext *>(D.getDeclContext()), |
Adrian Prantl | a40cce8 | 2018-11-09 19:17:56 +0000 | [diff] [blame] | 1358 | D.getLocation(), D.getLocation(), NameIdent, QT, |
Sander de Smalen | 891af03a | 2018-02-03 13:55:59 +0000 | [diff] [blame] | 1359 | getContext().CreateTypeSourceInfo(QT), SC_Auto); |
Sander de Smalen | 9084a3b | 2018-02-13 07:49:34 +0000 | [diff] [blame] | 1360 | ArtificialDecl->setImplicit(); |
Sander de Smalen | 891af03a | 2018-02-03 13:55:59 +0000 | [diff] [blame] | 1361 | |
| 1362 | MD = DI->EmitDeclareOfAutoVariable(ArtificialDecl, VlaSize.NumElts, |
| 1363 | Builder); |
| 1364 | } |
| 1365 | assert(MD && "No Size expression debug node created"); |
| 1366 | DI->registerVLASizeExpression(VlaSize.Type, MD); |
| 1367 | } |
| 1368 | } |
| 1369 | |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1370 | /// EmitAutoVarAlloca - Emit the alloca and debug information for a |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 1371 | /// local variable. Does not emit initialization or destruction. |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1372 | CodeGenFunction::AutoVarEmission |
| 1373 | CodeGenFunction::EmitAutoVarAlloca(const VarDecl &D) { |
| 1374 | QualType Ty = D.getType(); |
Yaxun Liu | b7318e0 | 2017-10-13 03:37:48 +0000 | [diff] [blame] | 1375 | assert( |
| 1376 | Ty.getAddressSpace() == LangAS::Default || |
| 1377 | (Ty.getAddressSpace() == LangAS::opencl_private && getLangOpts().OpenCL)); |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1378 | |
| 1379 | AutoVarEmission emission(D); |
| 1380 | |
Akira Hatanaka | 8e57b07 | 2018-10-01 21:51:28 +0000 | [diff] [blame] | 1381 | bool isEscapingByRef = D.isEscapingByref(); |
| 1382 | emission.IsEscapingByRef = isEscapingByRef; |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1383 | |
| 1384 | CharUnits alignment = getContext().getDeclAlign(&D); |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1385 | |
John McCall | 23c29fe | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 1386 | // If the type is variably-modified, emit all the VLA sizes for it. |
| 1387 | if (Ty->isVariablyModifiedType()) |
| 1388 | EmitVariablyModifiedType(Ty); |
| 1389 | |
Sander de Smalen | 891af03a | 2018-02-03 13:55:59 +0000 | [diff] [blame] | 1390 | auto *DI = getDebugInfo(); |
| 1391 | bool EmitDebugInfo = DI && CGM.getCodeGenOpts().getDebugInfo() >= |
| 1392 | codegenoptions::LimitedDebugInfo; |
| 1393 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1394 | Address address = Address::invalid(); |
Yaxun Liu | a2a9cfa | 2018-05-17 11:16:35 +0000 | [diff] [blame] | 1395 | Address AllocaAddr = Address::invalid(); |
Alexey Bataev | 9c39781 | 2019-04-03 17:57:06 +0000 | [diff] [blame] | 1396 | Address OpenMPLocalAddr = |
| 1397 | getLangOpts().OpenMP |
| 1398 | ? CGM.getOpenMPRuntime().getAddressOfLocalVariable(*this, &D) |
| 1399 | : Address::invalid(); |
Amy Huang | 7fac5c8 | 2019-06-20 17:15:21 +0000 | [diff] [blame] | 1400 | bool NRVO = getLangOpts().ElideConstructors && D.isNRVOVariable(); |
| 1401 | |
Alexey Bataev | 9c39781 | 2019-04-03 17:57:06 +0000 | [diff] [blame] | 1402 | if (getLangOpts().OpenMP && OpenMPLocalAddr.isValid()) { |
| 1403 | address = OpenMPLocalAddr; |
| 1404 | } else if (Ty->isConstantSizeType()) { |
Richard Smith | 2bcde3a | 2013-06-02 00:09:52 +0000 | [diff] [blame] | 1405 | // If this value is an array or struct with a statically determinable |
| 1406 | // constant initializer, there are optimizations we can do. |
Rafael Espindola | 609f5d9 | 2013-03-26 18:41:47 +0000 | [diff] [blame] | 1407 | // |
| 1408 | // TODO: We should constant-evaluate the initializer of any variable, |
| 1409 | // as long as it is initialized by a constant expression. Currently, |
| 1410 | // isConstantInitializer produces wrong answers for structs with |
| 1411 | // reference or bitfield members, and a few other cases, and checking |
| 1412 | // for POD-ness protects us from some of these. |
Richard Smith | 2bcde3a | 2013-06-02 00:09:52 +0000 | [diff] [blame] | 1413 | if (D.getInit() && (Ty->isArrayType() || Ty->isRecordType()) && |
| 1414 | (D.isConstexpr() || |
| 1415 | ((Ty.isPODType(getContext()) || |
| 1416 | getContext().getBaseElementType(Ty)->isObjCObjectPointerType()) && |
| 1417 | D.getInit()->isConstantInitializer(getContext(), false)))) { |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1418 | |
Rafael Espindola | 609f5d9 | 2013-03-26 18:41:47 +0000 | [diff] [blame] | 1419 | // If the variable's a const type, and it's neither an NRVO |
| 1420 | // candidate nor a __block variable and has no mutable members, |
| 1421 | // emit it as a global instead. |
Anastasia Stulova | e4a1c38 | 2016-11-29 17:01:19 +0000 | [diff] [blame] | 1422 | // Exception is if a variable is located in non-constant address space |
| 1423 | // in OpenCL. |
| 1424 | if ((!getLangOpts().OpenCL || |
| 1425 | Ty.getAddressSpace() == LangAS::opencl_constant) && |
Akira Hatanaka | 8e57b07 | 2018-10-01 21:51:28 +0000 | [diff] [blame] | 1426 | (CGM.getCodeGenOpts().MergeAllConstants && !NRVO && |
| 1427 | !isEscapingByRef && CGM.isTypeConstant(Ty, true))) { |
Rafael Espindola | 609f5d9 | 2013-03-26 18:41:47 +0000 | [diff] [blame] | 1428 | EmitStaticVarDecl(D, llvm::GlobalValue::InternalLinkage); |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1429 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1430 | // Signal this condition to later callbacks. |
| 1431 | emission.Addr = Address::invalid(); |
Rafael Espindola | 609f5d9 | 2013-03-26 18:41:47 +0000 | [diff] [blame] | 1432 | assert(emission.wasEmittedAsGlobal()); |
| 1433 | return emission; |
Tanya Lattner | f9d41df | 2009-11-04 01:18:09 +0000 | [diff] [blame] | 1434 | } |
Eric Christopher | 31ab130 | 2011-08-23 22:38:00 +0000 | [diff] [blame] | 1435 | |
Rafael Espindola | 609f5d9 | 2013-03-26 18:41:47 +0000 | [diff] [blame] | 1436 | // Otherwise, tell the initialization code that we're in this case. |
| 1437 | emission.IsConstantAggregate = true; |
| 1438 | } |
Eric Christopher | 31ab130 | 2011-08-23 22:38:00 +0000 | [diff] [blame] | 1439 | |
Rafael Espindola | 609f5d9 | 2013-03-26 18:41:47 +0000 | [diff] [blame] | 1440 | // A normal fixed sized variable becomes an alloca in the entry block, |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 1441 | // unless: |
| 1442 | // - it's an NRVO variable. |
| 1443 | // - we are compiling OpenMP and it's an OpenMP local variable. |
Alexey Bataev | 9c39781 | 2019-04-03 17:57:06 +0000 | [diff] [blame] | 1444 | if (NRVO) { |
Rafael Espindola | 609f5d9 | 2013-03-26 18:41:47 +0000 | [diff] [blame] | 1445 | // The named return value optimization: allocate this variable in the |
| 1446 | // return slot, so that we can elide the copy when returning this |
| 1447 | // variable (C++0x [class.copy]p34). |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1448 | address = ReturnValue; |
Eric Christopher | 31ab130 | 2011-08-23 22:38:00 +0000 | [diff] [blame] | 1449 | |
Rafael Espindola | 609f5d9 | 2013-03-26 18:41:47 +0000 | [diff] [blame] | 1450 | if (const RecordType *RecordTy = Ty->getAs<RecordType>()) { |
Akira Hatanaka | 673af7a | 2018-03-29 17:56:24 +0000 | [diff] [blame] | 1451 | const auto *RD = RecordTy->getDecl(); |
| 1452 | const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD); |
| 1453 | if ((CXXRD && !CXXRD->hasTrivialDestructor()) || |
| 1454 | RD->isNonTrivialToPrimitiveDestroy()) { |
Rafael Espindola | 609f5d9 | 2013-03-26 18:41:47 +0000 | [diff] [blame] | 1455 | // Create a flag that is used to indicate when the NRVO was applied |
| 1456 | // to this variable. Set it to zero to indicate that NRVO was not |
| 1457 | // applied. |
| 1458 | llvm::Value *Zero = Builder.getFalse(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1459 | Address NRVOFlag = |
| 1460 | CreateTempAlloca(Zero->getType(), CharUnits::One(), "nrvo"); |
Rafael Espindola | 609f5d9 | 2013-03-26 18:41:47 +0000 | [diff] [blame] | 1461 | EnsureInsertPoint(); |
| 1462 | Builder.CreateStore(Zero, NRVOFlag); |
Eric Christopher | 31ab130 | 2011-08-23 22:38:00 +0000 | [diff] [blame] | 1463 | |
Rafael Espindola | 609f5d9 | 2013-03-26 18:41:47 +0000 | [diff] [blame] | 1464 | // Record the NRVO flag for this variable. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1465 | NRVOFlags[&D] = NRVOFlag.getPointer(); |
| 1466 | emission.NRVOFlag = NRVOFlag.getPointer(); |
Nadav Rotem | 1da3094 | 2013-03-23 06:43:35 +0000 | [diff] [blame] | 1467 | } |
Douglas Gregor | 290c93e | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 1468 | } |
Chris Lattner | b781dc79 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 1469 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1470 | CharUnits allocaAlignment; |
| 1471 | llvm::Type *allocaTy; |
Akira Hatanaka | 8e57b07 | 2018-10-01 21:51:28 +0000 | [diff] [blame] | 1472 | if (isEscapingByRef) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1473 | auto &byrefInfo = getBlockByrefInfo(&D); |
| 1474 | allocaTy = byrefInfo.Type; |
| 1475 | allocaAlignment = byrefInfo.ByrefAlignment; |
| 1476 | } else { |
| 1477 | allocaTy = ConvertTypeForMem(Ty); |
| 1478 | allocaAlignment = alignment; |
| 1479 | } |
Rafael Espindola | 609f5d9 | 2013-03-26 18:41:47 +0000 | [diff] [blame] | 1480 | |
John McCall | 999110f | 2015-09-08 09:18:30 +0000 | [diff] [blame] | 1481 | // Create the alloca. Note that we set the name separately from |
| 1482 | // building the instruction so that it's there even in no-asserts |
| 1483 | // builds. |
Yaxun Liu | a2a9cfa | 2018-05-17 11:16:35 +0000 | [diff] [blame] | 1484 | address = CreateTempAlloca(allocaTy, allocaAlignment, D.getName(), |
| 1485 | /*ArraySize=*/nullptr, &AllocaAddr); |
Rafael Espindola | 609f5d9 | 2013-03-26 18:41:47 +0000 | [diff] [blame] | 1486 | |
Reid Kleckner | 1f88e93 | 2015-10-07 21:03:41 +0000 | [diff] [blame] | 1487 | // Don't emit lifetime markers for MSVC catch parameters. The lifetime of |
| 1488 | // the catch parameter starts in the catchpad instruction, and we can't |
| 1489 | // insert code in those basic blocks. |
| 1490 | bool IsMSCatchParam = |
| 1491 | D.isExceptionVariable() && getTarget().getCXXABI().isMicrosoft(); |
| 1492 | |
Vitaly Buka | 64c80b4 | 2016-10-26 05:42:30 +0000 | [diff] [blame] | 1493 | // Emit a lifetime intrinsic if meaningful. There's no point in doing this |
| 1494 | // if we don't have a valid insertion point (?). |
Reid Kleckner | 1f88e93 | 2015-10-07 21:03:41 +0000 | [diff] [blame] | 1495 | if (HaveInsertPoint() && !IsMSCatchParam) { |
Akira Hatanaka | fdcd18b | 2017-01-25 22:55:13 +0000 | [diff] [blame] | 1496 | // If there's a jump into the lifetime of this variable, its lifetime |
| 1497 | // gets broken up into several regions in IR, which requires more work |
| 1498 | // to handle correctly. For now, just omit the intrinsics; this is a |
| 1499 | // rare case, and it's better to just be conservatively correct. |
| 1500 | // PR28267. |
| 1501 | // |
| 1502 | // We have to do this in all language modes if there's a jump past the |
| 1503 | // declaration. We also have to do it in C if there's a jump to an |
| 1504 | // earlier point in the current block because non-VLA lifetimes begin as |
| 1505 | // soon as the containing block is entered, not when its variables |
| 1506 | // actually come into scope; suppressing the lifetime annotations |
| 1507 | // completely in this case is unnecessarily pessimistic, but again, this |
| 1508 | // is rare. |
| 1509 | if (!Bypasses.IsBypassed(&D) && |
| 1510 | !(!getLangOpts().CPlusPlus && hasLabelBeenSeenInCurrentScope())) { |
Vitaly Buka | 64c80b4 | 2016-10-26 05:42:30 +0000 | [diff] [blame] | 1511 | uint64_t size = CGM.getDataLayout().getTypeAllocSize(allocaTy); |
| 1512 | emission.SizeForLifetimeMarkers = |
Yaxun Liu | a2a9cfa | 2018-05-17 11:16:35 +0000 | [diff] [blame] | 1513 | EmitLifetimeStart(size, AllocaAddr.getPointer()); |
Vitaly Buka | 64c80b4 | 2016-10-26 05:42:30 +0000 | [diff] [blame] | 1514 | } |
Arnaud A. de Grandmaison | e69ec55 | 2014-10-08 14:04:26 +0000 | [diff] [blame] | 1515 | } else { |
Rafael Espindola | 609f5d9 | 2013-03-26 18:41:47 +0000 | [diff] [blame] | 1516 | assert(!emission.useLifetimeMarkers()); |
Arnaud A. de Grandmaison | e69ec55 | 2014-10-08 14:04:26 +0000 | [diff] [blame] | 1517 | } |
Chris Lattner | b781dc79 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 1518 | } |
Chris Lattner | 03df122 | 2007-06-02 04:53:11 +0000 | [diff] [blame] | 1519 | } else { |
Daniel Dunbar | b6adc43 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 1520 | EnsureInsertPoint(); |
| 1521 | |
Anders Carlsson | 15949b3 | 2009-02-09 20:41:50 +0000 | [diff] [blame] | 1522 | if (!DidCallStackSave) { |
Anders Carlsson | 3003288 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 1523 | // Save the stack. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1524 | Address Stack = |
| 1525 | CreateTempAlloca(Int8PtrTy, getPointerAlign(), "saved_stack"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1526 | |
James Y Knight | 8799cae | 2019-02-03 21:53:49 +0000 | [diff] [blame] | 1527 | llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave); |
David Blaikie | 4ba525b | 2015-07-14 17:27:39 +0000 | [diff] [blame] | 1528 | llvm::Value *V = Builder.CreateCall(F); |
Anders Carlsson | 3003288 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 1529 | Builder.CreateStore(V, Stack); |
Anders Carlsson | 15949b3 | 2009-02-09 20:41:50 +0000 | [diff] [blame] | 1530 | |
| 1531 | DidCallStackSave = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1532 | |
John McCall | a464ff9 | 2010-07-21 06:13:08 +0000 | [diff] [blame] | 1533 | // Push a cleanup block and restore the stack there. |
John McCall | e4df6c8 | 2011-01-28 08:37:24 +0000 | [diff] [blame] | 1534 | // FIXME: in general circumstances, this should be an EH cleanup. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1535 | pushStackRestore(NormalCleanup, Stack); |
Anders Carlsson | 3003288 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 1536 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1537 | |
Sander de Smalen | 891af03a | 2018-02-03 13:55:59 +0000 | [diff] [blame] | 1538 | auto VlaSize = getVLASize(Ty); |
| 1539 | llvm::Type *llvmTy = ConvertTypeForMem(VlaSize.Type); |
Anders Carlsson | 3003288 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 1540 | |
| 1541 | // Allocate memory for the array. |
Yaxun Liu | a2a9cfa | 2018-05-17 11:16:35 +0000 | [diff] [blame] | 1542 | address = CreateTempAlloca(llvmTy, alignment, "vla", VlaSize.NumElts, |
| 1543 | &AllocaAddr); |
Sander de Smalen | 891af03a | 2018-02-03 13:55:59 +0000 | [diff] [blame] | 1544 | |
| 1545 | // If we have debug info enabled, properly describe the VLA dimensions for |
| 1546 | // this type by registering the vla size expression for each of the |
| 1547 | // dimensions. |
| 1548 | EmitAndRegisterVariableArrayDimensions(DI, D, EmitDebugInfo); |
Chris Lattner | 03df122 | 2007-06-02 04:53:11 +0000 | [diff] [blame] | 1549 | } |
Eli Friedman | f408470 | 2008-12-20 23:11:59 +0000 | [diff] [blame] | 1550 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1551 | setAddrOfLocalVar(&D, address); |
| 1552 | emission.Addr = address; |
Yaxun Liu | a2a9cfa | 2018-05-17 11:16:35 +0000 | [diff] [blame] | 1553 | emission.AllocaAddr = AllocaAddr; |
Sanjiv Gupta | 18de624 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 1554 | |
| 1555 | // Emit debug info for local var declaration. |
Sander de Smalen | 891af03a | 2018-02-03 13:55:59 +0000 | [diff] [blame] | 1556 | if (EmitDebugInfo && HaveInsertPoint()) { |
Amy Huang | 7fac5c8 | 2019-06-20 17:15:21 +0000 | [diff] [blame] | 1557 | Address DebugAddr = address; |
| 1558 | bool UsePointerValue = NRVO && ReturnValuePointer.isValid(); |
Sander de Smalen | 891af03a | 2018-02-03 13:55:59 +0000 | [diff] [blame] | 1559 | DI->setLocation(D.getLocation()); |
Amy Huang | 7fac5c8 | 2019-06-20 17:15:21 +0000 | [diff] [blame] | 1560 | |
| 1561 | // If NRVO, use a pointer to the return address. |
| 1562 | if (UsePointerValue) |
| 1563 | DebugAddr = ReturnValuePointer; |
| 1564 | |
| 1565 | (void)DI->EmitDeclareOfAutoVariable(&D, DebugAddr.getPointer(), Builder, |
| 1566 | UsePointerValue); |
Sander de Smalen | 891af03a | 2018-02-03 13:55:59 +0000 | [diff] [blame] | 1567 | } |
Sanjiv Gupta | 18de624 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 1568 | |
Volodymyr Sapsai | 93317d8 | 2019-03-01 02:15:39 +0000 | [diff] [blame] | 1569 | if (D.hasAttr<AnnotateAttr>() && HaveInsertPoint()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1570 | EmitVarAnnotations(&D, address.getPointer()); |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1571 | |
Kuba Mracek | 5e5e4e7 | 2017-04-14 16:53:25 +0000 | [diff] [blame] | 1572 | // Make sure we call @llvm.lifetime.end. |
| 1573 | if (emission.useLifetimeMarkers()) |
| 1574 | EHStack.pushCleanup<CallLifetimeEnd>(NormalEHLifetimeMarker, |
Yaxun Liu | a2a9cfa | 2018-05-17 11:16:35 +0000 | [diff] [blame] | 1575 | emission.getOriginalAllocatedAddress(), |
Kuba Mracek | 5e5e4e7 | 2017-04-14 16:53:25 +0000 | [diff] [blame] | 1576 | emission.getSizeForLifetimeMarkers()); |
| 1577 | |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1578 | return emission; |
| 1579 | } |
| 1580 | |
JF Bastien | 6b23972 | 2018-05-19 04:21:26 +0000 | [diff] [blame] | 1581 | static bool isCapturedBy(const VarDecl &, const Expr *); |
| 1582 | |
| 1583 | /// Determines whether the given __block variable is potentially |
| 1584 | /// captured by the given statement. |
| 1585 | static bool isCapturedBy(const VarDecl &Var, const Stmt *S) { |
| 1586 | if (const Expr *E = dyn_cast<Expr>(S)) |
| 1587 | return isCapturedBy(Var, E); |
| 1588 | for (const Stmt *SubStmt : S->children()) |
| 1589 | if (isCapturedBy(Var, SubStmt)) |
| 1590 | return true; |
| 1591 | return false; |
| 1592 | } |
| 1593 | |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1594 | /// Determines whether the given __block variable is potentially |
| 1595 | /// captured by the given expression. |
JF Bastien | 6b23972 | 2018-05-19 04:21:26 +0000 | [diff] [blame] | 1596 | static bool isCapturedBy(const VarDecl &Var, const Expr *E) { |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1597 | // Skip the most common kinds of expressions that make |
| 1598 | // hierarchy-walking expensive. |
JF Bastien | 6b23972 | 2018-05-19 04:21:26 +0000 | [diff] [blame] | 1599 | E = E->IgnoreParenCasts(); |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1600 | |
JF Bastien | 6b23972 | 2018-05-19 04:21:26 +0000 | [diff] [blame] | 1601 | if (const BlockExpr *BE = dyn_cast<BlockExpr>(E)) { |
| 1602 | const BlockDecl *Block = BE->getBlockDecl(); |
| 1603 | for (const auto &I : Block->captures()) { |
| 1604 | if (I.getVariable() == &Var) |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1605 | return true; |
| 1606 | } |
| 1607 | |
| 1608 | // No need to walk into the subexpressions. |
| 1609 | return false; |
| 1610 | } |
| 1611 | |
JF Bastien | 6b23972 | 2018-05-19 04:21:26 +0000 | [diff] [blame] | 1612 | if (const StmtExpr *SE = dyn_cast<StmtExpr>(E)) { |
Fariborz Jahanian | 797f1e2 | 2011-08-23 16:47:15 +0000 | [diff] [blame] | 1613 | const CompoundStmt *CS = SE->getSubStmt(); |
Aaron Ballman | c7e4e21 | 2014-03-17 14:19:37 +0000 | [diff] [blame] | 1614 | for (const auto *BI : CS->body()) |
JF Bastien | 6b23972 | 2018-05-19 04:21:26 +0000 | [diff] [blame] | 1615 | if (const auto *BIE = dyn_cast<Expr>(BI)) { |
| 1616 | if (isCapturedBy(Var, BIE)) |
| 1617 | return true; |
Fariborz Jahanian | 5c4b2ca | 2011-08-25 00:06:26 +0000 | [diff] [blame] | 1618 | } |
Aaron Ballman | c7e4e21 | 2014-03-17 14:19:37 +0000 | [diff] [blame] | 1619 | else if (const auto *DS = dyn_cast<DeclStmt>(BI)) { |
Fariborz Jahanian | 5c4b2ca | 2011-08-25 00:06:26 +0000 | [diff] [blame] | 1620 | // special case declarations |
Aaron Ballman | 535bbcc | 2014-03-14 17:01:24 +0000 | [diff] [blame] | 1621 | for (const auto *I : DS->decls()) { |
| 1622 | if (const auto *VD = dyn_cast<VarDecl>((I))) { |
| 1623 | const Expr *Init = VD->getInit(); |
JF Bastien | 6b23972 | 2018-05-19 04:21:26 +0000 | [diff] [blame] | 1624 | if (Init && isCapturedBy(Var, Init)) |
Fariborz Jahanian | 5c4b2ca | 2011-08-25 00:06:26 +0000 | [diff] [blame] | 1625 | return true; |
| 1626 | } |
| 1627 | } |
| 1628 | } |
| 1629 | else |
| 1630 | // FIXME. Make safe assumption assuming arbitrary statements cause capturing. |
| 1631 | // Later, provide code to poke into statements for capture analysis. |
| 1632 | return true; |
Fariborz Jahanian | 797f1e2 | 2011-08-23 16:47:15 +0000 | [diff] [blame] | 1633 | return false; |
| 1634 | } |
Eric Christopher | 31ab130 | 2011-08-23 22:38:00 +0000 | [diff] [blame] | 1635 | |
JF Bastien | 6b23972 | 2018-05-19 04:21:26 +0000 | [diff] [blame] | 1636 | for (const Stmt *SubStmt : E->children()) |
| 1637 | if (isCapturedBy(Var, SubStmt)) |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1638 | return true; |
| 1639 | |
| 1640 | return false; |
| 1641 | } |
| 1642 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1643 | /// Determine whether the given initializer is trivial in the sense |
Douglas Gregor | 82e1af2 | 2011-07-01 21:08:19 +0000 | [diff] [blame] | 1644 | /// that it requires no code to be generated. |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 1645 | bool CodeGenFunction::isTrivialInitializer(const Expr *Init) { |
Douglas Gregor | 82e1af2 | 2011-07-01 21:08:19 +0000 | [diff] [blame] | 1646 | if (!Init) |
| 1647 | return true; |
Eric Christopher | 31ab130 | 2011-08-23 22:38:00 +0000 | [diff] [blame] | 1648 | |
Douglas Gregor | 82e1af2 | 2011-07-01 21:08:19 +0000 | [diff] [blame] | 1649 | if (const CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) |
| 1650 | if (CXXConstructorDecl *Constructor = Construct->getConstructor()) |
| 1651 | if (Constructor->isTrivial() && |
| 1652 | Constructor->isDefaultConstructor() && |
| 1653 | !Construct->requiresZeroInitialization()) |
| 1654 | return true; |
Eric Christopher | 31ab130 | 2011-08-23 22:38:00 +0000 | [diff] [blame] | 1655 | |
Douglas Gregor | 82e1af2 | 2011-07-01 21:08:19 +0000 | [diff] [blame] | 1656 | return false; |
| 1657 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 1658 | |
Vitaly Buka | e726766 | 2019-07-10 22:53:50 +0000 | [diff] [blame] | 1659 | void CodeGenFunction::emitZeroOrPatternForAutoVarInit(QualType type, |
| 1660 | const VarDecl &D, |
| 1661 | Address Loc) { |
| 1662 | auto trivialAutoVarInit = getContext().getLangOpts().getTrivialAutoVarInit(); |
| 1663 | CharUnits Size = getContext().getTypeSizeInChars(type); |
| 1664 | bool isVolatile = type.isVolatileQualified(); |
| 1665 | if (!Size.isZero()) { |
| 1666 | switch (trivialAutoVarInit) { |
| 1667 | case LangOptions::TrivialAutoVarInitKind::Uninitialized: |
| 1668 | llvm_unreachable("Uninitialized handled by caller"); |
| 1669 | case LangOptions::TrivialAutoVarInitKind::Zero: |
| 1670 | emitStoresForZeroInit(CGM, D, Loc, isVolatile, Builder); |
| 1671 | break; |
| 1672 | case LangOptions::TrivialAutoVarInitKind::Pattern: |
| 1673 | emitStoresForPatternInit(CGM, D, Loc, isVolatile, Builder); |
| 1674 | break; |
| 1675 | } |
| 1676 | return; |
| 1677 | } |
| 1678 | |
| 1679 | // VLAs look zero-sized to getTypeInfo. We can't emit constant stores to |
| 1680 | // them, so emit a memcpy with the VLA size to initialize each element. |
| 1681 | // Technically zero-sized or negative-sized VLAs are undefined, and UBSan |
| 1682 | // will catch that code, but there exists code which generates zero-sized |
| 1683 | // VLAs. Be nice and initialize whatever they requested. |
| 1684 | const auto *VlaType = getContext().getAsVariableArrayType(type); |
| 1685 | if (!VlaType) |
| 1686 | return; |
| 1687 | auto VlaSize = getVLASize(VlaType); |
| 1688 | auto SizeVal = VlaSize.NumElts; |
| 1689 | CharUnits EltSize = getContext().getTypeSizeInChars(VlaSize.Type); |
| 1690 | switch (trivialAutoVarInit) { |
| 1691 | case LangOptions::TrivialAutoVarInitKind::Uninitialized: |
| 1692 | llvm_unreachable("Uninitialized handled by caller"); |
| 1693 | |
| 1694 | case LangOptions::TrivialAutoVarInitKind::Zero: |
| 1695 | if (!EltSize.isOne()) |
| 1696 | SizeVal = Builder.CreateNUWMul(SizeVal, CGM.getSize(EltSize)); |
| 1697 | Builder.CreateMemSet(Loc, llvm::ConstantInt::get(Int8Ty, 0), SizeVal, |
| 1698 | isVolatile); |
| 1699 | break; |
| 1700 | |
| 1701 | case LangOptions::TrivialAutoVarInitKind::Pattern: { |
| 1702 | llvm::Type *ElTy = Loc.getElementType(); |
| 1703 | llvm::Constant *Constant = constWithPadding( |
| 1704 | CGM, IsPattern::Yes, initializationPatternFor(CGM, ElTy)); |
| 1705 | CharUnits ConstantAlign = getContext().getTypeAlignInChars(VlaSize.Type); |
| 1706 | llvm::BasicBlock *SetupBB = createBasicBlock("vla-setup.loop"); |
| 1707 | llvm::BasicBlock *LoopBB = createBasicBlock("vla-init.loop"); |
| 1708 | llvm::BasicBlock *ContBB = createBasicBlock("vla-init.cont"); |
| 1709 | llvm::Value *IsZeroSizedVLA = Builder.CreateICmpEQ( |
| 1710 | SizeVal, llvm::ConstantInt::get(SizeVal->getType(), 0), |
| 1711 | "vla.iszerosized"); |
| 1712 | Builder.CreateCondBr(IsZeroSizedVLA, ContBB, SetupBB); |
| 1713 | EmitBlock(SetupBB); |
| 1714 | if (!EltSize.isOne()) |
| 1715 | SizeVal = Builder.CreateNUWMul(SizeVal, CGM.getSize(EltSize)); |
| 1716 | llvm::Value *BaseSizeInChars = |
| 1717 | llvm::ConstantInt::get(IntPtrTy, EltSize.getQuantity()); |
| 1718 | Address Begin = Builder.CreateElementBitCast(Loc, Int8Ty, "vla.begin"); |
| 1719 | llvm::Value *End = |
| 1720 | Builder.CreateInBoundsGEP(Begin.getPointer(), SizeVal, "vla.end"); |
| 1721 | llvm::BasicBlock *OriginBB = Builder.GetInsertBlock(); |
| 1722 | EmitBlock(LoopBB); |
| 1723 | llvm::PHINode *Cur = Builder.CreatePHI(Begin.getType(), 2, "vla.cur"); |
| 1724 | Cur->addIncoming(Begin.getPointer(), OriginBB); |
| 1725 | CharUnits CurAlign = Loc.getAlignment().alignmentOfArrayElement(EltSize); |
| 1726 | Builder.CreateMemCpy(Address(Cur, CurAlign), |
| 1727 | createUnnamedGlobalForMemcpyFrom( |
| 1728 | CGM, D, Builder, Constant, ConstantAlign), |
| 1729 | BaseSizeInChars, isVolatile); |
| 1730 | llvm::Value *Next = |
| 1731 | Builder.CreateInBoundsGEP(Int8Ty, Cur, BaseSizeInChars, "vla.next"); |
| 1732 | llvm::Value *Done = Builder.CreateICmpEQ(Next, End, "vla-init.isdone"); |
| 1733 | Builder.CreateCondBr(Done, ContBB, LoopBB); |
| 1734 | Cur->addIncoming(Next, LoopBB); |
| 1735 | EmitBlock(ContBB); |
| 1736 | } break; |
| 1737 | } |
| 1738 | } |
| 1739 | |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1740 | void CodeGenFunction::EmitAutoVarInit(const AutoVarEmission &emission) { |
John McCall | 9e2e22f | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 1741 | assert(emission.Variable && "emission was not valid!"); |
| 1742 | |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1743 | // If this was emitted as a global constant, we're done. |
| 1744 | if (emission.wasEmittedAsGlobal()) return; |
| 1745 | |
John McCall | 9e2e22f | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 1746 | const VarDecl &D = *emission.Variable; |
Adrian Prantl | 95b24e9 | 2015-02-03 20:00:54 +0000 | [diff] [blame] | 1747 | auto DL = ApplyDebugLocation::CreateDefaultArtificial(*this, D.getLocation()); |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1748 | QualType type = D.getType(); |
| 1749 | |
Chris Lattner | 07eb733 | 2007-07-12 00:39:48 +0000 | [diff] [blame] | 1750 | // If this local has an initializer, emit it now. |
Daniel Dunbar | b6adc43 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 1751 | const Expr *Init = D.getInit(); |
| 1752 | |
| 1753 | // If we are at an unreachable point, we don't need to emit the initializer |
| 1754 | // unless it contains a label. |
| 1755 | if (!HaveInsertPoint()) { |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1756 | if (!Init || !ContainsLabel(Init)) return; |
| 1757 | EnsureInsertPoint(); |
Daniel Dunbar | b6adc43 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 1758 | } |
| 1759 | |
John McCall | 7306487 | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 1760 | // Initialize the structure of a __block variable. |
Akira Hatanaka | 8e57b07 | 2018-10-01 21:51:28 +0000 | [diff] [blame] | 1761 | if (emission.IsEscapingByRef) |
John McCall | 7306487 | 2011-03-31 01:59:53 +0000 | [diff] [blame] | 1762 | emitByrefStructureInit(emission); |
Anders Carlsson | cadb9a6 | 2009-02-07 23:51:38 +0000 | [diff] [blame] | 1763 | |
Akira Hatanaka | 7275da0 | 2018-02-28 07:15:55 +0000 | [diff] [blame] | 1764 | // Initialize the variable here if it doesn't have a initializer and it is a |
| 1765 | // C struct that is non-trivial to initialize or an array containing such a |
| 1766 | // struct. |
| 1767 | if (!Init && |
| 1768 | type.isNonTrivialToPrimitiveDefaultInitialize() == |
| 1769 | QualType::PDIK_Struct) { |
| 1770 | LValue Dst = MakeAddrLValue(emission.getAllocatedAddress(), type); |
Akira Hatanaka | 8e57b07 | 2018-10-01 21:51:28 +0000 | [diff] [blame] | 1771 | if (emission.IsEscapingByRef) |
Akira Hatanaka | 7275da0 | 2018-02-28 07:15:55 +0000 | [diff] [blame] | 1772 | drillIntoBlockVariable(*this, Dst, &D); |
| 1773 | defaultInitNonTrivialCStructVar(Dst); |
| 1774 | return; |
| 1775 | } |
| 1776 | |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1777 | // Check whether this is a byref variable that's potentially |
| 1778 | // captured and moved by its own initializer. If so, we'll need to |
| 1779 | // emit the initializer first, then copy into the variable. |
JF Bastien | 14daa20 | 2018-12-18 05:12:21 +0000 | [diff] [blame] | 1780 | bool capturedByInit = |
| 1781 | Init && emission.IsEscapingByRef && isCapturedBy(D, Init); |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1782 | |
JF Bastien | 0bae08a | 2019-02-15 17:26:29 +0000 | [diff] [blame] | 1783 | bool locIsByrefHeader = !capturedByInit; |
| 1784 | const Address Loc = |
| 1785 | locIsByrefHeader ? emission.getObjectAddress(*this) : emission.Addr; |
JF Bastien | 14daa20 | 2018-12-18 05:12:21 +0000 | [diff] [blame] | 1786 | |
| 1787 | // Note: constexpr already initializes everything correctly. |
| 1788 | LangOptions::TrivialAutoVarInitKind trivialAutoVarInit = |
| 1789 | (D.isConstexpr() |
| 1790 | ? LangOptions::TrivialAutoVarInitKind::Uninitialized |
| 1791 | : (D.getAttr<UninitializedAttr>() |
| 1792 | ? LangOptions::TrivialAutoVarInitKind::Uninitialized |
| 1793 | : getContext().getLangOpts().getTrivialAutoVarInit())); |
| 1794 | |
JF Bastien | b347e75 | 2019-02-08 01:29:17 +0000 | [diff] [blame] | 1795 | auto initializeWhatIsTechnicallyUninitialized = [&](Address Loc) { |
JF Bastien | 14daa20 | 2018-12-18 05:12:21 +0000 | [diff] [blame] | 1796 | if (trivialAutoVarInit == |
| 1797 | LangOptions::TrivialAutoVarInitKind::Uninitialized) |
| 1798 | return; |
| 1799 | |
JF Bastien | b347e75 | 2019-02-08 01:29:17 +0000 | [diff] [blame] | 1800 | // Only initialize a __block's storage: we always initialize the header. |
JF Bastien | 0bae08a | 2019-02-15 17:26:29 +0000 | [diff] [blame] | 1801 | if (emission.IsEscapingByRef && !locIsByrefHeader) |
JF Bastien | b347e75 | 2019-02-08 01:29:17 +0000 | [diff] [blame] | 1802 | Loc = emitBlockByrefAddress(Loc, &D, /*follow=*/false); |
| 1803 | |
Vitaly Buka | e726766 | 2019-07-10 22:53:50 +0000 | [diff] [blame] | 1804 | return emitZeroOrPatternForAutoVarInit(type, D, Loc); |
JF Bastien | 14daa20 | 2018-12-18 05:12:21 +0000 | [diff] [blame] | 1805 | }; |
| 1806 | |
Vitaly Buka | e726766 | 2019-07-10 22:53:50 +0000 | [diff] [blame] | 1807 | if (isTrivialInitializer(Init)) |
| 1808 | return initializeWhatIsTechnicallyUninitialized(Loc); |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1809 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1810 | llvm::Constant *constant = nullptr; |
Richard Smith | 715f7a1 | 2019-06-11 17:50:32 +0000 | [diff] [blame] | 1811 | if (emission.IsConstantAggregate || |
| 1812 | D.mightBeUsableInConstantExpressions(getContext())) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1813 | assert(!capturedByInit && "constant init contains a capturing block?"); |
John McCall | de0fe07 | 2017-08-15 21:42:52 +0000 | [diff] [blame] | 1814 | constant = ConstantEmitter(*this).tryEmitAbstractForInitializer(D); |
JF Bastien | d39fbc7 | 2019-04-30 22:56:53 +0000 | [diff] [blame] | 1815 | if (constant && !constant->isZeroValue() && |
| 1816 | (trivialAutoVarInit != |
| 1817 | LangOptions::TrivialAutoVarInitKind::Uninitialized)) { |
Alexander Potapenko | 4f7bc0e | 2019-02-26 10:46:21 +0000 | [diff] [blame] | 1818 | IsPattern isPattern = |
| 1819 | (trivialAutoVarInit == LangOptions::TrivialAutoVarInitKind::Pattern) |
| 1820 | ? IsPattern::Yes |
| 1821 | : IsPattern::No; |
JF Bastien | d39fbc7 | 2019-04-30 22:56:53 +0000 | [diff] [blame] | 1822 | // C guarantees that brace-init with fewer initializers than members in |
| 1823 | // the aggregate will initialize the rest of the aggregate as-if it were |
| 1824 | // static initialization. In turn static initialization guarantees that |
| 1825 | // padding is initialized to zero bits. We could instead pattern-init if D |
| 1826 | // has any ImplicitValueInitExpr, but that seems to be unintuitive |
| 1827 | // behavior. |
| 1828 | constant = constWithPadding(CGM, IsPattern::No, |
Alexander Potapenko | 4f7bc0e | 2019-02-26 10:46:21 +0000 | [diff] [blame] | 1829 | replaceUndef(CGM, isPattern, constant)); |
| 1830 | } |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1831 | } |
| 1832 | |
| 1833 | if (!constant) { |
JF Bastien | b347e75 | 2019-02-08 01:29:17 +0000 | [diff] [blame] | 1834 | initializeWhatIsTechnicallyUninitialized(Loc); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1835 | LValue lv = MakeAddrLValue(Loc, type); |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 1836 | lv.setNonGC(true); |
David Blaikie | 66e4197 | 2015-01-14 07:38:27 +0000 | [diff] [blame] | 1837 | return EmitExprAsInit(Init, &D, lv, capturedByInit); |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 1838 | } |
John McCall | 91ca10f | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 1839 | |
Richard Smith | 2bcde3a | 2013-06-02 00:09:52 +0000 | [diff] [blame] | 1840 | if (!emission.IsConstantAggregate) { |
| 1841 | // For simple scalar/complex initialization, store the value directly. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1842 | LValue lv = MakeAddrLValue(Loc, type); |
Richard Smith | 2bcde3a | 2013-06-02 00:09:52 +0000 | [diff] [blame] | 1843 | lv.setNonGC(true); |
| 1844 | return EmitStoreThroughLValue(RValue::get(constant), lv, true); |
| 1845 | } |
| 1846 | |
Yaxun Liu | daceb1e | 2018-05-14 19:20:12 +0000 | [diff] [blame] | 1847 | llvm::Type *BP = CGM.Int8Ty->getPointerTo(Loc.getAddressSpace()); |
JF Bastien | 0bae08a | 2019-02-15 17:26:29 +0000 | [diff] [blame] | 1848 | emitStoresForConstant( |
| 1849 | CGM, D, (Loc.getType() == BP) ? Loc : Builder.CreateBitCast(Loc, BP), |
Vitaly Buka | 96ff25a | 2019-06-27 01:34:21 +0000 | [diff] [blame] | 1850 | type.isVolatileQualified(), Builder, constant); |
John McCall | 91ca10f | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
Richard Smith | e78fac5 | 2018-04-05 20:52:58 +0000 | [diff] [blame] | 1853 | /// Emit an expression as an initializer for an object (variable, field, etc.) |
| 1854 | /// at the given location. The expression is not necessarily the normal |
| 1855 | /// initializer for the object, and the address is not necessarily |
John McCall | 91ca10f | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 1856 | /// its normal location. |
| 1857 | /// |
| 1858 | /// \param init the initializing expression |
Richard Smith | e78fac5 | 2018-04-05 20:52:58 +0000 | [diff] [blame] | 1859 | /// \param D the object to act as if we're initializing |
John McCall | 91ca10f | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 1860 | /// \param loc the address to initialize; its type is a pointer |
Richard Smith | e78fac5 | 2018-04-05 20:52:58 +0000 | [diff] [blame] | 1861 | /// to the LLVM mapping of the object's type |
John McCall | 91ca10f | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 1862 | /// \param alignment the alignment of the address |
Richard Smith | e78fac5 | 2018-04-05 20:52:58 +0000 | [diff] [blame] | 1863 | /// \param capturedByInit true if \p D is a __block variable |
John McCall | 91ca10f | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 1864 | /// whose address is potentially changed by the initializer |
David Blaikie | 73ca569 | 2014-12-09 00:32:22 +0000 | [diff] [blame] | 1865 | void CodeGenFunction::EmitExprAsInit(const Expr *init, const ValueDecl *D, |
David Blaikie | 66e4197 | 2015-01-14 07:38:27 +0000 | [diff] [blame] | 1866 | LValue lvalue, bool capturedByInit) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1867 | QualType type = D->getType(); |
John McCall | 91ca10f | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 1868 | |
| 1869 | if (type->isReferenceType()) { |
Richard Smith | a1c9d4d | 2013-06-12 23:38:09 +0000 | [diff] [blame] | 1870 | RValue rvalue = EmitReferenceBindingToExpr(init); |
Eric Christopher | 31ab130 | 2011-08-23 22:38:00 +0000 | [diff] [blame] | 1871 | if (capturedByInit) |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 1872 | drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D)); |
David Blaikie | 66e4197 | 2015-01-14 07:38:27 +0000 | [diff] [blame] | 1873 | EmitStoreThroughLValue(rvalue, lvalue, true); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 1874 | return; |
| 1875 | } |
| 1876 | switch (getEvaluationKind(type)) { |
| 1877 | case TEK_Scalar: |
David Blaikie | 66e4197 | 2015-01-14 07:38:27 +0000 | [diff] [blame] | 1878 | EmitScalarInit(init, D, lvalue, capturedByInit); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 1879 | return; |
| 1880 | case TEK_Complex: { |
John McCall | 91ca10f | 2011-03-08 09:11:50 +0000 | [diff] [blame] | 1881 | ComplexPairTy complex = EmitComplexExpr(init); |
John McCall | 1553b19 | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 1882 | if (capturedByInit) |
| 1883 | drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D)); |
David Blaikie | 66e4197 | 2015-01-14 07:38:27 +0000 | [diff] [blame] | 1884 | EmitStoreOfComplex(complex, lvalue, /*init*/ true); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 1885 | return; |
| 1886 | } |
| 1887 | case TEK_Aggregate: |
John McCall | a8ec7eb | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 1888 | if (type->isAtomicType()) { |
| 1889 | EmitAtomicInit(const_cast<Expr*>(init), lvalue); |
| 1890 | } else { |
Richard Smith | e78fac5 | 2018-04-05 20:52:58 +0000 | [diff] [blame] | 1891 | AggValueSlot::Overlap_t Overlap = AggValueSlot::MayOverlap; |
| 1892 | if (isa<VarDecl>(D)) |
| 1893 | Overlap = AggValueSlot::DoesNotOverlap; |
| 1894 | else if (auto *FD = dyn_cast<FieldDecl>(D)) |
Richard Smith | 8cca3a5 | 2019-06-20 20:56:20 +0000 | [diff] [blame] | 1895 | Overlap = getOverlapForFieldInit(FD); |
John McCall | a8ec7eb | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 1896 | // TODO: how can we delay here if D is captured by its initializer? |
| 1897 | EmitAggExpr(init, AggValueSlot::forLValue(lvalue, |
Chad Rosier | 615ed1a | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 1898 | AggValueSlot::IsDestructed, |
John McCall | a5efa73 | 2011-08-25 23:04:34 +0000 | [diff] [blame] | 1899 | AggValueSlot::DoesNotNeedGCBarriers, |
Richard Smith | e78fac5 | 2018-04-05 20:52:58 +0000 | [diff] [blame] | 1900 | AggValueSlot::IsNotAliased, |
| 1901 | Overlap)); |
John McCall | a8ec7eb | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 1902 | } |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 1903 | return; |
Fariborz Jahanian | c614073 | 2010-03-12 21:40:43 +0000 | [diff] [blame] | 1904 | } |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 1905 | llvm_unreachable("bad evaluation kind"); |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1906 | } |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1907 | |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1908 | /// Enter a destroy cleanup for the given local variable. |
| 1909 | void CodeGenFunction::emitAutoVarTypeCleanup( |
| 1910 | const CodeGenFunction::AutoVarEmission &emission, |
| 1911 | QualType::DestructionKind dtorKind) { |
| 1912 | assert(dtorKind != QualType::DK_none); |
| 1913 | |
| 1914 | // Note that for __block variables, we want to destroy the |
| 1915 | // original stack object, not the possibly forwarded object. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1916 | Address addr = emission.getObjectAddress(*this); |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1917 | |
| 1918 | const VarDecl *var = emission.Variable; |
| 1919 | QualType type = var->getType(); |
| 1920 | |
| 1921 | CleanupKind cleanupKind = NormalAndEHCleanup; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1922 | CodeGenFunction::Destroyer *destroyer = nullptr; |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1923 | |
| 1924 | switch (dtorKind) { |
| 1925 | case QualType::DK_none: |
| 1926 | llvm_unreachable("no cleanup for trivially-destructible variable"); |
| 1927 | |
| 1928 | case QualType::DK_cxx_destructor: |
| 1929 | // If there's an NRVO flag on the emission, we need a different |
| 1930 | // cleanup. |
| 1931 | if (emission.NRVOFlag) { |
| 1932 | assert(!type->isArrayType()); |
| 1933 | CXXDestructorDecl *dtor = type->getAsCXXRecordDecl()->getDestructor(); |
Marco Antognini | 8855963 | 2019-07-22 09:39:13 +0000 | [diff] [blame] | 1934 | EHStack.pushCleanup<DestroyNRVOVariableCXX>(cleanupKind, addr, type, dtor, |
Akira Hatanaka | 673af7a | 2018-03-29 17:56:24 +0000 | [diff] [blame] | 1935 | emission.NRVOFlag); |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1936 | return; |
| 1937 | } |
| 1938 | break; |
| 1939 | |
| 1940 | case QualType::DK_objc_strong_lifetime: |
| 1941 | // Suppress cleanups for pseudo-strong variables. |
| 1942 | if (var->isARCPseudoStrong()) return; |
Eric Christopher | 31ab130 | 2011-08-23 22:38:00 +0000 | [diff] [blame] | 1943 | |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1944 | // Otherwise, consider whether to use an EH cleanup or not. |
| 1945 | cleanupKind = getARCCleanupKind(); |
| 1946 | |
| 1947 | // Use the imprecise destroyer by default. |
| 1948 | if (!var->hasAttr<ObjCPreciseLifetimeAttr>()) |
| 1949 | destroyer = CodeGenFunction::destroyARCStrongImprecise; |
| 1950 | break; |
| 1951 | |
| 1952 | case QualType::DK_objc_weak_lifetime: |
| 1953 | break; |
Akira Hatanaka | 7275da0 | 2018-02-28 07:15:55 +0000 | [diff] [blame] | 1954 | |
| 1955 | case QualType::DK_nontrivial_c_struct: |
| 1956 | destroyer = CodeGenFunction::destroyNonTrivialCStruct; |
Akira Hatanaka | 673af7a | 2018-03-29 17:56:24 +0000 | [diff] [blame] | 1957 | if (emission.NRVOFlag) { |
| 1958 | assert(!type->isArrayType()); |
| 1959 | EHStack.pushCleanup<DestroyNRVOVariableC>(cleanupKind, addr, |
| 1960 | emission.NRVOFlag, type); |
| 1961 | return; |
| 1962 | } |
Akira Hatanaka | 7275da0 | 2018-02-28 07:15:55 +0000 | [diff] [blame] | 1963 | break; |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1964 | } |
| 1965 | |
| 1966 | // If we haven't chosen a more specific destroyer, use the default. |
Peter Collingbourne | 1425b45 | 2012-01-26 03:33:36 +0000 | [diff] [blame] | 1967 | if (!destroyer) destroyer = getDestroyer(dtorKind); |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 1968 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 1969 | // Use an EH cleanup in array destructors iff the destructor itself |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 1970 | // is being pushed as an EH cleanup. |
| 1971 | bool useEHCleanup = (cleanupKind & EHCleanup); |
| 1972 | EHStack.pushCleanup<DestroyObject>(cleanupKind, addr, type, destroyer, |
| 1973 | useEHCleanup); |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1976 | void CodeGenFunction::EmitAutoVarCleanups(const AutoVarEmission &emission) { |
John McCall | 9e2e22f | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 1977 | assert(emission.Variable && "emission was not valid!"); |
| 1978 | |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1979 | // If this was emitted as a global constant, we're done. |
| 1980 | if (emission.wasEmittedAsGlobal()) return; |
| 1981 | |
John McCall | 8c38d35 | 2012-04-13 18:44:05 +0000 | [diff] [blame] | 1982 | // If we don't have an insertion point, we're done. Sema prevents |
| 1983 | // us from jumping into any of these scopes anyway. |
| 1984 | if (!HaveInsertPoint()) return; |
| 1985 | |
John McCall | 9e2e22f | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 1986 | const VarDecl &D = *emission.Variable; |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1987 | |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1988 | // Check the type for a cleanup. |
Richard Smith | 2b4fa53 | 2019-09-29 05:08:46 +0000 | [diff] [blame] | 1989 | if (QualType::DestructionKind dtorKind = D.needsDestruction(getContext())) |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1990 | emitAutoVarTypeCleanup(emission, dtorKind); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1991 | |
John McCall | 1bd2556 | 2011-06-24 23:21:27 +0000 | [diff] [blame] | 1992 | // In GC mode, honor objc_precise_lifetime. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1993 | if (getLangOpts().getGC() != LangOptions::NonGC && |
John McCall | 1bd2556 | 2011-06-24 23:21:27 +0000 | [diff] [blame] | 1994 | D.hasAttr<ObjCPreciseLifetimeAttr>()) { |
| 1995 | EHStack.pushCleanup<ExtendGCLifetime>(NormalCleanup, &D); |
| 1996 | } |
| 1997 | |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 1998 | // Handle the cleanup attribute. |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1999 | if (const CleanupAttr *CA = D.getAttr<CleanupAttr>()) { |
Anders Carlsson | cadb9a6 | 2009-02-07 23:51:38 +0000 | [diff] [blame] | 2000 | const FunctionDecl *FD = CA->getFunctionDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2001 | |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 2002 | llvm::Constant *F = CGM.GetAddrOfFunction(FD); |
Anders Carlsson | cadb9a6 | 2009-02-07 23:51:38 +0000 | [diff] [blame] | 2003 | assert(F && "Could not find function!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2004 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2005 | const CGFunctionInfo &Info = CGM.getTypes().arrangeFunctionDeclaration(FD); |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 2006 | EHStack.pushCleanup<CallCleanupFunction>(NormalAndEHCleanup, F, &Info, &D); |
Anders Carlsson | cadb9a6 | 2009-02-07 23:51:38 +0000 | [diff] [blame] | 2007 | } |
Mike Stump | 626aecc | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 2008 | |
John McCall | c533cb7 | 2011-02-22 06:44:22 +0000 | [diff] [blame] | 2009 | // If this is a block variable, call _Block_object_destroy |
Akira Hatanaka | cb6a933 | 2018-07-26 16:51:21 +0000 | [diff] [blame] | 2010 | // (on the unforwarded address). Don't enter this cleanup if we're in pure-GC |
| 2011 | // mode. |
Akira Hatanaka | 8e57b07 | 2018-10-01 21:51:28 +0000 | [diff] [blame] | 2012 | if (emission.IsEscapingByRef && |
| 2013 | CGM.getLangOpts().getGC() != LangOptions::GCOnly) { |
Akira Hatanaka | cb6a933 | 2018-07-26 16:51:21 +0000 | [diff] [blame] | 2014 | BlockFieldFlags Flags = BLOCK_FIELD_IS_BYREF; |
| 2015 | if (emission.Variable->getType().isObjCGCWeak()) |
| 2016 | Flags |= BLOCK_FIELD_IS_WEAK; |
| 2017 | enterByrefCleanup(NormalAndEHCleanup, emission.Addr, Flags, |
Akira Hatanaka | 9978da3 | 2018-08-10 15:09:24 +0000 | [diff] [blame] | 2018 | /*LoadBlockVarAddr*/ false, |
| 2019 | cxxDestructorCanThrow(emission.Variable->getType())); |
Akira Hatanaka | cb6a933 | 2018-07-26 16:51:21 +0000 | [diff] [blame] | 2020 | } |
Chris Lattner | 03df122 | 2007-06-02 04:53:11 +0000 | [diff] [blame] | 2021 | } |
Chris Lattner | 53621a5 | 2007-06-13 20:44:40 +0000 | [diff] [blame] | 2022 | |
Peter Collingbourne | 1425b45 | 2012-01-26 03:33:36 +0000 | [diff] [blame] | 2023 | CodeGenFunction::Destroyer * |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 2024 | CodeGenFunction::getDestroyer(QualType::DestructionKind kind) { |
| 2025 | switch (kind) { |
| 2026 | case QualType::DK_none: llvm_unreachable("no destroyer for trivial dtor"); |
John McCall | c2f0001 | 2011-07-09 09:09:00 +0000 | [diff] [blame] | 2027 | case QualType::DK_cxx_destructor: |
Peter Collingbourne | 1425b45 | 2012-01-26 03:33:36 +0000 | [diff] [blame] | 2028 | return destroyCXXObject; |
John McCall | c2f0001 | 2011-07-09 09:09:00 +0000 | [diff] [blame] | 2029 | case QualType::DK_objc_strong_lifetime: |
Peter Collingbourne | 1425b45 | 2012-01-26 03:33:36 +0000 | [diff] [blame] | 2030 | return destroyARCStrongPrecise; |
John McCall | c2f0001 | 2011-07-09 09:09:00 +0000 | [diff] [blame] | 2031 | case QualType::DK_objc_weak_lifetime: |
Peter Collingbourne | 1425b45 | 2012-01-26 03:33:36 +0000 | [diff] [blame] | 2032 | return destroyARCWeak; |
Akira Hatanaka | 7275da0 | 2018-02-28 07:15:55 +0000 | [diff] [blame] | 2033 | case QualType::DK_nontrivial_c_struct: |
| 2034 | return destroyNonTrivialCStruct; |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 2035 | } |
Matt Beaumont-Gay | 934bbf5 | 2012-01-27 00:46:27 +0000 | [diff] [blame] | 2036 | llvm_unreachable("Unknown DestructionKind"); |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 2037 | } |
| 2038 | |
John McCall | 12cc42a | 2013-02-01 05:11:40 +0000 | [diff] [blame] | 2039 | /// pushEHDestroy - Push the standard destructor for the given type as |
| 2040 | /// an EH-only cleanup. |
| 2041 | void CodeGenFunction::pushEHDestroy(QualType::DestructionKind dtorKind, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2042 | Address addr, QualType type) { |
John McCall | 12cc42a | 2013-02-01 05:11:40 +0000 | [diff] [blame] | 2043 | assert(dtorKind && "cannot push destructor for trivial type"); |
| 2044 | assert(needsEHCleanup(dtorKind)); |
| 2045 | |
| 2046 | pushDestroy(EHCleanup, addr, type, getDestroyer(dtorKind), true); |
| 2047 | } |
| 2048 | |
| 2049 | /// pushDestroy - Push the standard destructor for the given type as |
| 2050 | /// at least a normal cleanup. |
John McCall | 4bd0fb1 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 2051 | void CodeGenFunction::pushDestroy(QualType::DestructionKind dtorKind, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2052 | Address addr, QualType type) { |
John McCall | 4bd0fb1 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 2053 | assert(dtorKind && "cannot push destructor for trivial type"); |
| 2054 | |
| 2055 | CleanupKind cleanupKind = getCleanupKind(dtorKind); |
| 2056 | pushDestroy(cleanupKind, addr, type, getDestroyer(dtorKind), |
| 2057 | cleanupKind & EHCleanup); |
| 2058 | } |
| 2059 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2060 | void CodeGenFunction::pushDestroy(CleanupKind cleanupKind, Address addr, |
Peter Collingbourne | 1425b45 | 2012-01-26 03:33:36 +0000 | [diff] [blame] | 2061 | QualType type, Destroyer *destroyer, |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 2062 | bool useEHCleanupForArray) { |
John McCall | 4bd0fb1 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 2063 | pushFullExprCleanup<DestroyObject>(cleanupKind, addr, type, |
| 2064 | destroyer, useEHCleanupForArray); |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2067 | void CodeGenFunction::pushStackRestore(CleanupKind Kind, Address SPMem) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2068 | EHStack.pushCleanup<CallStackRestore>(Kind, SPMem); |
| 2069 | } |
| 2070 | |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 2071 | void CodeGenFunction::pushLifetimeExtendedDestroy( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2072 | CleanupKind cleanupKind, Address addr, QualType type, |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 2073 | Destroyer *destroyer, bool useEHCleanupForArray) { |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 2074 | // Push an EH-only cleanup for the object now. |
| 2075 | // FIXME: When popping normal cleanups, we need to keep this EH cleanup |
| 2076 | // around in case a temporary's destructor throws an exception. |
| 2077 | if (cleanupKind & EHCleanup) |
| 2078 | EHStack.pushCleanup<DestroyObject>( |
| 2079 | static_cast<CleanupKind>(cleanupKind & ~NormalCleanup), addr, type, |
| 2080 | destroyer, useEHCleanupForArray); |
| 2081 | |
| 2082 | // Remember that we need to push a full cleanup for the object at the |
| 2083 | // end of the full-expression. |
| 2084 | pushCleanupAfterFullExpr<DestroyObject>( |
| 2085 | cleanupKind, addr, type, destroyer, useEHCleanupForArray); |
| 2086 | } |
| 2087 | |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 2088 | /// emitDestroy - Immediately perform the destruction of the given |
| 2089 | /// object. |
| 2090 | /// |
| 2091 | /// \param addr - the address of the object; a type* |
| 2092 | /// \param type - the type of the object; if an array type, all |
| 2093 | /// objects are destroyed in reverse order |
| 2094 | /// \param destroyer - the function to call to destroy individual |
| 2095 | /// elements |
| 2096 | /// \param useEHCleanupForArray - whether an EH cleanup should be |
| 2097 | /// used when destroying array elements, in case one of the |
| 2098 | /// destructions throws an exception |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2099 | void CodeGenFunction::emitDestroy(Address addr, QualType type, |
Peter Collingbourne | 1425b45 | 2012-01-26 03:33:36 +0000 | [diff] [blame] | 2100 | Destroyer *destroyer, |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 2101 | bool useEHCleanupForArray) { |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 2102 | const ArrayType *arrayType = getContext().getAsArrayType(type); |
| 2103 | if (!arrayType) |
| 2104 | return destroyer(*this, addr, type); |
| 2105 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2106 | llvm::Value *length = emitArrayLength(arrayType, type, addr); |
| 2107 | |
| 2108 | CharUnits elementAlign = |
| 2109 | addr.getAlignment() |
| 2110 | .alignmentOfArrayElement(getContext().getTypeSizeInChars(type)); |
John McCall | 97eab0a | 2011-07-13 08:09:46 +0000 | [diff] [blame] | 2111 | |
| 2112 | // Normally we have to check whether the array is zero-length. |
| 2113 | bool checkZeroLength = true; |
| 2114 | |
| 2115 | // But if the array length is constant, we can suppress that. |
| 2116 | if (llvm::ConstantInt *constLength = dyn_cast<llvm::ConstantInt>(length)) { |
| 2117 | // ...and if it's constant zero, we can just skip the entire thing. |
| 2118 | if (constLength->isZero()) return; |
| 2119 | checkZeroLength = false; |
| 2120 | } |
| 2121 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2122 | llvm::Value *begin = addr.getPointer(); |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 2123 | llvm::Value *end = Builder.CreateInBoundsGEP(begin, length); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2124 | emitArrayDestroy(begin, end, type, elementAlign, destroyer, |
John McCall | 97eab0a | 2011-07-13 08:09:46 +0000 | [diff] [blame] | 2125 | checkZeroLength, useEHCleanupForArray); |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 2126 | } |
| 2127 | |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 2128 | /// emitArrayDestroy - Destroys all the elements of the given array, |
| 2129 | /// beginning from last to first. The array cannot be zero-length. |
| 2130 | /// |
| 2131 | /// \param begin - a type* denoting the first element of the array |
| 2132 | /// \param end - a type* denoting one past the end of the array |
NAKAMURA Takumi | ff7a925 | 2015-09-08 09:42:41 +0000 | [diff] [blame] | 2133 | /// \param elementType - the element type of the array |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 2134 | /// \param destroyer - the function to call to destroy elements |
| 2135 | /// \param useEHCleanup - whether to push an EH cleanup to destroy |
| 2136 | /// the remaining elements in case the destruction of a single |
| 2137 | /// element throws |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 2138 | void CodeGenFunction::emitArrayDestroy(llvm::Value *begin, |
| 2139 | llvm::Value *end, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2140 | QualType elementType, |
| 2141 | CharUnits elementAlign, |
Peter Collingbourne | 1425b45 | 2012-01-26 03:33:36 +0000 | [diff] [blame] | 2142 | Destroyer *destroyer, |
John McCall | 97eab0a | 2011-07-13 08:09:46 +0000 | [diff] [blame] | 2143 | bool checkZeroLength, |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 2144 | bool useEHCleanup) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2145 | assert(!elementType->isArrayType()); |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 2146 | |
| 2147 | // The basic structure here is a do-while loop, because we don't |
| 2148 | // need to check for the zero-element case. |
| 2149 | llvm::BasicBlock *bodyBB = createBasicBlock("arraydestroy.body"); |
| 2150 | llvm::BasicBlock *doneBB = createBasicBlock("arraydestroy.done"); |
| 2151 | |
John McCall | 97eab0a | 2011-07-13 08:09:46 +0000 | [diff] [blame] | 2152 | if (checkZeroLength) { |
| 2153 | llvm::Value *isEmpty = Builder.CreateICmpEQ(begin, end, |
| 2154 | "arraydestroy.isempty"); |
| 2155 | Builder.CreateCondBr(isEmpty, doneBB, bodyBB); |
| 2156 | } |
| 2157 | |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 2158 | // Enter the loop body, making that address the current address. |
| 2159 | llvm::BasicBlock *entryBB = Builder.GetInsertBlock(); |
| 2160 | EmitBlock(bodyBB); |
| 2161 | llvm::PHINode *elementPast = |
| 2162 | Builder.CreatePHI(begin->getType(), 2, "arraydestroy.elementPast"); |
| 2163 | elementPast->addIncoming(end, entryBB); |
| 2164 | |
| 2165 | // Shift the address back by one element. |
| 2166 | llvm::Value *negativeOne = llvm::ConstantInt::get(SizeTy, -1, true); |
| 2167 | llvm::Value *element = Builder.CreateInBoundsGEP(elementPast, negativeOne, |
| 2168 | "arraydestroy.element"); |
| 2169 | |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 2170 | if (useEHCleanup) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2171 | pushRegularPartialArrayCleanup(begin, element, elementType, elementAlign, |
| 2172 | destroyer); |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 2173 | |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 2174 | // Perform the actual destruction there. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2175 | destroyer(*this, Address(element, elementAlign), elementType); |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 2176 | |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 2177 | if (useEHCleanup) |
| 2178 | PopCleanupBlock(); |
| 2179 | |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 2180 | // Check whether we've reached the end. |
| 2181 | llvm::Value *done = Builder.CreateICmpEQ(element, begin, "arraydestroy.done"); |
| 2182 | Builder.CreateCondBr(done, doneBB, bodyBB); |
| 2183 | elementPast->addIncoming(element, Builder.GetInsertBlock()); |
| 2184 | |
| 2185 | // Done. |
| 2186 | EmitBlock(doneBB); |
| 2187 | } |
| 2188 | |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 2189 | /// Perform partial array destruction as if in an EH cleanup. Unlike |
| 2190 | /// emitArrayDestroy, the element type here may still be an array type. |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 2191 | static void emitPartialArrayDestroy(CodeGenFunction &CGF, |
| 2192 | llvm::Value *begin, llvm::Value *end, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2193 | QualType type, CharUnits elementAlign, |
Peter Collingbourne | 1425b45 | 2012-01-26 03:33:36 +0000 | [diff] [blame] | 2194 | CodeGenFunction::Destroyer *destroyer) { |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 2195 | // If the element type is itself an array, drill down. |
John McCall | 97eab0a | 2011-07-13 08:09:46 +0000 | [diff] [blame] | 2196 | unsigned arrayDepth = 0; |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 2197 | while (const ArrayType *arrayType = CGF.getContext().getAsArrayType(type)) { |
| 2198 | // VLAs don't require a GEP index to walk into. |
| 2199 | if (!isa<VariableArrayType>(arrayType)) |
John McCall | 97eab0a | 2011-07-13 08:09:46 +0000 | [diff] [blame] | 2200 | arrayDepth++; |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 2201 | type = arrayType->getElementType(); |
| 2202 | } |
John McCall | 97eab0a | 2011-07-13 08:09:46 +0000 | [diff] [blame] | 2203 | |
| 2204 | if (arrayDepth) { |
John McCall | f0f0b7a | 2015-09-14 18:57:08 +0000 | [diff] [blame] | 2205 | llvm::Value *zero = llvm::ConstantInt::get(CGF.SizeTy, 0); |
John McCall | 97eab0a | 2011-07-13 08:09:46 +0000 | [diff] [blame] | 2206 | |
John McCall | f0f0b7a | 2015-09-14 18:57:08 +0000 | [diff] [blame] | 2207 | SmallVector<llvm::Value*,4> gepIndices(arrayDepth+1, zero); |
Jay Foad | 040dd82 | 2011-07-22 08:16:57 +0000 | [diff] [blame] | 2208 | begin = CGF.Builder.CreateInBoundsGEP(begin, gepIndices, "pad.arraybegin"); |
| 2209 | end = CGF.Builder.CreateInBoundsGEP(end, gepIndices, "pad.arrayend"); |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 2210 | } |
| 2211 | |
John McCall | 97eab0a | 2011-07-13 08:09:46 +0000 | [diff] [blame] | 2212 | // Destroy the array. We don't ever need an EH cleanup because we |
| 2213 | // assume that we're in an EH cleanup ourselves, so a throwing |
| 2214 | // destructor causes an immediate terminate. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2215 | CGF.emitArrayDestroy(begin, end, type, elementAlign, destroyer, |
John McCall | 97eab0a | 2011-07-13 08:09:46 +0000 | [diff] [blame] | 2216 | /*checkZeroLength*/ true, /*useEHCleanup*/ false); |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 2217 | } |
| 2218 | |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 2219 | namespace { |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 2220 | /// RegularPartialArrayDestroy - a cleanup which performs a partial |
| 2221 | /// array destroy where the end pointer is regularly determined and |
| 2222 | /// does not need to be loaded from a local. |
David Blaikie | 7e70d68 | 2015-08-18 22:40:54 +0000 | [diff] [blame] | 2223 | class RegularPartialArrayDestroy final : public EHScopeStack::Cleanup { |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 2224 | llvm::Value *ArrayBegin; |
| 2225 | llvm::Value *ArrayEnd; |
| 2226 | QualType ElementType; |
Peter Collingbourne | 1425b45 | 2012-01-26 03:33:36 +0000 | [diff] [blame] | 2227 | CodeGenFunction::Destroyer *Destroyer; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2228 | CharUnits ElementAlign; |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 2229 | public: |
| 2230 | RegularPartialArrayDestroy(llvm::Value *arrayBegin, llvm::Value *arrayEnd, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2231 | QualType elementType, CharUnits elementAlign, |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 2232 | CodeGenFunction::Destroyer *destroyer) |
| 2233 | : ArrayBegin(arrayBegin), ArrayEnd(arrayEnd), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2234 | ElementType(elementType), Destroyer(destroyer), |
| 2235 | ElementAlign(elementAlign) {} |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 2236 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2237 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 2238 | emitPartialArrayDestroy(CGF, ArrayBegin, ArrayEnd, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2239 | ElementType, ElementAlign, Destroyer); |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 2240 | } |
| 2241 | }; |
| 2242 | |
| 2243 | /// IrregularPartialArrayDestroy - a cleanup which performs a |
| 2244 | /// partial array destroy where the end pointer is irregularly |
| 2245 | /// determined and must be loaded from a local. |
David Blaikie | 7e70d68 | 2015-08-18 22:40:54 +0000 | [diff] [blame] | 2246 | class IrregularPartialArrayDestroy final : public EHScopeStack::Cleanup { |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 2247 | llvm::Value *ArrayBegin; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2248 | Address ArrayEndPointer; |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 2249 | QualType ElementType; |
Peter Collingbourne | 1425b45 | 2012-01-26 03:33:36 +0000 | [diff] [blame] | 2250 | CodeGenFunction::Destroyer *Destroyer; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2251 | CharUnits ElementAlign; |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 2252 | public: |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 2253 | IrregularPartialArrayDestroy(llvm::Value *arrayBegin, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2254 | Address arrayEndPointer, |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 2255 | QualType elementType, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2256 | CharUnits elementAlign, |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 2257 | CodeGenFunction::Destroyer *destroyer) |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 2258 | : ArrayBegin(arrayBegin), ArrayEndPointer(arrayEndPointer), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2259 | ElementType(elementType), Destroyer(destroyer), |
| 2260 | ElementAlign(elementAlign) {} |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 2261 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2262 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 2263 | llvm::Value *arrayEnd = CGF.Builder.CreateLoad(ArrayEndPointer); |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 2264 | emitPartialArrayDestroy(CGF, ArrayBegin, arrayEnd, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2265 | ElementType, ElementAlign, Destroyer); |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 2266 | } |
| 2267 | }; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 2268 | } // end anonymous namespace |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 2269 | |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 2270 | /// pushIrregularPartialArrayCleanup - Push an EH cleanup to destroy |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 2271 | /// already-constructed elements of the given array. The cleanup |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 2272 | /// may be popped with DeactivateCleanupBlock or PopCleanupBlock. |
Eric Christopher | 31ab130 | 2011-08-23 22:38:00 +0000 | [diff] [blame] | 2273 | /// |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 2274 | /// \param elementType - the immediate element type of the array; |
| 2275 | /// possibly still an array type |
John McCall | 4bd0fb1 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 2276 | void CodeGenFunction::pushIrregularPartialArrayCleanup(llvm::Value *arrayBegin, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2277 | Address arrayEndPointer, |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 2278 | QualType elementType, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2279 | CharUnits elementAlign, |
Peter Collingbourne | 1425b45 | 2012-01-26 03:33:36 +0000 | [diff] [blame] | 2280 | Destroyer *destroyer) { |
John McCall | 4bd0fb1 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 2281 | pushFullExprCleanup<IrregularPartialArrayDestroy>(EHCleanup, |
| 2282 | arrayBegin, arrayEndPointer, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2283 | elementType, elementAlign, |
| 2284 | destroyer); |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 2285 | } |
| 2286 | |
| 2287 | /// pushRegularPartialArrayCleanup - Push an EH cleanup to destroy |
| 2288 | /// already-constructed elements of the given array. The cleanup |
| 2289 | /// may be popped with DeactivateCleanupBlock or PopCleanupBlock. |
Eric Christopher | 31ab130 | 2011-08-23 22:38:00 +0000 | [diff] [blame] | 2290 | /// |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 2291 | /// \param elementType - the immediate element type of the array; |
| 2292 | /// possibly still an array type |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 2293 | void CodeGenFunction::pushRegularPartialArrayCleanup(llvm::Value *arrayBegin, |
| 2294 | llvm::Value *arrayEnd, |
| 2295 | QualType elementType, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2296 | CharUnits elementAlign, |
Peter Collingbourne | 1425b45 | 2012-01-26 03:33:36 +0000 | [diff] [blame] | 2297 | Destroyer *destroyer) { |
John McCall | 4bd0fb1 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 2298 | pushFullExprCleanup<RegularPartialArrayDestroy>(EHCleanup, |
John McCall | 178360e | 2011-07-11 08:38:19 +0000 | [diff] [blame] | 2299 | arrayBegin, arrayEnd, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2300 | elementType, elementAlign, |
| 2301 | destroyer); |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 2302 | } |
| 2303 | |
Nadav Rotem | 1da3094 | 2013-03-23 06:43:35 +0000 | [diff] [blame] | 2304 | /// Lazily declare the @llvm.lifetime.start intrinsic. |
James Y Knight | 76f7874 | 2019-02-05 19:17:50 +0000 | [diff] [blame] | 2305 | llvm::Function *CodeGenModule::getLLVMLifetimeStartFn() { |
Matt Arsenault | d972949 | 2017-04-10 20:18:45 +0000 | [diff] [blame] | 2306 | if (LifetimeStartFn) |
| 2307 | return LifetimeStartFn; |
Nadav Rotem | 1da3094 | 2013-03-23 06:43:35 +0000 | [diff] [blame] | 2308 | LifetimeStartFn = llvm::Intrinsic::getDeclaration(&getModule(), |
Yaxun Liu | 7f7f323 | 2017-04-17 20:03:11 +0000 | [diff] [blame] | 2309 | llvm::Intrinsic::lifetime_start, AllocaInt8PtrTy); |
Nadav Rotem | 1da3094 | 2013-03-23 06:43:35 +0000 | [diff] [blame] | 2310 | return LifetimeStartFn; |
| 2311 | } |
| 2312 | |
| 2313 | /// Lazily declare the @llvm.lifetime.end intrinsic. |
James Y Knight | 76f7874 | 2019-02-05 19:17:50 +0000 | [diff] [blame] | 2314 | llvm::Function *CodeGenModule::getLLVMLifetimeEndFn() { |
Matt Arsenault | d972949 | 2017-04-10 20:18:45 +0000 | [diff] [blame] | 2315 | if (LifetimeEndFn) |
| 2316 | return LifetimeEndFn; |
Nadav Rotem | 1da3094 | 2013-03-23 06:43:35 +0000 | [diff] [blame] | 2317 | LifetimeEndFn = llvm::Intrinsic::getDeclaration(&getModule(), |
Yaxun Liu | 7f7f323 | 2017-04-17 20:03:11 +0000 | [diff] [blame] | 2318 | llvm::Intrinsic::lifetime_end, AllocaInt8PtrTy); |
Nadav Rotem | 1da3094 | 2013-03-23 06:43:35 +0000 | [diff] [blame] | 2319 | return LifetimeEndFn; |
| 2320 | } |
| 2321 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2322 | namespace { |
| 2323 | /// A cleanup to perform a release of an object at the end of a |
| 2324 | /// function. This is used to balance out the incoming +1 of a |
| 2325 | /// ns_consumed argument when we can't reasonably do that just by |
| 2326 | /// not doing the initial retain for a __block argument. |
David Blaikie | 7e70d68 | 2015-08-18 22:40:54 +0000 | [diff] [blame] | 2327 | struct ConsumeARCParameter final : EHScopeStack::Cleanup { |
John McCall | cdda29c | 2013-03-13 03:10:54 +0000 | [diff] [blame] | 2328 | ConsumeARCParameter(llvm::Value *param, |
| 2329 | ARCPreciseLifetime_t precise) |
| 2330 | : Param(param), Precise(precise) {} |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2331 | |
| 2332 | llvm::Value *Param; |
John McCall | cdda29c | 2013-03-13 03:10:54 +0000 | [diff] [blame] | 2333 | ARCPreciseLifetime_t Precise; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2334 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2335 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
John McCall | cdda29c | 2013-03-13 03:10:54 +0000 | [diff] [blame] | 2336 | CGF.EmitARCRelease(Param, Precise); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2337 | } |
| 2338 | }; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 2339 | } // end anonymous namespace |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2340 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2341 | /// Emit an alloca (or GlobalValue depending on target) |
Chris Lattner | b781dc79 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 2342 | /// for the specified parameter and set up LocalDeclMap. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2343 | void CodeGenFunction::EmitParmDecl(const VarDecl &D, ParamValue Arg, |
| 2344 | unsigned ArgNo) { |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2345 | // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl? |
Sanjiv Gupta | d795924 | 2008-10-31 09:52:39 +0000 | [diff] [blame] | 2346 | assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) && |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2347 | "Invalid argument to EmitParmDecl"); |
John McCall | 147d021 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 2348 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2349 | Arg.getAnyValue()->setName(D.getName()); |
John McCall | 147d021 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 2350 | |
Adrian Prantl | 51936dd | 2013-03-14 17:53:33 +0000 | [diff] [blame] | 2351 | QualType Ty = D.getType(); |
| 2352 | |
John McCall | 147d021 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 2353 | // Use better IR generation for certain implicit parameters. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2354 | if (auto IPD = dyn_cast<ImplicitParamDecl>(&D)) { |
John McCall | 147d021 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 2355 | // The only implicit argument a block has is its literal. |
Saleem Abdulrasool | c1b4638 | 2018-02-21 21:47:51 +0000 | [diff] [blame] | 2356 | // This may be passed as an inalloca'ed value on Windows x86. |
John McCall | 147d021 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 2357 | if (BlockInfo) { |
Saleem Abdulrasool | c1b4638 | 2018-02-21 21:47:51 +0000 | [diff] [blame] | 2358 | llvm::Value *V = Arg.isIndirect() |
| 2359 | ? Builder.CreateLoad(Arg.getIndirectAddress()) |
| 2360 | : Arg.getDirectValue(); |
| 2361 | setBlockContextParameter(IPD, ArgNo, V); |
John McCall | 147d021 | 2011-02-22 22:38:33 +0000 | [diff] [blame] | 2362 | return; |
| 2363 | } |
| 2364 | } |
| 2365 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2366 | Address DeclPtr = Address::invalid(); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2367 | bool DoStore = false; |
| 2368 | bool IsScalar = hasScalarEvaluationKind(Ty); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2369 | // 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] | 2370 | if (Arg.isIndirect()) { |
| 2371 | DeclPtr = Arg.getIndirectAddress(); |
Reid Kleckner | 5e8edba | 2014-04-02 00:16:53 +0000 | [diff] [blame] | 2372 | // 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] | 2373 | unsigned AS = DeclPtr.getType()->getAddressSpace(); |
Reid Kleckner | 5e8edba | 2014-04-02 00:16:53 +0000 | [diff] [blame] | 2374 | llvm::Type *IRTy = ConvertTypeForMem(Ty)->getPointerTo(AS); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2375 | if (DeclPtr.getType() != IRTy) |
| 2376 | DeclPtr = Builder.CreateBitCast(DeclPtr, IRTy, D.getName()); |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 2377 | // Indirect argument is in alloca address space, which may be different |
| 2378 | // from the default address space. |
| 2379 | auto AllocaAS = CGM.getASTAllocaAddressSpace(); |
| 2380 | auto *V = DeclPtr.getPointer(); |
| 2381 | auto SrcLangAS = getLangOpts().OpenCL ? LangAS::opencl_private : AllocaAS; |
| 2382 | auto DestLangAS = |
| 2383 | getLangOpts().OpenCL ? LangAS::opencl_private : LangAS::Default; |
| 2384 | if (SrcLangAS != DestLangAS) { |
| 2385 | assert(getContext().getTargetAddressSpace(SrcLangAS) == |
| 2386 | CGM.getDataLayout().getAllocaAddrSpace()); |
| 2387 | auto DestAS = getContext().getTargetAddressSpace(DestLangAS); |
| 2388 | auto *T = V->getType()->getPointerElementType()->getPointerTo(DestAS); |
| 2389 | DeclPtr = Address(getTargetHooks().performAddrSpaceCast( |
| 2390 | *this, V, SrcLangAS, DestLangAS, T, true), |
| 2391 | DeclPtr.getAlignment()); |
| 2392 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2393 | |
Reid Kleckner | 23f4c4b | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 2394 | // Push a destructor cleanup for this parameter if the ABI requires it. |
Reid Kleckner | 1981944 | 2014-07-25 21:39:46 +0000 | [diff] [blame] | 2395 | // Don't push a cleanup in a thunk for a method that will also emit a |
| 2396 | // cleanup. |
Akira Hatanaka | 8528297 | 2018-05-15 21:00:30 +0000 | [diff] [blame] | 2397 | if (hasAggregateEvaluationKind(Ty) && !CurFuncIsThunk && |
Simon Pilgrim | 1cd399c | 2019-10-03 11:22:48 +0000 | [diff] [blame] | 2398 | Ty->castAs<RecordType>()->getDecl()->isParamDestroyedInCallee()) { |
Richard Smith | 2b4fa53 | 2019-09-29 05:08:46 +0000 | [diff] [blame] | 2399 | if (QualType::DestructionKind DtorKind = |
| 2400 | D.needsDestruction(getContext())) { |
Akira Hatanaka | 7275da0 | 2018-02-28 07:15:55 +0000 | [diff] [blame] | 2401 | assert((DtorKind == QualType::DK_cxx_destructor || |
| 2402 | DtorKind == QualType::DK_nontrivial_c_struct) && |
| 2403 | "unexpected destructor type"); |
| 2404 | pushDestroy(DtorKind, DeclPtr, Ty); |
Akira Hatanaka | ccda3d2 | 2018-04-27 06:57:00 +0000 | [diff] [blame] | 2405 | CalleeDestructedParamCleanups[cast<ParmVarDecl>(&D)] = |
| 2406 | EHStack.stable_begin(); |
Akira Hatanaka | 7275da0 | 2018-02-28 07:15:55 +0000 | [diff] [blame] | 2407 | } |
Reid Kleckner | 23f4c4b | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 2408 | } |
Chris Lattner | 53621a5 | 2007-06-13 20:44:40 +0000 | [diff] [blame] | 2409 | } else { |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 2410 | // Check if the parameter address is controlled by OpenMP runtime. |
| 2411 | Address OpenMPLocalAddr = |
| 2412 | getLangOpts().OpenMP |
| 2413 | ? CGM.getOpenMPRuntime().getAddressOfLocalVariable(*this, &D) |
| 2414 | : Address::invalid(); |
| 2415 | if (getLangOpts().OpenMP && OpenMPLocalAddr.isValid()) { |
| 2416 | DeclPtr = OpenMPLocalAddr; |
| 2417 | } else { |
| 2418 | // Otherwise, create a temporary to hold the value. |
| 2419 | DeclPtr = CreateMemTemp(Ty, getContext().getDeclAlign(&D), |
| 2420 | D.getName() + ".addr"); |
| 2421 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2422 | DoStore = true; |
| 2423 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2424 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2425 | llvm::Value *ArgVal = (DoStore ? Arg.getDirectValue() : nullptr); |
| 2426 | |
| 2427 | LValue lv = MakeAddrLValue(DeclPtr, Ty); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2428 | if (IsScalar) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2429 | Qualifiers qs = Ty.getQualifiers(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2430 | if (Qualifiers::ObjCLifetime lt = qs.getObjCLifetime()) { |
| 2431 | // We honor __attribute__((ns_consumed)) for types with lifetime. |
| 2432 | // For __strong, it's handled by just skipping the initial retain; |
| 2433 | // otherwise we have to balance out the initial +1 with an extra |
| 2434 | // cleanup to do the release at the end of the function. |
| 2435 | bool isConsumed = D.hasAttr<NSConsumedAttr>(); |
| 2436 | |
Erik Pilkington | 1e36882 | 2019-01-04 18:33:06 +0000 | [diff] [blame] | 2437 | // If a parameter is pseudo-strong then we can omit the implicit retain. |
John McCall | d463132 | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 2438 | if (D.isARCPseudoStrong()) { |
Erik Pilkington | 1e36882 | 2019-01-04 18:33:06 +0000 | [diff] [blame] | 2439 | assert(lt == Qualifiers::OCL_Strong && |
| 2440 | "pseudo-strong variable isn't strong?"); |
| 2441 | assert(qs.hasConst() && "pseudo-strong variable should be const!"); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2442 | lt = Qualifiers::OCL_ExplicitNone; |
| 2443 | } |
| 2444 | |
Saleem Abdulrasool | da6784e | 2017-06-27 18:37:51 +0000 | [diff] [blame] | 2445 | // Load objects passed indirectly. |
| 2446 | if (Arg.isIndirect() && !ArgVal) |
| 2447 | ArgVal = Builder.CreateLoad(DeclPtr); |
| 2448 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2449 | if (lt == Qualifiers::OCL_Strong) { |
Fariborz Jahanian | 134cec6 | 2013-02-21 00:40:10 +0000 | [diff] [blame] | 2450 | if (!isConsumed) { |
| 2451 | if (CGM.getCodeGenOpts().OptimizationLevel == 0) { |
| 2452 | // use objc_storeStrong(&dest, value) for retaining the |
| 2453 | // object. But first, store a null into 'dest' because |
| 2454 | // objc_storeStrong attempts to release its old value. |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 2455 | llvm::Value *Null = CGM.EmitNullConstant(D.getType()); |
Fariborz Jahanian | 134cec6 | 2013-02-21 00:40:10 +0000 | [diff] [blame] | 2456 | EmitStoreOfScalar(Null, lv, /* isInitialization */ true); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2457 | EmitARCStoreStrongCall(lv.getAddress(), ArgVal, true); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2458 | DoStore = false; |
Fariborz Jahanian | 134cec6 | 2013-02-21 00:40:10 +0000 | [diff] [blame] | 2459 | } |
| 2460 | else |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2461 | // Don't use objc_retainBlock for block pointers, because we |
| 2462 | // don't want to Block_copy something just because we got it |
| 2463 | // as a parameter. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2464 | ArgVal = EmitARCRetainNonBlock(ArgVal); |
Fariborz Jahanian | 134cec6 | 2013-02-21 00:40:10 +0000 | [diff] [blame] | 2465 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2466 | } else { |
| 2467 | // Push the cleanup for a consumed parameter. |
John McCall | cdda29c | 2013-03-13 03:10:54 +0000 | [diff] [blame] | 2468 | if (isConsumed) { |
| 2469 | ARCPreciseLifetime_t precise = (D.hasAttr<ObjCPreciseLifetimeAttr>() |
| 2470 | ? ARCPreciseLifetime : ARCImpreciseLifetime); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2471 | EHStack.pushCleanup<ConsumeARCParameter>(getARCCleanupKind(), ArgVal, |
John McCall | cdda29c | 2013-03-13 03:10:54 +0000 | [diff] [blame] | 2472 | precise); |
| 2473 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2474 | |
| 2475 | if (lt == Qualifiers::OCL_Weak) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2476 | EmitARCInitWeak(DeclPtr, ArgVal); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2477 | 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] | 2478 | } |
| 2479 | } |
| 2480 | |
| 2481 | // Enter the cleanup scope. |
| 2482 | EmitAutoVarWithLifetime(*this, D, DeclPtr, lt); |
| 2483 | } |
Chris Lattner | 53621a5 | 2007-06-13 20:44:40 +0000 | [diff] [blame] | 2484 | } |
| 2485 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2486 | // Store the initial value into the alloca. |
| 2487 | if (DoStore) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2488 | EmitStoreOfScalar(ArgVal, lv, /* isInitialization */ true); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2489 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2490 | setAddrOfLocalVar(&D, DeclPtr); |
Sanjiv Gupta | 18de624 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 2491 | |
Vedant Kumar | 04818d8 | 2019-07-29 22:49:55 +0000 | [diff] [blame] | 2492 | // Emit debug info for param declarations in non-thunk functions. |
Alexey Samsonov | 74a3868 | 2012-05-04 07:39:27 +0000 | [diff] [blame] | 2493 | if (CGDebugInfo *DI = getDebugInfo()) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 2494 | if (CGM.getCodeGenOpts().getDebugInfo() >= |
Vedant Kumar | 04818d8 | 2019-07-29 22:49:55 +0000 | [diff] [blame] | 2495 | codegenoptions::LimitedDebugInfo && |
| 2496 | !CurFuncIsThunk) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2497 | DI->EmitDeclareOfArgVariable(&D, DeclPtr.getPointer(), ArgNo, Builder); |
Alexey Samsonov | 74a3868 | 2012-05-04 07:39:27 +0000 | [diff] [blame] | 2498 | } |
| 2499 | } |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2500 | |
| 2501 | if (D.hasAttr<AnnotateAttr>()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2502 | EmitVarAnnotations(&D, DeclPtr.getPointer()); |
Vedant Kumar | 42c17ec | 2017-03-14 01:56:34 +0000 | [diff] [blame] | 2503 | |
| 2504 | // We can only check return value nullability if all arguments to the |
| 2505 | // function satisfy their nullability preconditions. This makes it necessary |
| 2506 | // to emit null checks for args in the function body itself. |
| 2507 | if (requiresReturnValueNullabilityCheck()) { |
| 2508 | auto Nullability = Ty->getNullability(getContext()); |
| 2509 | if (Nullability && *Nullability == NullabilityKind::NonNull) { |
| 2510 | SanitizerScope SanScope(this); |
| 2511 | RetValNullabilityPrecondition = |
| 2512 | Builder.CreateAnd(RetValNullabilityPrecondition, |
| 2513 | Builder.CreateIsNotNull(Arg.getAnyValue())); |
| 2514 | } |
| 2515 | } |
Chris Lattner | 53621a5 | 2007-06-13 20:44:40 +0000 | [diff] [blame] | 2516 | } |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 2517 | |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 2518 | void CodeGenModule::EmitOMPDeclareReduction(const OMPDeclareReductionDecl *D, |
| 2519 | CodeGenFunction *CGF) { |
| 2520 | if (!LangOpts.OpenMP || (!LangOpts.EmitAllDecls && !D->isUsed())) |
| 2521 | return; |
| 2522 | getOpenMPRuntime().emitUserDefinedReduction(CGF, D); |
| 2523 | } |
Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 2524 | |
Michael Kruse | 251e148 | 2019-02-01 20:25:04 +0000 | [diff] [blame] | 2525 | void CodeGenModule::EmitOMPDeclareMapper(const OMPDeclareMapperDecl *D, |
Michael Kruse | d47b943 | 2019-08-05 18:43:21 +0000 | [diff] [blame] | 2526 | CodeGenFunction *CGF) { |
| 2527 | if (!LangOpts.OpenMP || LangOpts.OpenMPSimd || |
| 2528 | (!LangOpts.EmitAllDecls && !D->isUsed())) |
Michael Kruse | 251e148 | 2019-02-01 20:25:04 +0000 | [diff] [blame] | 2529 | return; |
Michael Kruse | d47b943 | 2019-08-05 18:43:21 +0000 | [diff] [blame] | 2530 | getOpenMPRuntime().emitUserDefinedMapper(D, CGF); |
Michael Kruse | 251e148 | 2019-02-01 20:25:04 +0000 | [diff] [blame] | 2531 | } |
| 2532 | |
Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 2533 | void CodeGenModule::EmitOMPRequiresDecl(const OMPRequiresDecl *D) { |
Alexey Bataev | 982a35e | 2019-03-19 17:09:52 +0000 | [diff] [blame] | 2534 | getOpenMPRuntime().checkArchForUnifiedAddressing(D); |
Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 2535 | } |