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