1193326Sed//===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===// 2193326Sed// 3193326Sed// The LLVM Compiler Infrastructure 4193326Sed// 5193326Sed// This file is distributed under the University of Illinois Open Source 6193326Sed// License. See LICENSE.TXT for details. 7193326Sed// 8193326Sed//===----------------------------------------------------------------------===// 9193326Sed// 10193326Sed// This contains code to emit Decl nodes as LLVM code. 11193326Sed// 12193326Sed//===----------------------------------------------------------------------===// 13193326Sed 14249423Sdim#include "CodeGenFunction.h" 15193326Sed#include "CGDebugInfo.h" 16249423Sdim#include "CGOpenCLRuntime.h" 17193326Sed#include "CodeGenModule.h" 18193326Sed#include "clang/AST/ASTContext.h" 19201361Srdivacky#include "clang/AST/CharUnits.h" 20193326Sed#include "clang/AST/Decl.h" 21193326Sed#include "clang/AST/DeclObjC.h" 22193326Sed#include "clang/Basic/SourceManager.h" 23193326Sed#include "clang/Basic/TargetInfo.h" 24263508Sdim#include "clang/CodeGen/CGFunctionInfo.h" 25210299Sed#include "clang/Frontend/CodeGenOptions.h" 26249423Sdim#include "llvm/IR/DataLayout.h" 27249423Sdim#include "llvm/IR/GlobalVariable.h" 28249423Sdim#include "llvm/IR/Intrinsics.h" 29249423Sdim#include "llvm/IR/Type.h" 30193326Sedusing namespace clang; 31193326Sedusing namespace CodeGen; 32193326Sed 33193326Sed 34193326Sedvoid CodeGenFunction::EmitDecl(const Decl &D) { 35193326Sed switch (D.getKind()) { 36207619Srdivacky case Decl::TranslationUnit: 37207619Srdivacky case Decl::Namespace: 38207619Srdivacky case Decl::UnresolvedUsingTypename: 39207619Srdivacky case Decl::ClassTemplateSpecialization: 40207619Srdivacky case Decl::ClassTemplatePartialSpecialization: 41263508Sdim case Decl::VarTemplateSpecialization: 42263508Sdim case Decl::VarTemplatePartialSpecialization: 43207619Srdivacky case Decl::TemplateTypeParm: 44207619Srdivacky case Decl::UnresolvedUsingValue: 45210299Sed case Decl::NonTypeTemplateParm: 46207619Srdivacky case Decl::CXXMethod: 47207619Srdivacky case Decl::CXXConstructor: 48207619Srdivacky case Decl::CXXDestructor: 49207619Srdivacky case Decl::CXXConversion: 50207619Srdivacky case Decl::Field: 51251662Sdim case Decl::MSProperty: 52218893Sdim case Decl::IndirectField: 53207619Srdivacky case Decl::ObjCIvar: 54226633Sdim case Decl::ObjCAtDefsField: 55193326Sed case Decl::ParmVar: 56207619Srdivacky case Decl::ImplicitParam: 57207619Srdivacky case Decl::ClassTemplate: 58263508Sdim case Decl::VarTemplate: 59207619Srdivacky case Decl::FunctionTemplate: 60223017Sdim case Decl::TypeAliasTemplate: 61207619Srdivacky case Decl::TemplateTemplateParm: 62207619Srdivacky case Decl::ObjCMethod: 63207619Srdivacky case Decl::ObjCCategory: 64207619Srdivacky case Decl::ObjCProtocol: 65207619Srdivacky case Decl::ObjCInterface: 66207619Srdivacky case Decl::ObjCCategoryImpl: 67207619Srdivacky case Decl::ObjCImplementation: 68207619Srdivacky case Decl::ObjCProperty: 69207619Srdivacky case Decl::ObjCCompatibleAlias: 70210299Sed case Decl::AccessSpec: 71207619Srdivacky case Decl::LinkageSpec: 72207619Srdivacky case Decl::ObjCPropertyImpl: 73207619Srdivacky case Decl::FileScopeAsm: 74207619Srdivacky case Decl::Friend: 75207619Srdivacky case Decl::FriendTemplate: 76207619Srdivacky case Decl::Block: 77251662Sdim case Decl::Captured: 78226633Sdim case Decl::ClassScopeFunctionSpecialization: 79263508Sdim case Decl::UsingShadow: 80226633Sdim llvm_unreachable("Declaration should not be in declstmts!"); 81193326Sed case Decl::Function: // void X(); 82193326Sed case Decl::Record: // struct/union/class X; 83193326Sed case Decl::Enum: // enum X; 84198092Srdivacky case Decl::EnumConstant: // enum ? { X = ? } 85193326Sed case Decl::CXXRecord: // struct/union/class X; [C++] 86200583Srdivacky case Decl::StaticAssert: // static_assert(X, ""); [C++0x] 87218893Sdim case Decl::Label: // __label__ x; 88234353Sdim case Decl::Import: 89249423Sdim case Decl::OMPThreadPrivate: 90249423Sdim case Decl::Empty: 91193326Sed // None of these decls require codegen support. 92193326Sed return; 93198092Srdivacky 94263508Sdim case Decl::NamespaceAlias: 95263508Sdim if (CGDebugInfo *DI = getDebugInfo()) 96263508Sdim DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(D)); 97263508Sdim return; 98263508Sdim case Decl::Using: // using X; [C++] 99263508Sdim if (CGDebugInfo *DI = getDebugInfo()) 100263508Sdim DI->EmitUsingDecl(cast<UsingDecl>(D)); 101263508Sdim return; 102251662Sdim case Decl::UsingDirective: // using namespace X; [C++] 103251662Sdim if (CGDebugInfo *DI = getDebugInfo()) 104251662Sdim DI->EmitUsingDirective(cast<UsingDirectiveDecl>(D)); 105251662Sdim return; 106193326Sed case Decl::Var: { 107193326Sed const VarDecl &VD = cast<VarDecl>(D); 108218893Sdim assert(VD.isLocalVarDecl() && 109193326Sed "Should not see file-scope variables inside a function!"); 110218893Sdim return EmitVarDecl(VD); 111193326Sed } 112198092Srdivacky 113221345Sdim case Decl::Typedef: // typedef int X; 114221345Sdim case Decl::TypeAlias: { // using X = int; [C++0x] 115221345Sdim const TypedefNameDecl &TD = cast<TypedefNameDecl>(D); 116193326Sed QualType Ty = TD.getUnderlyingType(); 117198092Srdivacky 118193326Sed if (Ty->isVariablyModifiedType()) 119224145Sdim EmitVariablyModifiedType(Ty); 120193326Sed } 121193326Sed } 122193326Sed} 123193326Sed 124218893Sdim/// EmitVarDecl - This method handles emission of any variable declaration 125193326Sed/// inside a function, including static vars etc. 126218893Sdimvoid CodeGenFunction::EmitVarDecl(const VarDecl &D) { 127263508Sdim if (D.isStaticLocal()) { 128226633Sdim llvm::GlobalValue::LinkageTypes Linkage = 129203955Srdivacky llvm::GlobalValue::InternalLinkage; 130203955Srdivacky 131263508Sdim // If the variable is externally visible, it must have weak linkage so it 132263508Sdim // can be uniqued. 133263508Sdim if (D.isExternallyVisible()) { 134263508Sdim Linkage = llvm::GlobalValue::LinkOnceODRLinkage; 135226633Sdim 136263508Sdim // FIXME: We need to force the emission/use of a guard variable for 137263508Sdim // some variables even if we can constant-evaluate them because 138263508Sdim // we can't guarantee every translation unit will constant-evaluate them. 139263508Sdim } 140263508Sdim 141218893Sdim return EmitStaticVarDecl(D, Linkage); 142203955Srdivacky } 143263508Sdim 144263508Sdim if (D.hasExternalStorage()) 145193326Sed // Don't emit it now, allow it to be emitted lazily on its first use. 146193326Sed return; 147263508Sdim 148263508Sdim if (D.getStorageClass() == SC_OpenCLWorkGroupLocal) 149226633Sdim return CGM.getOpenCLRuntime().EmitWorkGroupLocalVarDecl(*this, D); 150193326Sed 151263508Sdim assert(D.hasLocalStorage()); 152263508Sdim return EmitAutoVarDecl(D); 153193326Sed} 154193326Sed 155200583Srdivackystatic std::string GetStaticDeclName(CodeGenFunction &CGF, const VarDecl &D, 156200583Srdivacky const char *Separator) { 157200583Srdivacky CodeGenModule &CGM = CGF.CGM; 158243830Sdim if (CGF.getLangOpts().CPlusPlus) { 159226633Sdim StringRef Name = CGM.getMangledName(&D); 160210299Sed return Name.str(); 161205408Srdivacky } 162226633Sdim 163200583Srdivacky std::string ContextName; 164218893Sdim if (!CGF.CurFuncDecl) { 165218893Sdim // Better be in a block declared in global scope. 166218893Sdim const NamedDecl *ND = cast<NamedDecl>(&D); 167218893Sdim const DeclContext *DC = ND->getDeclContext(); 168218893Sdim if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) { 169218893Sdim MangleBuffer Name; 170218893Sdim CGM.getBlockMangledName(GlobalDecl(), Name, BD); 171218893Sdim ContextName = Name.getString(); 172218893Sdim } 173218893Sdim else 174226633Sdim llvm_unreachable("Unknown context for block static var decl"); 175218893Sdim } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CGF.CurFuncDecl)) { 176226633Sdim StringRef Name = CGM.getMangledName(FD); 177210299Sed ContextName = Name.str(); 178205408Srdivacky } else if (isa<ObjCMethodDecl>(CGF.CurFuncDecl)) 179200583Srdivacky ContextName = CGF.CurFn->getName(); 180200583Srdivacky else 181226633Sdim llvm_unreachable("Unknown context for static var decl"); 182226633Sdim 183200583Srdivacky return ContextName + Separator + D.getNameAsString(); 184200583Srdivacky} 185200583Srdivacky 186193326Sedllvm::GlobalVariable * 187218893SdimCodeGenFunction::CreateStaticVarDecl(const VarDecl &D, 188218893Sdim const char *Separator, 189218893Sdim llvm::GlobalValue::LinkageTypes Linkage) { 190193326Sed QualType Ty = D.getType(); 191193326Sed assert(Ty->isConstantSizeType() && "VLAs can't be static"); 192193326Sed 193234353Sdim // Use the label if the variable is renamed with the asm-label extension. 194234353Sdim std::string Name; 195234353Sdim if (D.hasAttr<AsmLabelAttr>()) 196234353Sdim Name = CGM.getMangledName(&D); 197234353Sdim else 198234353Sdim Name = GetStaticDeclName(*this, D, Separator); 199198092Srdivacky 200226633Sdim llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty); 201243830Sdim unsigned AddrSpace = 202243830Sdim CGM.GetGlobalVarAddressSpace(&D, CGM.getContext().getTargetAddressSpace(Ty)); 203198092Srdivacky llvm::GlobalVariable *GV = 204198092Srdivacky new llvm::GlobalVariable(CGM.getModule(), LTy, 205198092Srdivacky Ty.isConstant(getContext()), Linkage, 206198092Srdivacky CGM.EmitNullConstant(D.getType()), Name, 0, 207239462Sdim llvm::GlobalVariable::NotThreadLocal, 208243830Sdim AddrSpace); 209203955Srdivacky GV->setAlignment(getContext().getDeclAlign(&D).getQuantity()); 210263508Sdim CGM.setGlobalVisibility(GV, &D); 211239462Sdim 212251662Sdim if (D.getTLSKind()) 213239462Sdim CGM.setTLSMode(GV, D); 214239462Sdim 215198092Srdivacky return GV; 216193326Sed} 217193326Sed 218234353Sdim/// hasNontrivialDestruction - Determine whether a type's destruction is 219234353Sdim/// non-trivial. If so, and the variable uses static initialization, we must 220234353Sdim/// register its destructor to run on exit. 221234353Sdimstatic bool hasNontrivialDestruction(QualType T) { 222234353Sdim CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); 223234353Sdim return RD && !RD->hasTrivialDestructor(); 224234353Sdim} 225234353Sdim 226218893Sdim/// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the 227200583Srdivacky/// global variable that has already been created for it. If the initializer 228200583Srdivacky/// has a different type than GV does, this may free GV and return a different 229200583Srdivacky/// one. Otherwise it just returns GV. 230200583Srdivackyllvm::GlobalVariable * 231218893SdimCodeGenFunction::AddInitializerToStaticVarDecl(const VarDecl &D, 232218893Sdim llvm::GlobalVariable *GV) { 233234353Sdim llvm::Constant *Init = CGM.EmitConstantInit(D, this); 234212904Sdim 235200583Srdivacky // If constant emission failed, then this should be a C++ static 236200583Srdivacky // initializer. 237200583Srdivacky if (!Init) { 238243830Sdim if (!getLangOpts().CPlusPlus) 239200583Srdivacky CGM.ErrorUnsupported(D.getInit(), "constant l-value expression"); 240218893Sdim else if (Builder.GetInsertBlock()) { 241226633Sdim // Since we have a static initializer, this global variable can't 242203955Srdivacky // be constant. 243203955Srdivacky GV->setConstant(false); 244218893Sdim 245234353Sdim EmitCXXGuardedInit(D, GV, /*PerformInit*/true); 246203955Srdivacky } 247200583Srdivacky return GV; 248200583Srdivacky } 249212904Sdim 250200583Srdivacky // The initializer may differ in type from the global. Rewrite 251200583Srdivacky // the global to match the initializer. (We have to do this 252200583Srdivacky // because some types, like unions, can't be completely represented 253200583Srdivacky // in the LLVM type system.) 254212904Sdim if (GV->getType()->getElementType() != Init->getType()) { 255200583Srdivacky llvm::GlobalVariable *OldGV = GV; 256226633Sdim 257200583Srdivacky GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), 258200583Srdivacky OldGV->isConstant(), 259200583Srdivacky OldGV->getLinkage(), Init, "", 260218893Sdim /*InsertBefore*/ OldGV, 261239462Sdim OldGV->getThreadLocalMode(), 262221345Sdim CGM.getContext().getTargetAddressSpace(D.getType())); 263218893Sdim GV->setVisibility(OldGV->getVisibility()); 264226633Sdim 265200583Srdivacky // Steal the name of the old global 266200583Srdivacky GV->takeName(OldGV); 267226633Sdim 268200583Srdivacky // Replace all uses of the old global with the new global 269200583Srdivacky llvm::Constant *NewPtrForOldDecl = 270200583Srdivacky llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); 271200583Srdivacky OldGV->replaceAllUsesWith(NewPtrForOldDecl); 272226633Sdim 273200583Srdivacky // Erase the old global, since it is no longer used. 274200583Srdivacky OldGV->eraseFromParent(); 275200583Srdivacky } 276226633Sdim 277234353Sdim GV->setConstant(CGM.isTypeConstant(D.getType(), true)); 278200583Srdivacky GV->setInitializer(Init); 279234353Sdim 280234353Sdim if (hasNontrivialDestruction(D.getType())) { 281234353Sdim // We have a constant initializer, but a nontrivial destructor. We still 282234353Sdim // need to perform a guarded "initialization" in order to register the 283234353Sdim // destructor. 284234353Sdim EmitCXXGuardedInit(D, GV, /*PerformInit*/false); 285234353Sdim } 286234353Sdim 287200583Srdivacky return GV; 288200583Srdivacky} 289200583Srdivacky 290218893Sdimvoid CodeGenFunction::EmitStaticVarDecl(const VarDecl &D, 291203955Srdivacky llvm::GlobalValue::LinkageTypes Linkage) { 292193326Sed llvm::Value *&DMEntry = LocalDeclMap[&D]; 293193326Sed assert(DMEntry == 0 && "Decl already exists in localdeclmap!"); 294198092Srdivacky 295234353Sdim // Check to see if we already have a global variable for this 296234353Sdim // declaration. This can happen when double-emitting function 297234353Sdim // bodies, e.g. with complete and base constructors. 298234353Sdim llvm::Constant *addr = 299234353Sdim CGM.getStaticLocalDeclAddress(&D); 300193326Sed 301234353Sdim llvm::GlobalVariable *var; 302234353Sdim if (addr) { 303234353Sdim var = cast<llvm::GlobalVariable>(addr->stripPointerCasts()); 304234353Sdim } else { 305234353Sdim addr = var = CreateStaticVarDecl(D, ".", Linkage); 306234353Sdim } 307234353Sdim 308193326Sed // Store into LocalDeclMap before generating initializer to handle 309193326Sed // circular references. 310234353Sdim DMEntry = addr; 311234353Sdim CGM.setStaticLocalDeclAddress(&D, addr); 312193326Sed 313207632Srdivacky // We can't have a VLA here, but we can have a pointer to a VLA, 314207632Srdivacky // even though that doesn't really make any sense. 315193326Sed // Make sure to evaluate VLA bounds now so that we have them for later. 316193326Sed if (D.getType()->isVariablyModifiedType()) 317224145Sdim EmitVariablyModifiedType(D.getType()); 318226633Sdim 319234353Sdim // Save the type in case adding the initializer forces a type change. 320234353Sdim llvm::Type *expectedType = addr->getType(); 321193326Sed 322200583Srdivacky // If this value has an initializer, emit it. 323200583Srdivacky if (D.getInit()) 324234353Sdim var = AddInitializerToStaticVarDecl(D, var); 325193326Sed 326234353Sdim var->setAlignment(getContext().getDeclAlign(&D).getQuantity()); 327205219Srdivacky 328226633Sdim if (D.hasAttr<AnnotateAttr>()) 329234353Sdim CGM.AddGlobalAnnotations(&D, var); 330193326Sed 331195341Sed if (const SectionAttr *SA = D.getAttr<SectionAttr>()) 332234353Sdim var->setSection(SA->getName()); 333198092Srdivacky 334195341Sed if (D.hasAttr<UsedAttr>()) 335234353Sdim CGM.AddUsedGlobal(var); 336193326Sed 337193326Sed // We may have to cast the constant because of the initializer 338193326Sed // mismatch above. 339193326Sed // 340193326Sed // FIXME: It is really dangerous to store this in the map; if anyone 341193326Sed // RAUW's the GV uses of this constant will be invalid. 342234353Sdim llvm::Constant *castedAddr = llvm::ConstantExpr::getBitCast(var, expectedType); 343234353Sdim DMEntry = castedAddr; 344234353Sdim CGM.setStaticLocalDeclAddress(&D, castedAddr); 345193326Sed 346193326Sed // Emit global variable debug descriptor for static vars. 347193326Sed CGDebugInfo *DI = getDebugInfo(); 348239462Sdim if (DI && 349243830Sdim CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo) { 350193326Sed DI->setLocation(D.getLocation()); 351234353Sdim DI->EmitGlobalVariable(var, &D); 352193326Sed } 353193326Sed} 354198092Srdivacky 355210299Sednamespace { 356224145Sdim struct DestroyObject : EHScopeStack::Cleanup { 357224145Sdim DestroyObject(llvm::Value *addr, QualType type, 358224145Sdim CodeGenFunction::Destroyer *destroyer, 359224145Sdim bool useEHCleanupForArray) 360234353Sdim : addr(addr), type(type), destroyer(destroyer), 361224145Sdim useEHCleanupForArray(useEHCleanupForArray) {} 362210299Sed 363224145Sdim llvm::Value *addr; 364224145Sdim QualType type; 365234353Sdim CodeGenFunction::Destroyer *destroyer; 366224145Sdim bool useEHCleanupForArray; 367210299Sed 368224145Sdim void Emit(CodeGenFunction &CGF, Flags flags) { 369224145Sdim // Don't use an EH cleanup recursively from an EH cleanup. 370224145Sdim bool useEHCleanupForArray = 371224145Sdim flags.isForNormalCleanup() && this->useEHCleanupForArray; 372224145Sdim 373224145Sdim CGF.emitDestroy(addr, type, destroyer, useEHCleanupForArray); 374210299Sed } 375210299Sed }; 376210299Sed 377224145Sdim struct DestroyNRVOVariable : EHScopeStack::Cleanup { 378224145Sdim DestroyNRVOVariable(llvm::Value *addr, 379224145Sdim const CXXDestructorDecl *Dtor, 380224145Sdim llvm::Value *NRVOFlag) 381224145Sdim : Dtor(Dtor), NRVOFlag(NRVOFlag), Loc(addr) {} 382210299Sed 383210299Sed const CXXDestructorDecl *Dtor; 384210299Sed llvm::Value *NRVOFlag; 385210299Sed llvm::Value *Loc; 386210299Sed 387224145Sdim void Emit(CodeGenFunction &CGF, Flags flags) { 388210299Sed // Along the exceptions path we always execute the dtor. 389224145Sdim bool NRVO = flags.isForNormalCleanup() && NRVOFlag; 390210299Sed 391210299Sed llvm::BasicBlock *SkipDtorBB = 0; 392210299Sed if (NRVO) { 393210299Sed // If we exited via NRVO, we skip the destructor call. 394210299Sed llvm::BasicBlock *RunDtorBB = CGF.createBasicBlock("nrvo.unused"); 395210299Sed SkipDtorBB = CGF.createBasicBlock("nrvo.skipdtor"); 396210299Sed llvm::Value *DidNRVO = CGF.Builder.CreateLoad(NRVOFlag, "nrvo.val"); 397210299Sed CGF.Builder.CreateCondBr(DidNRVO, SkipDtorBB, RunDtorBB); 398210299Sed CGF.EmitBlock(RunDtorBB); 399210299Sed } 400226633Sdim 401210299Sed CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, 402249423Sdim /*ForVirtualBase=*/false, 403249423Sdim /*Delegating=*/false, 404249423Sdim Loc); 405210299Sed 406210299Sed if (NRVO) CGF.EmitBlock(SkipDtorBB); 407210299Sed } 408210299Sed }; 409210299Sed 410212904Sdim struct CallStackRestore : EHScopeStack::Cleanup { 411212904Sdim llvm::Value *Stack; 412212904Sdim CallStackRestore(llvm::Value *Stack) : Stack(Stack) {} 413224145Sdim void Emit(CodeGenFunction &CGF, Flags flags) { 414226633Sdim llvm::Value *V = CGF.Builder.CreateLoad(Stack); 415212904Sdim llvm::Value *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stackrestore); 416212904Sdim CGF.Builder.CreateCall(F, V); 417212904Sdim } 418212904Sdim }; 419212904Sdim 420224145Sdim struct ExtendGCLifetime : EHScopeStack::Cleanup { 421224145Sdim const VarDecl &Var; 422224145Sdim ExtendGCLifetime(const VarDecl *var) : Var(*var) {} 423224145Sdim 424224145Sdim void Emit(CodeGenFunction &CGF, Flags flags) { 425224145Sdim // Compute the address of the local variable, in case it's a 426224145Sdim // byref or something. 427234353Sdim DeclRefExpr DRE(const_cast<VarDecl*>(&Var), false, 428234353Sdim Var.getType(), VK_LValue, SourceLocation()); 429263508Sdim llvm::Value *value = CGF.EmitLoadOfScalar(CGF.EmitDeclRefLValue(&DRE), 430263508Sdim SourceLocation()); 431224145Sdim CGF.EmitExtendGCLifetime(value); 432224145Sdim } 433224145Sdim }; 434224145Sdim 435212904Sdim struct CallCleanupFunction : EHScopeStack::Cleanup { 436212904Sdim llvm::Constant *CleanupFn; 437212904Sdim const CGFunctionInfo &FnInfo; 438212904Sdim const VarDecl &Var; 439226633Sdim 440212904Sdim CallCleanupFunction(llvm::Constant *CleanupFn, const CGFunctionInfo *Info, 441219077Sdim const VarDecl *Var) 442219077Sdim : CleanupFn(CleanupFn), FnInfo(*Info), Var(*Var) {} 443212904Sdim 444224145Sdim void Emit(CodeGenFunction &CGF, Flags flags) { 445234353Sdim DeclRefExpr DRE(const_cast<VarDecl*>(&Var), false, 446234353Sdim Var.getType(), VK_LValue, SourceLocation()); 447219077Sdim // Compute the address of the local variable, in case it's a byref 448219077Sdim // or something. 449219077Sdim llvm::Value *Addr = CGF.EmitDeclRefLValue(&DRE).getAddress(); 450219077Sdim 451212904Sdim // In some cases, the type of the function argument will be different from 452212904Sdim // the type of the pointer. An example of this is 453212904Sdim // void f(void* arg); 454212904Sdim // __attribute__((cleanup(f))) void *g; 455212904Sdim // 456212904Sdim // To fix this we insert a bitcast here. 457212904Sdim QualType ArgTy = FnInfo.arg_begin()->type; 458212904Sdim llvm::Value *Arg = 459212904Sdim CGF.Builder.CreateBitCast(Addr, CGF.ConvertType(ArgTy)); 460212904Sdim 461212904Sdim CallArgList Args; 462221345Sdim Args.add(RValue::get(Arg), 463221345Sdim CGF.getContext().getPointerType(Var.getType())); 464212904Sdim CGF.EmitCall(FnInfo, CleanupFn, ReturnValueSlot(), Args); 465212904Sdim } 466212904Sdim }; 467249423Sdim 468249423Sdim /// A cleanup to call @llvm.lifetime.end. 469249423Sdim class CallLifetimeEnd : public EHScopeStack::Cleanup { 470249423Sdim llvm::Value *Addr; 471249423Sdim llvm::Value *Size; 472249423Sdim public: 473249423Sdim CallLifetimeEnd(llvm::Value *addr, llvm::Value *size) 474249423Sdim : Addr(addr), Size(size) {} 475249423Sdim 476249423Sdim void Emit(CodeGenFunction &CGF, Flags flags) { 477249423Sdim llvm::Value *castAddr = CGF.Builder.CreateBitCast(Addr, CGF.Int8PtrTy); 478249423Sdim CGF.Builder.CreateCall2(CGF.CGM.getLLVMLifetimeEndFn(), 479249423Sdim Size, castAddr) 480249423Sdim ->setDoesNotThrow(); 481249423Sdim } 482249423Sdim }; 483212904Sdim} 484212904Sdim 485224145Sdim/// EmitAutoVarWithLifetime - Does the setup required for an automatic 486224145Sdim/// variable with lifetime. 487224145Sdimstatic void EmitAutoVarWithLifetime(CodeGenFunction &CGF, const VarDecl &var, 488224145Sdim llvm::Value *addr, 489224145Sdim Qualifiers::ObjCLifetime lifetime) { 490224145Sdim switch (lifetime) { 491224145Sdim case Qualifiers::OCL_None: 492224145Sdim llvm_unreachable("present but none"); 493218893Sdim 494224145Sdim case Qualifiers::OCL_ExplicitNone: 495224145Sdim // nothing to do 496224145Sdim break; 497224145Sdim 498224145Sdim case Qualifiers::OCL_Strong: { 499234353Sdim CodeGenFunction::Destroyer *destroyer = 500224145Sdim (var.hasAttr<ObjCPreciseLifetimeAttr>() 501224145Sdim ? CodeGenFunction::destroyARCStrongPrecise 502224145Sdim : CodeGenFunction::destroyARCStrongImprecise); 503224145Sdim 504224145Sdim CleanupKind cleanupKind = CGF.getARCCleanupKind(); 505224145Sdim CGF.pushDestroy(cleanupKind, addr, var.getType(), destroyer, 506224145Sdim cleanupKind & EHCleanup); 507224145Sdim break; 508224145Sdim } 509224145Sdim case Qualifiers::OCL_Autoreleasing: 510224145Sdim // nothing to do 511224145Sdim break; 512226633Sdim 513224145Sdim case Qualifiers::OCL_Weak: 514224145Sdim // __weak objects always get EH cleanups; otherwise, exceptions 515224145Sdim // could cause really nasty crashes instead of mere leaks. 516224145Sdim CGF.pushDestroy(NormalAndEHCleanup, addr, var.getType(), 517224145Sdim CodeGenFunction::destroyARCWeak, 518224145Sdim /*useEHCleanup*/ true); 519224145Sdim break; 520224145Sdim } 521224145Sdim} 522224145Sdim 523224145Sdimstatic bool isAccessedBy(const VarDecl &var, const Stmt *s) { 524224145Sdim if (const Expr *e = dyn_cast<Expr>(s)) { 525224145Sdim // Skip the most common kinds of expressions that make 526224145Sdim // hierarchy-walking expensive. 527224145Sdim s = e = e->IgnoreParenCasts(); 528224145Sdim 529224145Sdim if (const DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) 530224145Sdim return (ref->getDecl() == &var); 531239462Sdim if (const BlockExpr *be = dyn_cast<BlockExpr>(e)) { 532239462Sdim const BlockDecl *block = be->getBlockDecl(); 533239462Sdim for (BlockDecl::capture_const_iterator i = block->capture_begin(), 534239462Sdim e = block->capture_end(); i != e; ++i) { 535239462Sdim if (i->getVariable() == &var) 536239462Sdim return true; 537239462Sdim } 538239462Sdim } 539224145Sdim } 540224145Sdim 541224145Sdim for (Stmt::const_child_range children = s->children(); children; ++children) 542224145Sdim // children might be null; as in missing decl or conditional of an if-stmt. 543224145Sdim if ((*children) && isAccessedBy(var, *children)) 544224145Sdim return true; 545224145Sdim 546224145Sdim return false; 547224145Sdim} 548224145Sdim 549224145Sdimstatic bool isAccessedBy(const ValueDecl *decl, const Expr *e) { 550224145Sdim if (!decl) return false; 551224145Sdim if (!isa<VarDecl>(decl)) return false; 552224145Sdim const VarDecl *var = cast<VarDecl>(decl); 553224145Sdim return isAccessedBy(*var, e); 554224145Sdim} 555224145Sdim 556224145Sdimstatic void drillIntoBlockVariable(CodeGenFunction &CGF, 557224145Sdim LValue &lvalue, 558224145Sdim const VarDecl *var) { 559224145Sdim lvalue.setAddress(CGF.BuildBlockByrefAddress(lvalue.getAddress(), var)); 560224145Sdim} 561224145Sdim 562224145Sdimvoid CodeGenFunction::EmitScalarInit(const Expr *init, 563224145Sdim const ValueDecl *D, 564224145Sdim LValue lvalue, 565224145Sdim bool capturedByInit) { 566224145Sdim Qualifiers::ObjCLifetime lifetime = lvalue.getObjCLifetime(); 567224145Sdim if (!lifetime) { 568224145Sdim llvm::Value *value = EmitScalarExpr(init); 569224145Sdim if (capturedByInit) 570224145Sdim drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D)); 571234353Sdim EmitStoreThroughLValue(RValue::get(value), lvalue, true); 572224145Sdim return; 573224145Sdim } 574224145Sdim 575224145Sdim // If we're emitting a value with lifetime, we have to do the 576224145Sdim // initialization *before* we leave the cleanup scopes. 577234353Sdim if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(init)) { 578234353Sdim enterFullExpression(ewc); 579234353Sdim init = ewc->getSubExpr(); 580234353Sdim } 581224145Sdim CodeGenFunction::RunCleanupsScope Scope(*this); 582224145Sdim 583224145Sdim // We have to maintain the illusion that the variable is 584224145Sdim // zero-initialized. If the variable might be accessed in its 585224145Sdim // initializer, zero-initialize before running the initializer, then 586224145Sdim // actually perform the initialization with an assign. 587224145Sdim bool accessedByInit = false; 588224145Sdim if (lifetime != Qualifiers::OCL_ExplicitNone) 589226633Sdim accessedByInit = (capturedByInit || isAccessedBy(D, init)); 590224145Sdim if (accessedByInit) { 591224145Sdim LValue tempLV = lvalue; 592224145Sdim // Drill down to the __block object if necessary. 593224145Sdim if (capturedByInit) { 594224145Sdim // We can use a simple GEP for this because it can't have been 595224145Sdim // moved yet. 596224145Sdim tempLV.setAddress(Builder.CreateStructGEP(tempLV.getAddress(), 597224145Sdim getByRefValueLLVMField(cast<VarDecl>(D)))); 598224145Sdim } 599224145Sdim 600226633Sdim llvm::PointerType *ty 601224145Sdim = cast<llvm::PointerType>(tempLV.getAddress()->getType()); 602224145Sdim ty = cast<llvm::PointerType>(ty->getElementType()); 603224145Sdim 604224145Sdim llvm::Value *zero = llvm::ConstantPointerNull::get(ty); 605226633Sdim 606224145Sdim // If __weak, we want to use a barrier under certain conditions. 607224145Sdim if (lifetime == Qualifiers::OCL_Weak) 608224145Sdim EmitARCInitWeak(tempLV.getAddress(), zero); 609224145Sdim 610224145Sdim // Otherwise just do a simple store. 611224145Sdim else 612234353Sdim EmitStoreOfScalar(zero, tempLV, /* isInitialization */ true); 613224145Sdim } 614224145Sdim 615224145Sdim // Emit the initializer. 616224145Sdim llvm::Value *value = 0; 617224145Sdim 618224145Sdim switch (lifetime) { 619224145Sdim case Qualifiers::OCL_None: 620224145Sdim llvm_unreachable("present but none"); 621224145Sdim 622224145Sdim case Qualifiers::OCL_ExplicitNone: 623224145Sdim // nothing to do 624224145Sdim value = EmitScalarExpr(init); 625224145Sdim break; 626224145Sdim 627224145Sdim case Qualifiers::OCL_Strong: { 628224145Sdim value = EmitARCRetainScalarExpr(init); 629224145Sdim break; 630224145Sdim } 631224145Sdim 632224145Sdim case Qualifiers::OCL_Weak: { 633224145Sdim // No way to optimize a producing initializer into this. It's not 634224145Sdim // worth optimizing for, because the value will immediately 635224145Sdim // disappear in the common case. 636224145Sdim value = EmitScalarExpr(init); 637224145Sdim 638224145Sdim if (capturedByInit) drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D)); 639224145Sdim if (accessedByInit) 640224145Sdim EmitARCStoreWeak(lvalue.getAddress(), value, /*ignored*/ true); 641224145Sdim else 642224145Sdim EmitARCInitWeak(lvalue.getAddress(), value); 643224145Sdim return; 644224145Sdim } 645224145Sdim 646224145Sdim case Qualifiers::OCL_Autoreleasing: 647224145Sdim value = EmitARCRetainAutoreleaseScalarExpr(init); 648224145Sdim break; 649224145Sdim } 650224145Sdim 651224145Sdim if (capturedByInit) drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D)); 652224145Sdim 653224145Sdim // If the variable might have been accessed by its initializer, we 654224145Sdim // might have to initialize with a barrier. We have to do this for 655224145Sdim // both __weak and __strong, but __weak got filtered out above. 656224145Sdim if (accessedByInit && lifetime == Qualifiers::OCL_Strong) { 657263508Sdim llvm::Value *oldValue = EmitLoadOfScalar(lvalue, init->getExprLoc()); 658234353Sdim EmitStoreOfScalar(value, lvalue, /* isInitialization */ true); 659249423Sdim EmitARCRelease(oldValue, ARCImpreciseLifetime); 660224145Sdim return; 661224145Sdim } 662224145Sdim 663234353Sdim EmitStoreOfScalar(value, lvalue, /* isInitialization */ true); 664224145Sdim} 665224145Sdim 666224145Sdim/// EmitScalarInit - Initialize the given lvalue with the given object. 667224145Sdimvoid CodeGenFunction::EmitScalarInit(llvm::Value *init, LValue lvalue) { 668224145Sdim Qualifiers::ObjCLifetime lifetime = lvalue.getObjCLifetime(); 669224145Sdim if (!lifetime) 670234353Sdim return EmitStoreThroughLValue(RValue::get(init), lvalue, true); 671224145Sdim 672224145Sdim switch (lifetime) { 673224145Sdim case Qualifiers::OCL_None: 674224145Sdim llvm_unreachable("present but none"); 675224145Sdim 676224145Sdim case Qualifiers::OCL_ExplicitNone: 677224145Sdim // nothing to do 678224145Sdim break; 679224145Sdim 680224145Sdim case Qualifiers::OCL_Strong: 681224145Sdim init = EmitARCRetain(lvalue.getType(), init); 682224145Sdim break; 683224145Sdim 684224145Sdim case Qualifiers::OCL_Weak: 685224145Sdim // Initialize and then skip the primitive store. 686224145Sdim EmitARCInitWeak(lvalue.getAddress(), init); 687224145Sdim return; 688224145Sdim 689224145Sdim case Qualifiers::OCL_Autoreleasing: 690224145Sdim init = EmitARCRetainAutorelease(lvalue.getType(), init); 691224145Sdim break; 692224145Sdim } 693224145Sdim 694234353Sdim EmitStoreOfScalar(init, lvalue, /* isInitialization */ true); 695224145Sdim} 696224145Sdim 697218893Sdim/// canEmitInitWithFewStoresAfterMemset - Decide whether we can emit the 698218893Sdim/// non-zero parts of the specified initializer with equal or fewer than 699218893Sdim/// NumStores scalar stores. 700218893Sdimstatic bool canEmitInitWithFewStoresAfterMemset(llvm::Constant *Init, 701218893Sdim unsigned &NumStores) { 702218893Sdim // Zero and Undef never requires any extra stores. 703218893Sdim if (isa<llvm::ConstantAggregateZero>(Init) || 704218893Sdim isa<llvm::ConstantPointerNull>(Init) || 705218893Sdim isa<llvm::UndefValue>(Init)) 706218893Sdim return true; 707218893Sdim if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) || 708218893Sdim isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) || 709218893Sdim isa<llvm::ConstantExpr>(Init)) 710218893Sdim return Init->isNullValue() || NumStores--; 711218893Sdim 712218893Sdim // See if we can emit each element. 713218893Sdim if (isa<llvm::ConstantArray>(Init) || isa<llvm::ConstantStruct>(Init)) { 714218893Sdim for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) { 715218893Sdim llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i)); 716218893Sdim if (!canEmitInitWithFewStoresAfterMemset(Elt, NumStores)) 717218893Sdim return false; 718218893Sdim } 719218893Sdim return true; 720218893Sdim } 721234353Sdim 722234353Sdim if (llvm::ConstantDataSequential *CDS = 723234353Sdim dyn_cast<llvm::ConstantDataSequential>(Init)) { 724234353Sdim for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) { 725234353Sdim llvm::Constant *Elt = CDS->getElementAsConstant(i); 726234353Sdim if (!canEmitInitWithFewStoresAfterMemset(Elt, NumStores)) 727234353Sdim return false; 728234353Sdim } 729234353Sdim return true; 730234353Sdim } 731226633Sdim 732218893Sdim // Anything else is hard and scary. 733218893Sdim return false; 734218893Sdim} 735218893Sdim 736218893Sdim/// emitStoresForInitAfterMemset - For inits that 737218893Sdim/// canEmitInitWithFewStoresAfterMemset returned true for, emit the scalar 738218893Sdim/// stores that would be required. 739218893Sdimstatic void emitStoresForInitAfterMemset(llvm::Constant *Init, llvm::Value *Loc, 740219077Sdim bool isVolatile, CGBuilderTy &Builder) { 741243830Sdim assert(!Init->isNullValue() && !isa<llvm::UndefValue>(Init) && 742243830Sdim "called emitStoresForInitAfterMemset for zero or undef value."); 743226633Sdim 744218893Sdim if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) || 745218893Sdim isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) || 746218893Sdim isa<llvm::ConstantExpr>(Init)) { 747234353Sdim Builder.CreateStore(Init, Loc, isVolatile); 748218893Sdim return; 749218893Sdim } 750234353Sdim 751234353Sdim if (llvm::ConstantDataSequential *CDS = 752234353Sdim dyn_cast<llvm::ConstantDataSequential>(Init)) { 753234353Sdim for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) { 754234353Sdim llvm::Constant *Elt = CDS->getElementAsConstant(i); 755243830Sdim 756243830Sdim // If necessary, get a pointer to the element and emit it. 757243830Sdim if (!Elt->isNullValue() && !isa<llvm::UndefValue>(Elt)) 758243830Sdim emitStoresForInitAfterMemset(Elt, Builder.CreateConstGEP2_32(Loc, 0, i), 759243830Sdim isVolatile, Builder); 760234353Sdim } 761234353Sdim return; 762234353Sdim } 763226633Sdim 764218893Sdim assert((isa<llvm::ConstantStruct>(Init) || isa<llvm::ConstantArray>(Init)) && 765218893Sdim "Unknown value type!"); 766226633Sdim 767218893Sdim for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) { 768218893Sdim llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i)); 769243830Sdim 770243830Sdim // If necessary, get a pointer to the element and emit it. 771243830Sdim if (!Elt->isNullValue() && !isa<llvm::UndefValue>(Elt)) 772243830Sdim emitStoresForInitAfterMemset(Elt, Builder.CreateConstGEP2_32(Loc, 0, i), 773243830Sdim isVolatile, Builder); 774218893Sdim } 775218893Sdim} 776218893Sdim 777218893Sdim 778218893Sdim/// shouldUseMemSetPlusStoresToInitialize - Decide whether we should use memset 779218893Sdim/// plus some stores to initialize a local variable instead of using a memcpy 780218893Sdim/// from a constant global. It is beneficial to use memset if the global is all 781218893Sdim/// zeros, or mostly zeros and large. 782218893Sdimstatic bool shouldUseMemSetPlusStoresToInitialize(llvm::Constant *Init, 783218893Sdim uint64_t GlobalSize) { 784218893Sdim // If a global is all zeros, always use a memset. 785218893Sdim if (isa<llvm::ConstantAggregateZero>(Init)) return true; 786218893Sdim 787218893Sdim // If a non-zero global is <= 32 bytes, always use a memcpy. If it is large, 788218893Sdim // do it if it will require 6 or fewer scalar stores. 789218893Sdim // TODO: Should budget depends on the size? Avoiding a large global warrants 790218893Sdim // plopping in more stores. 791218893Sdim unsigned StoreBudget = 6; 792218893Sdim uint64_t SizeLimit = 32; 793226633Sdim 794226633Sdim return GlobalSize > SizeLimit && 795218893Sdim canEmitInitWithFewStoresAfterMemset(Init, StoreBudget); 796218893Sdim} 797218893Sdim 798249423Sdim/// Should we use the LLVM lifetime intrinsics for the given local variable? 799249423Sdimstatic bool shouldUseLifetimeMarkers(CodeGenFunction &CGF, const VarDecl &D, 800249423Sdim unsigned Size) { 801249423Sdim // Always emit lifetime markers in -fsanitize=use-after-scope mode. 802249423Sdim if (CGF.getLangOpts().Sanitize.UseAfterScope) 803249423Sdim return true; 804249423Sdim // For now, only in optimized builds. 805249423Sdim if (CGF.CGM.getCodeGenOpts().OptimizationLevel == 0) 806249423Sdim return false; 807218893Sdim 808249423Sdim // Limit the size of marked objects to 32 bytes. We don't want to increase 809249423Sdim // compile time by marking tiny objects. 810249423Sdim unsigned SizeThreshold = 32; 811249423Sdim 812249423Sdim return Size > SizeThreshold; 813249423Sdim} 814249423Sdim 815249423Sdim 816218893Sdim/// EmitAutoVarDecl - Emit code and set up an entry in LocalDeclMap for a 817193326Sed/// variable declaration with auto, register, or no storage class specifier. 818193326Sed/// These turn into simple stack objects, or GlobalValues depending on target. 819219077Sdimvoid CodeGenFunction::EmitAutoVarDecl(const VarDecl &D) { 820219077Sdim AutoVarEmission emission = EmitAutoVarAlloca(D); 821219077Sdim EmitAutoVarInit(emission); 822219077Sdim EmitAutoVarCleanups(emission); 823219077Sdim} 824219077Sdim 825219077Sdim/// EmitAutoVarAlloca - Emit the alloca and debug information for a 826219077Sdim/// local variable. Does not emit initalization or destruction. 827219077SdimCodeGenFunction::AutoVarEmission 828219077SdimCodeGenFunction::EmitAutoVarAlloca(const VarDecl &D) { 829193326Sed QualType Ty = D.getType(); 830219077Sdim 831219077Sdim AutoVarEmission emission(D); 832219077Sdim 833195341Sed bool isByRef = D.hasAttr<BlocksAttr>(); 834219077Sdim emission.IsByRef = isByRef; 835193326Sed 836219077Sdim CharUnits alignment = getContext().getDeclAlign(&D); 837219077Sdim emission.Alignment = alignment; 838219077Sdim 839224145Sdim // If the type is variably-modified, emit all the VLA sizes for it. 840224145Sdim if (Ty->isVariablyModifiedType()) 841224145Sdim EmitVariablyModifiedType(Ty); 842224145Sdim 843193326Sed llvm::Value *DeclPtr; 844193326Sed if (Ty->isConstantSizeType()) { 845249423Sdim bool NRVO = getLangOpts().ElideConstructors && 846249423Sdim D.isNRVOVariable(); 847219077Sdim 848263508Sdim // If this value is an array or struct with a statically determinable 849263508Sdim // constant initializer, there are optimizations we can do. 850249423Sdim // 851249423Sdim // TODO: We should constant-evaluate the initializer of any variable, 852249423Sdim // as long as it is initialized by a constant expression. Currently, 853249423Sdim // isConstantInitializer produces wrong answers for structs with 854249423Sdim // reference or bitfield members, and a few other cases, and checking 855249423Sdim // for POD-ness protects us from some of these. 856263508Sdim if (D.getInit() && (Ty->isArrayType() || Ty->isRecordType()) && 857263508Sdim (D.isConstexpr() || 858263508Sdim ((Ty.isPODType(getContext()) || 859263508Sdim getContext().getBaseElementType(Ty)->isObjCObjectPointerType()) && 860263508Sdim D.getInit()->isConstantInitializer(getContext(), false)))) { 861219077Sdim 862249423Sdim // If the variable's a const type, and it's neither an NRVO 863249423Sdim // candidate nor a __block variable and has no mutable members, 864249423Sdim // emit it as a global instead. 865249423Sdim if (CGM.getCodeGenOpts().MergeAllConstants && !NRVO && !isByRef && 866249423Sdim CGM.isTypeConstant(Ty, true)) { 867249423Sdim EmitStaticVarDecl(D, llvm::GlobalValue::InternalLinkage); 868219077Sdim 869249423Sdim emission.Address = 0; // signal this condition to later callbacks 870249423Sdim assert(emission.wasEmittedAsGlobal()); 871249423Sdim return emission; 872198893Srdivacky } 873226633Sdim 874249423Sdim // Otherwise, tell the initialization code that we're in this case. 875249423Sdim emission.IsConstantAggregate = true; 876249423Sdim } 877226633Sdim 878249423Sdim // A normal fixed sized variable becomes an alloca in the entry block, 879249423Sdim // unless it's an NRVO variable. 880249423Sdim llvm::Type *LTy = ConvertTypeForMem(Ty); 881226633Sdim 882249423Sdim if (NRVO) { 883249423Sdim // The named return value optimization: allocate this variable in the 884249423Sdim // return slot, so that we can elide the copy when returning this 885249423Sdim // variable (C++0x [class.copy]p34). 886249423Sdim DeclPtr = ReturnValue; 887226633Sdim 888249423Sdim if (const RecordType *RecordTy = Ty->getAs<RecordType>()) { 889249423Sdim if (!cast<CXXRecordDecl>(RecordTy->getDecl())->hasTrivialDestructor()) { 890249423Sdim // Create a flag that is used to indicate when the NRVO was applied 891249423Sdim // to this variable. Set it to zero to indicate that NRVO was not 892249423Sdim // applied. 893249423Sdim llvm::Value *Zero = Builder.getFalse(); 894249423Sdim llvm::Value *NRVOFlag = CreateTempAlloca(Zero->getType(), "nrvo"); 895249423Sdim EnsureInsertPoint(); 896249423Sdim Builder.CreateStore(Zero, NRVOFlag); 897249423Sdim 898249423Sdim // Record the NRVO flag for this variable. 899249423Sdim NRVOFlags[&D] = NRVOFlag; 900249423Sdim emission.NRVOFlag = NRVOFlag; 901208600Srdivacky } 902249423Sdim } 903249423Sdim } else { 904249423Sdim if (isByRef) 905249423Sdim LTy = BuildByRefType(&D); 906226633Sdim 907249423Sdim llvm::AllocaInst *Alloc = CreateTempAlloca(LTy); 908249423Sdim Alloc->setName(D.getName()); 909198092Srdivacky 910249423Sdim CharUnits allocaAlignment = alignment; 911249423Sdim if (isByRef) 912249423Sdim allocaAlignment = std::max(allocaAlignment, 913251662Sdim getContext().toCharUnitsFromBits(getTarget().getPointerAlign(0))); 914249423Sdim Alloc->setAlignment(allocaAlignment.getQuantity()); 915249423Sdim DeclPtr = Alloc; 916249423Sdim 917249423Sdim // Emit a lifetime intrinsic if meaningful. There's no point 918249423Sdim // in doing this if we don't have a valid insertion point (?). 919249423Sdim uint64_t size = CGM.getDataLayout().getTypeAllocSize(LTy); 920249423Sdim if (HaveInsertPoint() && shouldUseLifetimeMarkers(*this, D, size)) { 921249423Sdim llvm::Value *sizeV = llvm::ConstantInt::get(Int64Ty, size); 922249423Sdim 923249423Sdim emission.SizeForLifetimeMarkers = sizeV; 924249423Sdim llvm::Value *castAddr = Builder.CreateBitCast(Alloc, Int8PtrTy); 925249423Sdim Builder.CreateCall2(CGM.getLLVMLifetimeStartFn(), sizeV, castAddr) 926249423Sdim ->setDoesNotThrow(); 927249423Sdim } else { 928249423Sdim assert(!emission.useLifetimeMarkers()); 929208600Srdivacky } 930193326Sed } 931193326Sed } else { 932198092Srdivacky EnsureInsertPoint(); 933198092Srdivacky 934193326Sed if (!DidCallStackSave) { 935193326Sed // Save the stack. 936218893Sdim llvm::Value *Stack = CreateTempAlloca(Int8PtrTy, "saved_stack"); 937198092Srdivacky 938193326Sed llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave); 939193326Sed llvm::Value *V = Builder.CreateCall(F); 940198092Srdivacky 941193326Sed Builder.CreateStore(V, Stack); 942193326Sed 943193326Sed DidCallStackSave = true; 944198092Srdivacky 945212904Sdim // Push a cleanup block and restore the stack there. 946218893Sdim // FIXME: in general circumstances, this should be an EH cleanup. 947212904Sdim EHStack.pushCleanup<CallStackRestore>(NormalCleanup, Stack); 948193326Sed } 949198092Srdivacky 950224145Sdim llvm::Value *elementCount; 951224145Sdim QualType elementType; 952224145Sdim llvm::tie(elementCount, elementType) = getVLASize(Ty); 953193326Sed 954226633Sdim llvm::Type *llvmTy = ConvertTypeForMem(elementType); 955193326Sed 956193326Sed // Allocate memory for the array. 957224145Sdim llvm::AllocaInst *vla = Builder.CreateAlloca(llvmTy, elementCount, "vla"); 958224145Sdim vla->setAlignment(alignment.getQuantity()); 959198092Srdivacky 960224145Sdim DeclPtr = vla; 961193326Sed } 962193326Sed 963193326Sed llvm::Value *&DMEntry = LocalDeclMap[&D]; 964193326Sed assert(DMEntry == 0 && "Decl already exists in localdeclmap!"); 965193326Sed DMEntry = DeclPtr; 966219077Sdim emission.Address = DeclPtr; 967193326Sed 968193326Sed // Emit debug info for local var declaration. 969223017Sdim if (HaveInsertPoint()) 970223017Sdim if (CGDebugInfo *DI = getDebugInfo()) { 971243830Sdim if (CGM.getCodeGenOpts().getDebugInfo() 972243830Sdim >= CodeGenOptions::LimitedDebugInfo) { 973239462Sdim DI->setLocation(D.getLocation()); 974249423Sdim DI->EmitDeclareOfAutoVariable(&D, DeclPtr, Builder); 975239462Sdim } 976223017Sdim } 977198092Srdivacky 978226633Sdim if (D.hasAttr<AnnotateAttr>()) 979226633Sdim EmitVarAnnotations(&D, emission.Address); 980226633Sdim 981219077Sdim return emission; 982219077Sdim} 983219077Sdim 984219077Sdim/// Determines whether the given __block variable is potentially 985219077Sdim/// captured by the given expression. 986219077Sdimstatic bool isCapturedBy(const VarDecl &var, const Expr *e) { 987219077Sdim // Skip the most common kinds of expressions that make 988219077Sdim // hierarchy-walking expensive. 989219077Sdim e = e->IgnoreParenCasts(); 990219077Sdim 991219077Sdim if (const BlockExpr *be = dyn_cast<BlockExpr>(e)) { 992219077Sdim const BlockDecl *block = be->getBlockDecl(); 993219077Sdim for (BlockDecl::capture_const_iterator i = block->capture_begin(), 994219077Sdim e = block->capture_end(); i != e; ++i) { 995219077Sdim if (i->getVariable() == &var) 996219077Sdim return true; 997219077Sdim } 998219077Sdim 999219077Sdim // No need to walk into the subexpressions. 1000219077Sdim return false; 1001219077Sdim } 1002219077Sdim 1003226633Sdim if (const StmtExpr *SE = dyn_cast<StmtExpr>(e)) { 1004226633Sdim const CompoundStmt *CS = SE->getSubStmt(); 1005226633Sdim for (CompoundStmt::const_body_iterator BI = CS->body_begin(), 1006226633Sdim BE = CS->body_end(); BI != BE; ++BI) 1007226633Sdim if (Expr *E = dyn_cast<Expr>((*BI))) { 1008226633Sdim if (isCapturedBy(var, E)) 1009226633Sdim return true; 1010226633Sdim } 1011226633Sdim else if (DeclStmt *DS = dyn_cast<DeclStmt>((*BI))) { 1012226633Sdim // special case declarations 1013226633Sdim for (DeclStmt::decl_iterator I = DS->decl_begin(), E = DS->decl_end(); 1014226633Sdim I != E; ++I) { 1015226633Sdim if (VarDecl *VD = dyn_cast<VarDecl>((*I))) { 1016226633Sdim Expr *Init = VD->getInit(); 1017226633Sdim if (Init && isCapturedBy(var, Init)) 1018226633Sdim return true; 1019226633Sdim } 1020226633Sdim } 1021226633Sdim } 1022226633Sdim else 1023226633Sdim // FIXME. Make safe assumption assuming arbitrary statements cause capturing. 1024226633Sdim // Later, provide code to poke into statements for capture analysis. 1025226633Sdim return true; 1026226633Sdim return false; 1027226633Sdim } 1028226633Sdim 1029219077Sdim for (Stmt::const_child_range children = e->children(); children; ++children) 1030219077Sdim if (isCapturedBy(var, cast<Expr>(*children))) 1031219077Sdim return true; 1032219077Sdim 1033219077Sdim return false; 1034219077Sdim} 1035219077Sdim 1036224145Sdim/// \brief Determine whether the given initializer is trivial in the sense 1037224145Sdim/// that it requires no code to be generated. 1038224145Sdimstatic bool isTrivialInitializer(const Expr *Init) { 1039224145Sdim if (!Init) 1040224145Sdim return true; 1041226633Sdim 1042224145Sdim if (const CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) 1043224145Sdim if (CXXConstructorDecl *Constructor = Construct->getConstructor()) 1044224145Sdim if (Constructor->isTrivial() && 1045224145Sdim Constructor->isDefaultConstructor() && 1046224145Sdim !Construct->requiresZeroInitialization()) 1047224145Sdim return true; 1048226633Sdim 1049224145Sdim return false; 1050224145Sdim} 1051219077Sdimvoid CodeGenFunction::EmitAutoVarInit(const AutoVarEmission &emission) { 1052219077Sdim assert(emission.Variable && "emission was not valid!"); 1053219077Sdim 1054219077Sdim // If this was emitted as a global constant, we're done. 1055219077Sdim if (emission.wasEmittedAsGlobal()) return; 1056219077Sdim 1057219077Sdim const VarDecl &D = *emission.Variable; 1058219077Sdim QualType type = D.getType(); 1059219077Sdim 1060193326Sed // If this local has an initializer, emit it now. 1061198092Srdivacky const Expr *Init = D.getInit(); 1062198092Srdivacky 1063198092Srdivacky // If we are at an unreachable point, we don't need to emit the initializer 1064198092Srdivacky // unless it contains a label. 1065198092Srdivacky if (!HaveInsertPoint()) { 1066219077Sdim if (!Init || !ContainsLabel(Init)) return; 1067219077Sdim EnsureInsertPoint(); 1068198092Srdivacky } 1069198092Srdivacky 1070221345Sdim // Initialize the structure of a __block variable. 1071221345Sdim if (emission.IsByRef) 1072221345Sdim emitByrefStructureInit(emission); 1073219077Sdim 1074224145Sdim if (isTrivialInitializer(Init)) 1075224145Sdim return; 1076193326Sed 1077221345Sdim CharUnits alignment = emission.Alignment; 1078218893Sdim 1079219077Sdim // Check whether this is a byref variable that's potentially 1080219077Sdim // captured and moved by its own initializer. If so, we'll need to 1081219077Sdim // emit the initializer first, then copy into the variable. 1082219077Sdim bool capturedByInit = emission.IsByRef && isCapturedBy(D, Init); 1083219077Sdim 1084219077Sdim llvm::Value *Loc = 1085219077Sdim capturedByInit ? emission.Address : emission.getObjectAddress(*this); 1086219077Sdim 1087234353Sdim llvm::Constant *constant = 0; 1088263508Sdim if (emission.IsConstantAggregate || D.isConstexpr()) { 1089234353Sdim assert(!capturedByInit && "constant init contains a capturing block?"); 1090234353Sdim constant = CGM.EmitConstantInit(D, this); 1091234353Sdim } 1092234353Sdim 1093234353Sdim if (!constant) { 1094234353Sdim LValue lv = MakeAddrLValue(Loc, type, alignment); 1095224145Sdim lv.setNonGC(true); 1096224145Sdim return EmitExprAsInit(Init, &D, lv, capturedByInit); 1097224145Sdim } 1098221345Sdim 1099263508Sdim if (!emission.IsConstantAggregate) { 1100263508Sdim // For simple scalar/complex initialization, store the value directly. 1101263508Sdim LValue lv = MakeAddrLValue(Loc, type, alignment); 1102263508Sdim lv.setNonGC(true); 1103263508Sdim return EmitStoreThroughLValue(RValue::get(constant), lv, true); 1104263508Sdim } 1105263508Sdim 1106219077Sdim // If this is a simple aggregate initialization, we can optimize it 1107219077Sdim // in various ways. 1108221345Sdim bool isVolatile = type.isVolatileQualified(); 1109219077Sdim 1110221345Sdim llvm::Value *SizeVal = 1111226633Sdim llvm::ConstantInt::get(IntPtrTy, 1112221345Sdim getContext().getTypeSizeInChars(type).getQuantity()); 1113219077Sdim 1114226633Sdim llvm::Type *BP = Int8PtrTy; 1115221345Sdim if (Loc->getType() != BP) 1116226633Sdim Loc = Builder.CreateBitCast(Loc, BP); 1117221345Sdim 1118221345Sdim // If the initializer is all or mostly zeros, codegen with memset then do 1119221345Sdim // a few stores afterward. 1120226633Sdim if (shouldUseMemSetPlusStoresToInitialize(constant, 1121243830Sdim CGM.getDataLayout().getTypeAllocSize(constant->getType()))) { 1122221345Sdim Builder.CreateMemSet(Loc, llvm::ConstantInt::get(Int8Ty, 0), SizeVal, 1123221345Sdim alignment.getQuantity(), isVolatile); 1124243830Sdim // Zero and undef don't require a stores. 1125243830Sdim if (!constant->isNullValue() && !isa<llvm::UndefValue>(constant)) { 1126221345Sdim Loc = Builder.CreateBitCast(Loc, constant->getType()->getPointerTo()); 1127221345Sdim emitStoresForInitAfterMemset(constant, Loc, isVolatile, Builder); 1128221345Sdim } 1129221345Sdim } else { 1130226633Sdim // Otherwise, create a temporary global with the initializer then 1131221345Sdim // memcpy from the global to the alloca. 1132221345Sdim std::string Name = GetStaticDeclName(*this, D, "."); 1133221345Sdim llvm::GlobalVariable *GV = 1134221345Sdim new llvm::GlobalVariable(CGM.getModule(), constant->getType(), true, 1135226633Sdim llvm::GlobalValue::PrivateLinkage, 1136239462Sdim constant, Name); 1137221345Sdim GV->setAlignment(alignment.getQuantity()); 1138223017Sdim GV->setUnnamedAddr(true); 1139226633Sdim 1140221345Sdim llvm::Value *SrcPtr = GV; 1141221345Sdim if (SrcPtr->getType() != BP) 1142226633Sdim SrcPtr = Builder.CreateBitCast(SrcPtr, BP); 1143206275Srdivacky 1144221345Sdim Builder.CreateMemCpy(Loc, SrcPtr, SizeVal, alignment.getQuantity(), 1145221345Sdim isVolatile); 1146221345Sdim } 1147221345Sdim} 1148221345Sdim 1149221345Sdim/// Emit an expression as an initializer for a variable at the given 1150221345Sdim/// location. The expression is not necessarily the normal 1151221345Sdim/// initializer for the variable, and the address is not necessarily 1152221345Sdim/// its normal location. 1153221345Sdim/// 1154221345Sdim/// \param init the initializing expression 1155221345Sdim/// \param var the variable to act as if we're initializing 1156221345Sdim/// \param loc the address to initialize; its type is a pointer 1157221345Sdim/// to the LLVM mapping of the variable's type 1158221345Sdim/// \param alignment the alignment of the address 1159221345Sdim/// \param capturedByInit true if the variable is a __block variable 1160221345Sdim/// whose address is potentially changed by the initializer 1161221345Sdimvoid CodeGenFunction::EmitExprAsInit(const Expr *init, 1162224145Sdim const ValueDecl *D, 1163224145Sdim LValue lvalue, 1164221345Sdim bool capturedByInit) { 1165224145Sdim QualType type = D->getType(); 1166221345Sdim 1167221345Sdim if (type->isReferenceType()) { 1168263508Sdim RValue rvalue = EmitReferenceBindingToExpr(init); 1169226633Sdim if (capturedByInit) 1170224145Sdim drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D)); 1171234353Sdim EmitStoreThroughLValue(rvalue, lvalue, true); 1172249423Sdim return; 1173249423Sdim } 1174249423Sdim switch (getEvaluationKind(type)) { 1175249423Sdim case TEK_Scalar: 1176224145Sdim EmitScalarInit(init, D, lvalue, capturedByInit); 1177249423Sdim return; 1178249423Sdim case TEK_Complex: { 1179221345Sdim ComplexPairTy complex = EmitComplexExpr(init); 1180224145Sdim if (capturedByInit) 1181224145Sdim drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D)); 1182249423Sdim EmitStoreOfComplex(complex, lvalue, /*init*/ true); 1183249423Sdim return; 1184249423Sdim } 1185249423Sdim case TEK_Aggregate: 1186249423Sdim if (type->isAtomicType()) { 1187249423Sdim EmitAtomicInit(const_cast<Expr*>(init), lvalue); 1188249423Sdim } else { 1189249423Sdim // TODO: how can we delay here if D is captured by its initializer? 1190249423Sdim EmitAggExpr(init, AggValueSlot::forLValue(lvalue, 1191226633Sdim AggValueSlot::IsDestructed, 1192226633Sdim AggValueSlot::DoesNotNeedGCBarriers, 1193226633Sdim AggValueSlot::IsNotAliased)); 1194249423Sdim } 1195249423Sdim return; 1196205219Srdivacky } 1197249423Sdim llvm_unreachable("bad evaluation kind"); 1198219077Sdim} 1199210299Sed 1200224145Sdim/// Enter a destroy cleanup for the given local variable. 1201224145Sdimvoid CodeGenFunction::emitAutoVarTypeCleanup( 1202224145Sdim const CodeGenFunction::AutoVarEmission &emission, 1203224145Sdim QualType::DestructionKind dtorKind) { 1204224145Sdim assert(dtorKind != QualType::DK_none); 1205224145Sdim 1206224145Sdim // Note that for __block variables, we want to destroy the 1207224145Sdim // original stack object, not the possibly forwarded object. 1208224145Sdim llvm::Value *addr = emission.getObjectAddress(*this); 1209224145Sdim 1210224145Sdim const VarDecl *var = emission.Variable; 1211224145Sdim QualType type = var->getType(); 1212224145Sdim 1213224145Sdim CleanupKind cleanupKind = NormalAndEHCleanup; 1214224145Sdim CodeGenFunction::Destroyer *destroyer = 0; 1215224145Sdim 1216224145Sdim switch (dtorKind) { 1217224145Sdim case QualType::DK_none: 1218224145Sdim llvm_unreachable("no cleanup for trivially-destructible variable"); 1219224145Sdim 1220224145Sdim case QualType::DK_cxx_destructor: 1221224145Sdim // If there's an NRVO flag on the emission, we need a different 1222224145Sdim // cleanup. 1223224145Sdim if (emission.NRVOFlag) { 1224224145Sdim assert(!type->isArrayType()); 1225224145Sdim CXXDestructorDecl *dtor = type->getAsCXXRecordDecl()->getDestructor(); 1226224145Sdim EHStack.pushCleanup<DestroyNRVOVariable>(cleanupKind, addr, dtor, 1227224145Sdim emission.NRVOFlag); 1228224145Sdim return; 1229224145Sdim } 1230224145Sdim break; 1231224145Sdim 1232224145Sdim case QualType::DK_objc_strong_lifetime: 1233224145Sdim // Suppress cleanups for pseudo-strong variables. 1234224145Sdim if (var->isARCPseudoStrong()) return; 1235226633Sdim 1236224145Sdim // Otherwise, consider whether to use an EH cleanup or not. 1237224145Sdim cleanupKind = getARCCleanupKind(); 1238224145Sdim 1239224145Sdim // Use the imprecise destroyer by default. 1240224145Sdim if (!var->hasAttr<ObjCPreciseLifetimeAttr>()) 1241224145Sdim destroyer = CodeGenFunction::destroyARCStrongImprecise; 1242224145Sdim break; 1243224145Sdim 1244224145Sdim case QualType::DK_objc_weak_lifetime: 1245224145Sdim break; 1246224145Sdim } 1247224145Sdim 1248224145Sdim // If we haven't chosen a more specific destroyer, use the default. 1249234353Sdim if (!destroyer) destroyer = getDestroyer(dtorKind); 1250224145Sdim 1251224145Sdim // Use an EH cleanup in array destructors iff the destructor itself 1252224145Sdim // is being pushed as an EH cleanup. 1253224145Sdim bool useEHCleanup = (cleanupKind & EHCleanup); 1254224145Sdim EHStack.pushCleanup<DestroyObject>(cleanupKind, addr, type, destroyer, 1255224145Sdim useEHCleanup); 1256224145Sdim} 1257224145Sdim 1258219077Sdimvoid CodeGenFunction::EmitAutoVarCleanups(const AutoVarEmission &emission) { 1259219077Sdim assert(emission.Variable && "emission was not valid!"); 1260219077Sdim 1261219077Sdim // If this was emitted as a global constant, we're done. 1262219077Sdim if (emission.wasEmittedAsGlobal()) return; 1263219077Sdim 1264234982Sdim // If we don't have an insertion point, we're done. Sema prevents 1265234982Sdim // us from jumping into any of these scopes anyway. 1266234982Sdim if (!HaveInsertPoint()) return; 1267234982Sdim 1268219077Sdim const VarDecl &D = *emission.Variable; 1269219077Sdim 1270249423Sdim // Make sure we call @llvm.lifetime.end. This needs to happen 1271249423Sdim // *last*, so the cleanup needs to be pushed *first*. 1272249423Sdim if (emission.useLifetimeMarkers()) { 1273249423Sdim EHStack.pushCleanup<CallLifetimeEnd>(NormalCleanup, 1274249423Sdim emission.getAllocatedAddress(), 1275249423Sdim emission.getSizeForLifetimeMarkers()); 1276249423Sdim } 1277249423Sdim 1278224145Sdim // Check the type for a cleanup. 1279224145Sdim if (QualType::DestructionKind dtorKind = D.getType().isDestructedType()) 1280224145Sdim emitAutoVarTypeCleanup(emission, dtorKind); 1281219077Sdim 1282224145Sdim // In GC mode, honor objc_precise_lifetime. 1283234353Sdim if (getLangOpts().getGC() != LangOptions::NonGC && 1284224145Sdim D.hasAttr<ObjCPreciseLifetimeAttr>()) { 1285224145Sdim EHStack.pushCleanup<ExtendGCLifetime>(NormalCleanup, &D); 1286198092Srdivacky } 1287198092Srdivacky 1288219077Sdim // Handle the cleanup attribute. 1289195341Sed if (const CleanupAttr *CA = D.getAttr<CleanupAttr>()) { 1290193326Sed const FunctionDecl *FD = CA->getFunctionDecl(); 1291198092Srdivacky 1292219077Sdim llvm::Constant *F = CGM.GetAddrOfFunction(FD); 1293193326Sed assert(F && "Could not find function!"); 1294198092Srdivacky 1295234353Sdim const CGFunctionInfo &Info = CGM.getTypes().arrangeFunctionDeclaration(FD); 1296219077Sdim EHStack.pushCleanup<CallCleanupFunction>(NormalAndEHCleanup, F, &Info, &D); 1297193326Sed } 1298193326Sed 1299219077Sdim // If this is a block variable, call _Block_object_destroy 1300219077Sdim // (on the unforwarded address). 1301221345Sdim if (emission.IsByRef) 1302221345Sdim enterByrefCleanup(emission); 1303193326Sed} 1304193326Sed 1305234353SdimCodeGenFunction::Destroyer * 1306224145SdimCodeGenFunction::getDestroyer(QualType::DestructionKind kind) { 1307224145Sdim switch (kind) { 1308224145Sdim case QualType::DK_none: llvm_unreachable("no destroyer for trivial dtor"); 1309224145Sdim case QualType::DK_cxx_destructor: 1310234353Sdim return destroyCXXObject; 1311224145Sdim case QualType::DK_objc_strong_lifetime: 1312234353Sdim return destroyARCStrongPrecise; 1313224145Sdim case QualType::DK_objc_weak_lifetime: 1314234353Sdim return destroyARCWeak; 1315224145Sdim } 1316234353Sdim llvm_unreachable("Unknown DestructionKind"); 1317224145Sdim} 1318224145Sdim 1319249423Sdim/// pushEHDestroy - Push the standard destructor for the given type as 1320249423Sdim/// an EH-only cleanup. 1321249423Sdimvoid CodeGenFunction::pushEHDestroy(QualType::DestructionKind dtorKind, 1322249423Sdim llvm::Value *addr, QualType type) { 1323249423Sdim assert(dtorKind && "cannot push destructor for trivial type"); 1324249423Sdim assert(needsEHCleanup(dtorKind)); 1325249423Sdim 1326249423Sdim pushDestroy(EHCleanup, addr, type, getDestroyer(dtorKind), true); 1327249423Sdim} 1328249423Sdim 1329249423Sdim/// pushDestroy - Push the standard destructor for the given type as 1330249423Sdim/// at least a normal cleanup. 1331224145Sdimvoid CodeGenFunction::pushDestroy(QualType::DestructionKind dtorKind, 1332224145Sdim llvm::Value *addr, QualType type) { 1333224145Sdim assert(dtorKind && "cannot push destructor for trivial type"); 1334224145Sdim 1335224145Sdim CleanupKind cleanupKind = getCleanupKind(dtorKind); 1336224145Sdim pushDestroy(cleanupKind, addr, type, getDestroyer(dtorKind), 1337224145Sdim cleanupKind & EHCleanup); 1338224145Sdim} 1339224145Sdim 1340224145Sdimvoid CodeGenFunction::pushDestroy(CleanupKind cleanupKind, llvm::Value *addr, 1341234353Sdim QualType type, Destroyer *destroyer, 1342224145Sdim bool useEHCleanupForArray) { 1343224145Sdim pushFullExprCleanup<DestroyObject>(cleanupKind, addr, type, 1344224145Sdim destroyer, useEHCleanupForArray); 1345224145Sdim} 1346224145Sdim 1347263508Sdimvoid CodeGenFunction::pushLifetimeExtendedDestroy( 1348263508Sdim CleanupKind cleanupKind, llvm::Value *addr, QualType type, 1349263508Sdim Destroyer *destroyer, bool useEHCleanupForArray) { 1350263508Sdim assert(!isInConditionalBranch() && 1351263508Sdim "performing lifetime extension from within conditional"); 1352263508Sdim 1353263508Sdim // Push an EH-only cleanup for the object now. 1354263508Sdim // FIXME: When popping normal cleanups, we need to keep this EH cleanup 1355263508Sdim // around in case a temporary's destructor throws an exception. 1356263508Sdim if (cleanupKind & EHCleanup) 1357263508Sdim EHStack.pushCleanup<DestroyObject>( 1358263508Sdim static_cast<CleanupKind>(cleanupKind & ~NormalCleanup), addr, type, 1359263508Sdim destroyer, useEHCleanupForArray); 1360263508Sdim 1361263508Sdim // Remember that we need to push a full cleanup for the object at the 1362263508Sdim // end of the full-expression. 1363263508Sdim pushCleanupAfterFullExpr<DestroyObject>( 1364263508Sdim cleanupKind, addr, type, destroyer, useEHCleanupForArray); 1365263508Sdim} 1366263508Sdim 1367224145Sdim/// emitDestroy - Immediately perform the destruction of the given 1368224145Sdim/// object. 1369224145Sdim/// 1370224145Sdim/// \param addr - the address of the object; a type* 1371224145Sdim/// \param type - the type of the object; if an array type, all 1372224145Sdim/// objects are destroyed in reverse order 1373224145Sdim/// \param destroyer - the function to call to destroy individual 1374224145Sdim/// elements 1375224145Sdim/// \param useEHCleanupForArray - whether an EH cleanup should be 1376224145Sdim/// used when destroying array elements, in case one of the 1377224145Sdim/// destructions throws an exception 1378224145Sdimvoid CodeGenFunction::emitDestroy(llvm::Value *addr, QualType type, 1379234353Sdim Destroyer *destroyer, 1380224145Sdim bool useEHCleanupForArray) { 1381224145Sdim const ArrayType *arrayType = getContext().getAsArrayType(type); 1382224145Sdim if (!arrayType) 1383224145Sdim return destroyer(*this, addr, type); 1384224145Sdim 1385224145Sdim llvm::Value *begin = addr; 1386224145Sdim llvm::Value *length = emitArrayLength(arrayType, type, begin); 1387224145Sdim 1388224145Sdim // Normally we have to check whether the array is zero-length. 1389224145Sdim bool checkZeroLength = true; 1390224145Sdim 1391224145Sdim // But if the array length is constant, we can suppress that. 1392224145Sdim if (llvm::ConstantInt *constLength = dyn_cast<llvm::ConstantInt>(length)) { 1393224145Sdim // ...and if it's constant zero, we can just skip the entire thing. 1394224145Sdim if (constLength->isZero()) return; 1395224145Sdim checkZeroLength = false; 1396224145Sdim } 1397224145Sdim 1398224145Sdim llvm::Value *end = Builder.CreateInBoundsGEP(begin, length); 1399224145Sdim emitArrayDestroy(begin, end, type, destroyer, 1400224145Sdim checkZeroLength, useEHCleanupForArray); 1401224145Sdim} 1402224145Sdim 1403224145Sdim/// emitArrayDestroy - Destroys all the elements of the given array, 1404224145Sdim/// beginning from last to first. The array cannot be zero-length. 1405224145Sdim/// 1406224145Sdim/// \param begin - a type* denoting the first element of the array 1407224145Sdim/// \param end - a type* denoting one past the end of the array 1408224145Sdim/// \param type - the element type of the array 1409224145Sdim/// \param destroyer - the function to call to destroy elements 1410224145Sdim/// \param useEHCleanup - whether to push an EH cleanup to destroy 1411224145Sdim/// the remaining elements in case the destruction of a single 1412224145Sdim/// element throws 1413224145Sdimvoid CodeGenFunction::emitArrayDestroy(llvm::Value *begin, 1414224145Sdim llvm::Value *end, 1415224145Sdim QualType type, 1416234353Sdim Destroyer *destroyer, 1417224145Sdim bool checkZeroLength, 1418224145Sdim bool useEHCleanup) { 1419224145Sdim assert(!type->isArrayType()); 1420224145Sdim 1421224145Sdim // The basic structure here is a do-while loop, because we don't 1422224145Sdim // need to check for the zero-element case. 1423224145Sdim llvm::BasicBlock *bodyBB = createBasicBlock("arraydestroy.body"); 1424224145Sdim llvm::BasicBlock *doneBB = createBasicBlock("arraydestroy.done"); 1425224145Sdim 1426224145Sdim if (checkZeroLength) { 1427224145Sdim llvm::Value *isEmpty = Builder.CreateICmpEQ(begin, end, 1428224145Sdim "arraydestroy.isempty"); 1429224145Sdim Builder.CreateCondBr(isEmpty, doneBB, bodyBB); 1430224145Sdim } 1431224145Sdim 1432224145Sdim // Enter the loop body, making that address the current address. 1433224145Sdim llvm::BasicBlock *entryBB = Builder.GetInsertBlock(); 1434224145Sdim EmitBlock(bodyBB); 1435224145Sdim llvm::PHINode *elementPast = 1436224145Sdim Builder.CreatePHI(begin->getType(), 2, "arraydestroy.elementPast"); 1437224145Sdim elementPast->addIncoming(end, entryBB); 1438224145Sdim 1439224145Sdim // Shift the address back by one element. 1440224145Sdim llvm::Value *negativeOne = llvm::ConstantInt::get(SizeTy, -1, true); 1441224145Sdim llvm::Value *element = Builder.CreateInBoundsGEP(elementPast, negativeOne, 1442224145Sdim "arraydestroy.element"); 1443224145Sdim 1444224145Sdim if (useEHCleanup) 1445224145Sdim pushRegularPartialArrayCleanup(begin, element, type, destroyer); 1446224145Sdim 1447224145Sdim // Perform the actual destruction there. 1448224145Sdim destroyer(*this, element, type); 1449224145Sdim 1450224145Sdim if (useEHCleanup) 1451224145Sdim PopCleanupBlock(); 1452224145Sdim 1453224145Sdim // Check whether we've reached the end. 1454224145Sdim llvm::Value *done = Builder.CreateICmpEQ(element, begin, "arraydestroy.done"); 1455224145Sdim Builder.CreateCondBr(done, doneBB, bodyBB); 1456224145Sdim elementPast->addIncoming(element, Builder.GetInsertBlock()); 1457224145Sdim 1458224145Sdim // Done. 1459224145Sdim EmitBlock(doneBB); 1460224145Sdim} 1461224145Sdim 1462224145Sdim/// Perform partial array destruction as if in an EH cleanup. Unlike 1463224145Sdim/// emitArrayDestroy, the element type here may still be an array type. 1464224145Sdimstatic void emitPartialArrayDestroy(CodeGenFunction &CGF, 1465224145Sdim llvm::Value *begin, llvm::Value *end, 1466224145Sdim QualType type, 1467234353Sdim CodeGenFunction::Destroyer *destroyer) { 1468224145Sdim // If the element type is itself an array, drill down. 1469224145Sdim unsigned arrayDepth = 0; 1470224145Sdim while (const ArrayType *arrayType = CGF.getContext().getAsArrayType(type)) { 1471224145Sdim // VLAs don't require a GEP index to walk into. 1472224145Sdim if (!isa<VariableArrayType>(arrayType)) 1473224145Sdim arrayDepth++; 1474224145Sdim type = arrayType->getElementType(); 1475224145Sdim } 1476224145Sdim 1477224145Sdim if (arrayDepth) { 1478224145Sdim llvm::Value *zero = llvm::ConstantInt::get(CGF.SizeTy, arrayDepth+1); 1479224145Sdim 1480226633Sdim SmallVector<llvm::Value*,4> gepIndices(arrayDepth, zero); 1481226633Sdim begin = CGF.Builder.CreateInBoundsGEP(begin, gepIndices, "pad.arraybegin"); 1482226633Sdim end = CGF.Builder.CreateInBoundsGEP(end, gepIndices, "pad.arrayend"); 1483224145Sdim } 1484224145Sdim 1485224145Sdim // Destroy the array. We don't ever need an EH cleanup because we 1486224145Sdim // assume that we're in an EH cleanup ourselves, so a throwing 1487224145Sdim // destructor causes an immediate terminate. 1488224145Sdim CGF.emitArrayDestroy(begin, end, type, destroyer, 1489224145Sdim /*checkZeroLength*/ true, /*useEHCleanup*/ false); 1490224145Sdim} 1491224145Sdim 1492224145Sdimnamespace { 1493224145Sdim /// RegularPartialArrayDestroy - a cleanup which performs a partial 1494224145Sdim /// array destroy where the end pointer is regularly determined and 1495224145Sdim /// does not need to be loaded from a local. 1496224145Sdim class RegularPartialArrayDestroy : public EHScopeStack::Cleanup { 1497224145Sdim llvm::Value *ArrayBegin; 1498224145Sdim llvm::Value *ArrayEnd; 1499224145Sdim QualType ElementType; 1500234353Sdim CodeGenFunction::Destroyer *Destroyer; 1501224145Sdim public: 1502224145Sdim RegularPartialArrayDestroy(llvm::Value *arrayBegin, llvm::Value *arrayEnd, 1503224145Sdim QualType elementType, 1504224145Sdim CodeGenFunction::Destroyer *destroyer) 1505224145Sdim : ArrayBegin(arrayBegin), ArrayEnd(arrayEnd), 1506234353Sdim ElementType(elementType), Destroyer(destroyer) {} 1507224145Sdim 1508224145Sdim void Emit(CodeGenFunction &CGF, Flags flags) { 1509224145Sdim emitPartialArrayDestroy(CGF, ArrayBegin, ArrayEnd, 1510224145Sdim ElementType, Destroyer); 1511224145Sdim } 1512224145Sdim }; 1513224145Sdim 1514224145Sdim /// IrregularPartialArrayDestroy - a cleanup which performs a 1515224145Sdim /// partial array destroy where the end pointer is irregularly 1516224145Sdim /// determined and must be loaded from a local. 1517224145Sdim class IrregularPartialArrayDestroy : public EHScopeStack::Cleanup { 1518224145Sdim llvm::Value *ArrayBegin; 1519224145Sdim llvm::Value *ArrayEndPointer; 1520224145Sdim QualType ElementType; 1521234353Sdim CodeGenFunction::Destroyer *Destroyer; 1522224145Sdim public: 1523224145Sdim IrregularPartialArrayDestroy(llvm::Value *arrayBegin, 1524224145Sdim llvm::Value *arrayEndPointer, 1525224145Sdim QualType elementType, 1526224145Sdim CodeGenFunction::Destroyer *destroyer) 1527224145Sdim : ArrayBegin(arrayBegin), ArrayEndPointer(arrayEndPointer), 1528234353Sdim ElementType(elementType), Destroyer(destroyer) {} 1529224145Sdim 1530224145Sdim void Emit(CodeGenFunction &CGF, Flags flags) { 1531224145Sdim llvm::Value *arrayEnd = CGF.Builder.CreateLoad(ArrayEndPointer); 1532224145Sdim emitPartialArrayDestroy(CGF, ArrayBegin, arrayEnd, 1533224145Sdim ElementType, Destroyer); 1534224145Sdim } 1535224145Sdim }; 1536224145Sdim} 1537224145Sdim 1538224145Sdim/// pushIrregularPartialArrayCleanup - Push an EH cleanup to destroy 1539224145Sdim/// already-constructed elements of the given array. The cleanup 1540224145Sdim/// may be popped with DeactivateCleanupBlock or PopCleanupBlock. 1541226633Sdim/// 1542224145Sdim/// \param elementType - the immediate element type of the array; 1543224145Sdim/// possibly still an array type 1544224145Sdimvoid CodeGenFunction::pushIrregularPartialArrayCleanup(llvm::Value *arrayBegin, 1545224145Sdim llvm::Value *arrayEndPointer, 1546224145Sdim QualType elementType, 1547234353Sdim Destroyer *destroyer) { 1548224145Sdim pushFullExprCleanup<IrregularPartialArrayDestroy>(EHCleanup, 1549224145Sdim arrayBegin, arrayEndPointer, 1550234353Sdim elementType, destroyer); 1551224145Sdim} 1552224145Sdim 1553224145Sdim/// pushRegularPartialArrayCleanup - Push an EH cleanup to destroy 1554224145Sdim/// already-constructed elements of the given array. The cleanup 1555224145Sdim/// may be popped with DeactivateCleanupBlock or PopCleanupBlock. 1556226633Sdim/// 1557224145Sdim/// \param elementType - the immediate element type of the array; 1558224145Sdim/// possibly still an array type 1559224145Sdimvoid CodeGenFunction::pushRegularPartialArrayCleanup(llvm::Value *arrayBegin, 1560224145Sdim llvm::Value *arrayEnd, 1561224145Sdim QualType elementType, 1562234353Sdim Destroyer *destroyer) { 1563224145Sdim pushFullExprCleanup<RegularPartialArrayDestroy>(EHCleanup, 1564224145Sdim arrayBegin, arrayEnd, 1565234353Sdim elementType, destroyer); 1566224145Sdim} 1567224145Sdim 1568249423Sdim/// Lazily declare the @llvm.lifetime.start intrinsic. 1569249423Sdimllvm::Constant *CodeGenModule::getLLVMLifetimeStartFn() { 1570249423Sdim if (LifetimeStartFn) return LifetimeStartFn; 1571249423Sdim LifetimeStartFn = llvm::Intrinsic::getDeclaration(&getModule(), 1572249423Sdim llvm::Intrinsic::lifetime_start); 1573249423Sdim return LifetimeStartFn; 1574249423Sdim} 1575249423Sdim 1576249423Sdim/// Lazily declare the @llvm.lifetime.end intrinsic. 1577249423Sdimllvm::Constant *CodeGenModule::getLLVMLifetimeEndFn() { 1578249423Sdim if (LifetimeEndFn) return LifetimeEndFn; 1579249423Sdim LifetimeEndFn = llvm::Intrinsic::getDeclaration(&getModule(), 1580249423Sdim llvm::Intrinsic::lifetime_end); 1581249423Sdim return LifetimeEndFn; 1582249423Sdim} 1583249423Sdim 1584224145Sdimnamespace { 1585224145Sdim /// A cleanup to perform a release of an object at the end of a 1586224145Sdim /// function. This is used to balance out the incoming +1 of a 1587224145Sdim /// ns_consumed argument when we can't reasonably do that just by 1588224145Sdim /// not doing the initial retain for a __block argument. 1589224145Sdim struct ConsumeARCParameter : EHScopeStack::Cleanup { 1590249423Sdim ConsumeARCParameter(llvm::Value *param, 1591249423Sdim ARCPreciseLifetime_t precise) 1592249423Sdim : Param(param), Precise(precise) {} 1593224145Sdim 1594224145Sdim llvm::Value *Param; 1595249423Sdim ARCPreciseLifetime_t Precise; 1596224145Sdim 1597224145Sdim void Emit(CodeGenFunction &CGF, Flags flags) { 1598249423Sdim CGF.EmitARCRelease(Param, Precise); 1599224145Sdim } 1600224145Sdim }; 1601224145Sdim} 1602224145Sdim 1603198092Srdivacky/// Emit an alloca (or GlobalValue depending on target) 1604193326Sed/// for the specified parameter and set up LocalDeclMap. 1605221345Sdimvoid CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg, 1606221345Sdim unsigned ArgNo) { 1607193326Sed // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl? 1608193326Sed assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) && 1609193326Sed "Invalid argument to EmitParmDecl"); 1610219077Sdim 1611219077Sdim Arg->setName(D.getName()); 1612219077Sdim 1613249423Sdim QualType Ty = D.getType(); 1614249423Sdim 1615219077Sdim // Use better IR generation for certain implicit parameters. 1616219077Sdim if (isa<ImplicitParamDecl>(D)) { 1617219077Sdim // The only implicit argument a block has is its literal. 1618219077Sdim if (BlockInfo) { 1619219077Sdim LocalDeclMap[&D] = Arg; 1620249423Sdim llvm::Value *LocalAddr = 0; 1621249423Sdim if (CGM.getCodeGenOpts().OptimizationLevel == 0) { 1622249423Sdim // Allocate a stack slot to let the debug info survive the RA. 1623249423Sdim llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertTypeForMem(Ty), 1624249423Sdim D.getName() + ".addr"); 1625249423Sdim Alloc->setAlignment(getContext().getDeclAlign(&D).getQuantity()); 1626249423Sdim LValue lv = MakeAddrLValue(Alloc, Ty, getContext().getDeclAlign(&D)); 1627249423Sdim EmitStoreOfScalar(Arg, lv, /* isInitialization */ true); 1628249423Sdim LocalAddr = Builder.CreateLoad(Alloc); 1629249423Sdim } 1630219077Sdim 1631219077Sdim if (CGDebugInfo *DI = getDebugInfo()) { 1632243830Sdim if (CGM.getCodeGenOpts().getDebugInfo() 1633243830Sdim >= CodeGenOptions::LimitedDebugInfo) { 1634239462Sdim DI->setLocation(D.getLocation()); 1635249423Sdim DI->EmitDeclareOfBlockLiteralArgVariable(*BlockInfo, Arg, LocalAddr, Builder); 1636239462Sdim } 1637219077Sdim } 1638219077Sdim 1639219077Sdim return; 1640219077Sdim } 1641219077Sdim } 1642219077Sdim 1643193326Sed llvm::Value *DeclPtr; 1644263508Sdim bool HasNonScalarEvalKind = !CodeGenFunction::hasScalarEvaluationKind(Ty); 1645203955Srdivacky // If this is an aggregate or variable sized value, reuse the input pointer. 1646263508Sdim if (HasNonScalarEvalKind || !Ty->isConstantSizeType()) { 1647193326Sed DeclPtr = Arg; 1648263508Sdim // Push a destructor cleanup for this parameter if the ABI requires it. 1649263508Sdim if (HasNonScalarEvalKind && 1650263508Sdim getTarget().getCXXABI().isArgumentDestroyedByCallee()) { 1651263508Sdim if (const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl()) { 1652263508Sdim if (RD->hasNonTrivialDestructor()) 1653263508Sdim pushDestroy(QualType::DK_cxx_destructor, DeclPtr, Ty); 1654263508Sdim } 1655263508Sdim } 1656193326Sed } else { 1657203955Srdivacky // Otherwise, create a temporary to hold the value. 1658234353Sdim llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertTypeForMem(Ty), 1659234353Sdim D.getName() + ".addr"); 1660249423Sdim CharUnits Align = getContext().getDeclAlign(&D); 1661249423Sdim Alloc->setAlignment(Align.getQuantity()); 1662234353Sdim DeclPtr = Alloc; 1663198092Srdivacky 1664224145Sdim bool doStore = true; 1665224145Sdim 1666224145Sdim Qualifiers qs = Ty.getQualifiers(); 1667249423Sdim LValue lv = MakeAddrLValue(DeclPtr, Ty, Align); 1668224145Sdim if (Qualifiers::ObjCLifetime lt = qs.getObjCLifetime()) { 1669224145Sdim // We honor __attribute__((ns_consumed)) for types with lifetime. 1670224145Sdim // For __strong, it's handled by just skipping the initial retain; 1671224145Sdim // otherwise we have to balance out the initial +1 with an extra 1672224145Sdim // cleanup to do the release at the end of the function. 1673224145Sdim bool isConsumed = D.hasAttr<NSConsumedAttr>(); 1674224145Sdim 1675224145Sdim // 'self' is always formally __strong, but if this is not an 1676224145Sdim // init method then we don't want to retain it. 1677224145Sdim if (D.isARCPseudoStrong()) { 1678224145Sdim const ObjCMethodDecl *method = cast<ObjCMethodDecl>(CurCodeDecl); 1679224145Sdim assert(&D == method->getSelfDecl()); 1680224145Sdim assert(lt == Qualifiers::OCL_Strong); 1681224145Sdim assert(qs.hasConst()); 1682224145Sdim assert(method->getMethodFamily() != OMF_init); 1683224145Sdim (void) method; 1684224145Sdim lt = Qualifiers::OCL_ExplicitNone; 1685224145Sdim } 1686224145Sdim 1687224145Sdim if (lt == Qualifiers::OCL_Strong) { 1688249423Sdim if (!isConsumed) { 1689249423Sdim if (CGM.getCodeGenOpts().OptimizationLevel == 0) { 1690249423Sdim // use objc_storeStrong(&dest, value) for retaining the 1691249423Sdim // object. But first, store a null into 'dest' because 1692249423Sdim // objc_storeStrong attempts to release its old value. 1693263508Sdim llvm::Value *Null = CGM.EmitNullConstant(D.getType()); 1694249423Sdim EmitStoreOfScalar(Null, lv, /* isInitialization */ true); 1695249423Sdim EmitARCStoreStrongCall(lv.getAddress(), Arg, true); 1696249423Sdim doStore = false; 1697249423Sdim } 1698249423Sdim else 1699224145Sdim // Don't use objc_retainBlock for block pointers, because we 1700224145Sdim // don't want to Block_copy something just because we got it 1701224145Sdim // as a parameter. 1702249423Sdim Arg = EmitARCRetainNonBlock(Arg); 1703249423Sdim } 1704224145Sdim } else { 1705224145Sdim // Push the cleanup for a consumed parameter. 1706249423Sdim if (isConsumed) { 1707249423Sdim ARCPreciseLifetime_t precise = (D.hasAttr<ObjCPreciseLifetimeAttr>() 1708249423Sdim ? ARCPreciseLifetime : ARCImpreciseLifetime); 1709249423Sdim EHStack.pushCleanup<ConsumeARCParameter>(getARCCleanupKind(), Arg, 1710249423Sdim precise); 1711249423Sdim } 1712224145Sdim 1713224145Sdim if (lt == Qualifiers::OCL_Weak) { 1714224145Sdim EmitARCInitWeak(DeclPtr, Arg); 1715234353Sdim doStore = false; // The weak init is a store, no need to do two. 1716224145Sdim } 1717224145Sdim } 1718224145Sdim 1719224145Sdim // Enter the cleanup scope. 1720224145Sdim EmitAutoVarWithLifetime(*this, D, DeclPtr, lt); 1721224145Sdim } 1722224145Sdim 1723203955Srdivacky // Store the initial value into the alloca. 1724249423Sdim if (doStore) 1725234353Sdim EmitStoreOfScalar(Arg, lv, /* isInitialization */ true); 1726193326Sed } 1727193326Sed 1728193326Sed llvm::Value *&DMEntry = LocalDeclMap[&D]; 1729193326Sed assert(DMEntry == 0 && "Decl already exists in localdeclmap!"); 1730193326Sed DMEntry = DeclPtr; 1731193326Sed 1732193326Sed // Emit debug info for param declaration. 1733239462Sdim if (CGDebugInfo *DI = getDebugInfo()) { 1734243830Sdim if (CGM.getCodeGenOpts().getDebugInfo() 1735243830Sdim >= CodeGenOptions::LimitedDebugInfo) { 1736239462Sdim DI->EmitDeclareOfArgVariable(&D, DeclPtr, ArgNo, Builder); 1737239462Sdim } 1738239462Sdim } 1739226633Sdim 1740226633Sdim if (D.hasAttr<AnnotateAttr>()) 1741226633Sdim EmitVarAnnotations(&D, DeclPtr); 1742193326Sed} 1743