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