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