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