diff options
Diffstat (limited to 'contrib/llvm/tools/clang/lib/CodeGen/CGDecl.cpp')
-rw-r--r-- | contrib/llvm/tools/clang/lib/CodeGen/CGDecl.cpp | 199 |
1 files changed, 104 insertions, 95 deletions
diff --git a/contrib/llvm/tools/clang/lib/CodeGen/CGDecl.cpp b/contrib/llvm/tools/clang/lib/CodeGen/CGDecl.cpp index 66d6b33..91f8041 100644 --- a/contrib/llvm/tools/clang/lib/CodeGen/CGDecl.cpp +++ b/contrib/llvm/tools/clang/lib/CodeGen/CGDecl.cpp @@ -126,17 +126,11 @@ void CodeGenFunction::EmitDecl(const Decl &D) { void CodeGenFunction::EmitVarDecl(const VarDecl &D) { if (D.isStaticLocal()) { llvm::GlobalValue::LinkageTypes Linkage = - llvm::GlobalValue::InternalLinkage; + CGM.getLLVMLinkageVarDefinition(&D, /*isConstant=*/false); - // If the variable is externally visible, it must have weak linkage so it - // can be uniqued. - if (D.isExternallyVisible()) { - Linkage = llvm::GlobalValue::LinkOnceODRLinkage; - - // FIXME: We need to force the emission/use of a guard variable for - // some variables even if we can constant-evaluate them because - // we can't guarantee every translation unit will constant-evaluate them. - } + // FIXME: We need to force the emission/use of a guard variable for + // some variables even if we can constant-evaluate them because + // we can't guarantee every translation unit will constant-evaluate them. return EmitStaticVarDecl(D, Linkage); } @@ -155,35 +149,30 @@ void CodeGenFunction::EmitVarDecl(const VarDecl &D) { static std::string GetStaticDeclName(CodeGenFunction &CGF, const VarDecl &D, const char *Separator) { CodeGenModule &CGM = CGF.CGM; - if (CGF.getLangOpts().CPlusPlus) { - StringRef Name = CGM.getMangledName(&D); - return Name.str(); - } - std::string ContextName; + if (CGF.getLangOpts().CPlusPlus) + return CGM.getMangledName(&D).str(); + + StringRef ContextName; if (!CGF.CurFuncDecl) { // Better be in a block declared in global scope. const NamedDecl *ND = cast<NamedDecl>(&D); const DeclContext *DC = ND->getDeclContext(); - if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) { - MangleBuffer Name; - CGM.getBlockMangledName(GlobalDecl(), Name, BD); - ContextName = Name.getString(); - } + if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) + ContextName = CGM.getBlockMangledName(GlobalDecl(), BD); else llvm_unreachable("Unknown context for block static var decl"); - } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CGF.CurFuncDecl)) { - StringRef Name = CGM.getMangledName(FD); - ContextName = Name.str(); - } else if (isa<ObjCMethodDecl>(CGF.CurFuncDecl)) + } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CGF.CurFuncDecl)) + ContextName = CGM.getMangledName(FD); + else if (isa<ObjCMethodDecl>(CGF.CurFuncDecl)) ContextName = CGF.CurFn->getName(); else llvm_unreachable("Unknown context for static var decl"); - return ContextName + Separator + D.getNameAsString(); + return ContextName.str() + Separator + D.getNameAsString(); } -llvm::GlobalVariable * +llvm::Constant * CodeGenFunction::CreateStaticVarDecl(const VarDecl &D, const char *Separator, llvm::GlobalValue::LinkageTypes Linkage) { @@ -203,7 +192,7 @@ CodeGenFunction::CreateStaticVarDecl(const VarDecl &D, llvm::GlobalVariable *GV = new llvm::GlobalVariable(CGM.getModule(), LTy, Ty.isConstant(getContext()), Linkage, - CGM.EmitNullConstant(D.getType()), Name, 0, + CGM.EmitNullConstant(D.getType()), Name, nullptr, llvm::GlobalVariable::NotThreadLocal, AddrSpace); GV->setAlignment(getContext().getDeclAlign(&D).getQuantity()); @@ -212,6 +201,20 @@ CodeGenFunction::CreateStaticVarDecl(const VarDecl &D, if (D.getTLSKind()) CGM.setTLSMode(GV, D); + if (D.isExternallyVisible()) { + if (D.hasAttr<DLLImportAttr>()) + GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass); + else if (D.hasAttr<DLLExportAttr>()) + GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass); + } + + // Make sure the result is of the correct type. + unsigned ExpectedAddrSpace = CGM.getContext().getTargetAddressSpace(Ty); + if (AddrSpace != ExpectedAddrSpace) { + llvm::PointerType *PTy = llvm::PointerType::get(LTy, ExpectedAddrSpace); + return llvm::ConstantExpr::getAddrSpaceCast(GV, PTy); + } + return GV; } @@ -290,7 +293,7 @@ CodeGenFunction::AddInitializerToStaticVarDecl(const VarDecl &D, void CodeGenFunction::EmitStaticVarDecl(const VarDecl &D, llvm::GlobalValue::LinkageTypes Linkage) { llvm::Value *&DMEntry = LocalDeclMap[&D]; - assert(DMEntry == 0 && "Decl already exists in localdeclmap!"); + assert(!DMEntry && "Decl already exists in localdeclmap!"); // Check to see if we already have a global variable for this // declaration. This can happen when double-emitting function @@ -298,12 +301,8 @@ void CodeGenFunction::EmitStaticVarDecl(const VarDecl &D, llvm::Constant *addr = CGM.getStaticLocalDeclAddress(&D); - llvm::GlobalVariable *var; - if (addr) { - var = cast<llvm::GlobalVariable>(addr->stripPointerCasts()); - } else { - addr = var = CreateStaticVarDecl(D, ".", Linkage); - } + if (!addr) + addr = CreateStaticVarDecl(D, ".", Linkage); // Store into LocalDeclMap before generating initializer to handle // circular references. @@ -319,6 +318,8 @@ void CodeGenFunction::EmitStaticVarDecl(const VarDecl &D, // Save the type in case adding the initializer forces a type change. llvm::Type *expectedType = addr->getType(); + llvm::GlobalVariable *var = + cast<llvm::GlobalVariable>(addr->stripPointerCasts()); // If this value has an initializer, emit it. if (D.getInit()) var = AddInitializerToStaticVarDecl(D, var); @@ -332,17 +333,20 @@ void CodeGenFunction::EmitStaticVarDecl(const VarDecl &D, var->setSection(SA->getName()); if (D.hasAttr<UsedAttr>()) - CGM.AddUsedGlobal(var); + CGM.addUsedGlobal(var); // We may have to cast the constant because of the initializer // mismatch above. // // FIXME: It is really dangerous to store this in the map; if anyone // RAUW's the GV uses of this constant will be invalid. - llvm::Constant *castedAddr = llvm::ConstantExpr::getBitCast(var, expectedType); + llvm::Constant *castedAddr = + llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(var, expectedType); DMEntry = castedAddr; CGM.setStaticLocalDeclAddress(&D, castedAddr); + CGM.reportGlobalToASan(var, D); + // Emit global variable debug descriptor for static vars. CGDebugInfo *DI = getDebugInfo(); if (DI && @@ -365,7 +369,7 @@ namespace { CodeGenFunction::Destroyer *destroyer; bool useEHCleanupForArray; - void Emit(CodeGenFunction &CGF, Flags flags) { + void Emit(CodeGenFunction &CGF, Flags flags) override { // Don't use an EH cleanup recursively from an EH cleanup. bool useEHCleanupForArray = flags.isForNormalCleanup() && this->useEHCleanupForArray; @@ -384,11 +388,11 @@ namespace { llvm::Value *NRVOFlag; llvm::Value *Loc; - void Emit(CodeGenFunction &CGF, Flags flags) { + void Emit(CodeGenFunction &CGF, Flags flags) override { // Along the exceptions path we always execute the dtor. bool NRVO = flags.isForNormalCleanup() && NRVOFlag; - llvm::BasicBlock *SkipDtorBB = 0; + llvm::BasicBlock *SkipDtorBB = nullptr; if (NRVO) { // If we exited via NRVO, we skip the destructor call. llvm::BasicBlock *RunDtorBB = CGF.createBasicBlock("nrvo.unused"); @@ -410,7 +414,7 @@ namespace { struct CallStackRestore : EHScopeStack::Cleanup { llvm::Value *Stack; CallStackRestore(llvm::Value *Stack) : Stack(Stack) {} - void Emit(CodeGenFunction &CGF, Flags flags) { + void Emit(CodeGenFunction &CGF, Flags flags) override { llvm::Value *V = CGF.Builder.CreateLoad(Stack); llvm::Value *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stackrestore); CGF.Builder.CreateCall(F, V); @@ -421,7 +425,7 @@ namespace { const VarDecl &Var; ExtendGCLifetime(const VarDecl *var) : Var(*var) {} - void Emit(CodeGenFunction &CGF, Flags flags) { + void Emit(CodeGenFunction &CGF, Flags flags) override { // Compute the address of the local variable, in case it's a // byref or something. DeclRefExpr DRE(const_cast<VarDecl*>(&Var), false, @@ -441,7 +445,7 @@ namespace { const VarDecl *Var) : CleanupFn(CleanupFn), FnInfo(*Info), Var(*Var) {} - void Emit(CodeGenFunction &CGF, Flags flags) { + void Emit(CodeGenFunction &CGF, Flags flags) override { DeclRefExpr DRE(const_cast<VarDecl*>(&Var), false, Var.getType(), VK_LValue, SourceLocation()); // Compute the address of the local variable, in case it's a byref @@ -473,7 +477,7 @@ namespace { CallLifetimeEnd(llvm::Value *addr, llvm::Value *size) : Addr(addr), Size(size) {} - void Emit(CodeGenFunction &CGF, Flags flags) { + void Emit(CodeGenFunction &CGF, Flags flags) override { llvm::Value *castAddr = CGF.Builder.CreateBitCast(Addr, CGF.Int8PtrTy); CGF.Builder.CreateCall2(CGF.CGM.getLLVMLifetimeEndFn(), Size, castAddr) @@ -530,9 +534,8 @@ static bool isAccessedBy(const VarDecl &var, const Stmt *s) { return (ref->getDecl() == &var); if (const BlockExpr *be = dyn_cast<BlockExpr>(e)) { const BlockDecl *block = be->getBlockDecl(); - for (BlockDecl::capture_const_iterator i = block->capture_begin(), - e = block->capture_end(); i != e; ++i) { - if (i->getVariable() == &var) + for (const auto &I : block->captures()) { + if (I.getVariable() == &var) return true; } } @@ -571,7 +574,10 @@ void CodeGenFunction::EmitScalarInit(const Expr *init, EmitStoreThroughLValue(RValue::get(value), lvalue, true); return; } - + + if (const CXXDefaultInitExpr *DIE = dyn_cast<CXXDefaultInitExpr>(init)) + init = DIE->getExpr(); + // If we're emitting a value with lifetime, we have to do the // initialization *before* we leave the cleanup scopes. if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(init)) { @@ -613,7 +619,7 @@ void CodeGenFunction::EmitScalarInit(const Expr *init, } // Emit the initializer. - llvm::Value *value = 0; + llvm::Value *value = nullptr; switch (lifetime) { case Qualifiers::OCL_None: @@ -798,9 +804,6 @@ static bool shouldUseMemSetPlusStoresToInitialize(llvm::Constant *Init, /// Should we use the LLVM lifetime intrinsics for the given local variable? static bool shouldUseLifetimeMarkers(CodeGenFunction &CGF, const VarDecl &D, unsigned Size) { - // Always emit lifetime markers in -fsanitize=use-after-scope mode. - if (CGF.getLangOpts().Sanitize.UseAfterScope) - return true; // For now, only in optimized builds. if (CGF.CGM.getCodeGenOpts().OptimizationLevel == 0) return false; @@ -823,7 +826,7 @@ void CodeGenFunction::EmitAutoVarDecl(const VarDecl &D) { } /// EmitAutoVarAlloca - Emit the alloca and debug information for a -/// local variable. Does not emit initalization or destruction. +/// local variable. Does not emit initialization or destruction. CodeGenFunction::AutoVarEmission CodeGenFunction::EmitAutoVarAlloca(const VarDecl &D) { QualType Ty = D.getType(); @@ -866,7 +869,7 @@ CodeGenFunction::EmitAutoVarAlloca(const VarDecl &D) { CGM.isTypeConstant(Ty, true)) { EmitStaticVarDecl(D, llvm::GlobalValue::InternalLinkage); - emission.Address = 0; // signal this condition to later callbacks + emission.Address = nullptr; // signal this condition to later callbacks assert(emission.wasEmittedAsGlobal()); return emission; } @@ -944,12 +947,12 @@ CodeGenFunction::EmitAutoVarAlloca(const VarDecl &D) { // Push a cleanup block and restore the stack there. // FIXME: in general circumstances, this should be an EH cleanup. - EHStack.pushCleanup<CallStackRestore>(NormalCleanup, Stack); + pushStackRestore(NormalCleanup, Stack); } llvm::Value *elementCount; QualType elementType; - llvm::tie(elementCount, elementType) = getVLASize(Ty); + std::tie(elementCount, elementType) = getVLASize(Ty); llvm::Type *llvmTy = ConvertTypeForMem(elementType); @@ -961,7 +964,7 @@ CodeGenFunction::EmitAutoVarAlloca(const VarDecl &D) { } llvm::Value *&DMEntry = LocalDeclMap[&D]; - assert(DMEntry == 0 && "Decl already exists in localdeclmap!"); + assert(!DMEntry && "Decl already exists in localdeclmap!"); DMEntry = DeclPtr; emission.Address = DeclPtr; @@ -990,9 +993,8 @@ static bool isCapturedBy(const VarDecl &var, const Expr *e) { if (const BlockExpr *be = dyn_cast<BlockExpr>(e)) { const BlockDecl *block = be->getBlockDecl(); - for (BlockDecl::capture_const_iterator i = block->capture_begin(), - e = block->capture_end(); i != e; ++i) { - if (i->getVariable() == &var) + for (const auto &I : block->captures()) { + if (I.getVariable() == &var) return true; } @@ -1002,18 +1004,16 @@ static bool isCapturedBy(const VarDecl &var, const Expr *e) { if (const StmtExpr *SE = dyn_cast<StmtExpr>(e)) { const CompoundStmt *CS = SE->getSubStmt(); - for (CompoundStmt::const_body_iterator BI = CS->body_begin(), - BE = CS->body_end(); BI != BE; ++BI) - if (Expr *E = dyn_cast<Expr>((*BI))) { + for (const auto *BI : CS->body()) + if (const auto *E = dyn_cast<Expr>(BI)) { if (isCapturedBy(var, E)) return true; } - else if (DeclStmt *DS = dyn_cast<DeclStmt>((*BI))) { + else if (const auto *DS = dyn_cast<DeclStmt>(BI)) { // special case declarations - for (DeclStmt::decl_iterator I = DS->decl_begin(), E = DS->decl_end(); - I != E; ++I) { - if (VarDecl *VD = dyn_cast<VarDecl>((*I))) { - Expr *Init = VD->getInit(); + for (const auto *I : DS->decls()) { + if (const auto *VD = dyn_cast<VarDecl>((I))) { + const Expr *Init = VD->getInit(); if (Init && isCapturedBy(var, Init)) return true; } @@ -1084,7 +1084,7 @@ void CodeGenFunction::EmitAutoVarInit(const AutoVarEmission &emission) { llvm::Value *Loc = capturedByInit ? emission.Address : emission.getObjectAddress(*this); - llvm::Constant *constant = 0; + llvm::Constant *constant = nullptr; if (emission.IsConstantAggregate || D.isConstexpr()) { assert(!capturedByInit && "constant init contains a capturing block?"); constant = CGM.EmitConstantInit(D, this); @@ -1211,7 +1211,7 @@ void CodeGenFunction::emitAutoVarTypeCleanup( QualType type = var->getType(); CleanupKind cleanupKind = NormalAndEHCleanup; - CodeGenFunction::Destroyer *destroyer = 0; + CodeGenFunction::Destroyer *destroyer = nullptr; switch (dtorKind) { case QualType::DK_none: @@ -1344,6 +1344,10 @@ void CodeGenFunction::pushDestroy(CleanupKind cleanupKind, llvm::Value *addr, destroyer, useEHCleanupForArray); } +void CodeGenFunction::pushStackRestore(CleanupKind Kind, llvm::Value *SPMem) { + EHStack.pushCleanup<CallStackRestore>(Kind, SPMem); +} + void CodeGenFunction::pushLifetimeExtendedDestroy( CleanupKind cleanupKind, llvm::Value *addr, QualType type, Destroyer *destroyer, bool useEHCleanupForArray) { @@ -1505,7 +1509,7 @@ namespace { : ArrayBegin(arrayBegin), ArrayEnd(arrayEnd), ElementType(elementType), Destroyer(destroyer) {} - void Emit(CodeGenFunction &CGF, Flags flags) { + void Emit(CodeGenFunction &CGF, Flags flags) override { emitPartialArrayDestroy(CGF, ArrayBegin, ArrayEnd, ElementType, Destroyer); } @@ -1527,7 +1531,7 @@ namespace { : ArrayBegin(arrayBegin), ArrayEndPointer(arrayEndPointer), ElementType(elementType), Destroyer(destroyer) {} - void Emit(CodeGenFunction &CGF, Flags flags) { + void Emit(CodeGenFunction &CGF, Flags flags) override { llvm::Value *arrayEnd = CGF.Builder.CreateLoad(ArrayEndPointer); emitPartialArrayDestroy(CGF, ArrayBegin, arrayEnd, ElementType, Destroyer); @@ -1594,7 +1598,7 @@ namespace { llvm::Value *Param; ARCPreciseLifetime_t Precise; - void Emit(CodeGenFunction &CGF, Flags flags) { + void Emit(CodeGenFunction &CGF, Flags flags) override { CGF.EmitARCRelease(Param, Precise); } }; @@ -1603,7 +1607,7 @@ namespace { /// Emit an alloca (or GlobalValue depending on target) /// for the specified parameter and set up LocalDeclMap. void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg, - unsigned ArgNo) { + bool ArgIsPointer, unsigned ArgNo) { // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl? assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) && "Invalid argument to EmitParmDecl"); @@ -1617,7 +1621,7 @@ void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg, // The only implicit argument a block has is its literal. if (BlockInfo) { LocalDeclMap[&D] = Arg; - llvm::Value *LocalAddr = 0; + llvm::Value *LocalAddr = nullptr; if (CGM.getCodeGenOpts().OptimizationLevel == 0) { // Allocate a stack slot to let the debug info survive the RA. llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertTypeForMem(Ty), @@ -1641,30 +1645,35 @@ void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg, } llvm::Value *DeclPtr; - bool HasNonScalarEvalKind = !CodeGenFunction::hasScalarEvaluationKind(Ty); - // If this is an aggregate or variable sized value, reuse the input pointer. - if (HasNonScalarEvalKind || !Ty->isConstantSizeType()) { - DeclPtr = Arg; + bool DoStore = false; + bool IsScalar = hasScalarEvaluationKind(Ty); + CharUnits Align = getContext().getDeclAlign(&D); + // If we already have a pointer to the argument, reuse the input pointer. + if (ArgIsPointer) { + // If we have a prettier pointer type at this point, bitcast to that. + unsigned AS = cast<llvm::PointerType>(Arg->getType())->getAddressSpace(); + llvm::Type *IRTy = ConvertTypeForMem(Ty)->getPointerTo(AS); + DeclPtr = Arg->getType() == IRTy ? Arg : Builder.CreateBitCast(Arg, IRTy, + D.getName()); // Push a destructor cleanup for this parameter if the ABI requires it. - if (HasNonScalarEvalKind && - getTarget().getCXXABI().isArgumentDestroyedByCallee()) { - if (const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl()) { - if (RD->hasNonTrivialDestructor()) - pushDestroy(QualType::DK_cxx_destructor, DeclPtr, Ty); - } + if (!IsScalar && + getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()) { + const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl(); + if (RD && RD->hasNonTrivialDestructor()) + pushDestroy(QualType::DK_cxx_destructor, DeclPtr, Ty); } } else { // Otherwise, create a temporary to hold the value. llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertTypeForMem(Ty), D.getName() + ".addr"); - CharUnits Align = getContext().getDeclAlign(&D); Alloc->setAlignment(Align.getQuantity()); DeclPtr = Alloc; + DoStore = true; + } - bool doStore = true; - + LValue lv = MakeAddrLValue(DeclPtr, Ty, Align); + if (IsScalar) { Qualifiers qs = Ty.getQualifiers(); - LValue lv = MakeAddrLValue(DeclPtr, Ty, Align); if (Qualifiers::ObjCLifetime lt = qs.getObjCLifetime()) { // We honor __attribute__((ns_consumed)) for types with lifetime. // For __strong, it's handled by just skipping the initial retain; @@ -1693,7 +1702,7 @@ void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg, llvm::Value *Null = CGM.EmitNullConstant(D.getType()); EmitStoreOfScalar(Null, lv, /* isInitialization */ true); EmitARCStoreStrongCall(lv.getAddress(), Arg, true); - doStore = false; + DoStore = false; } else // Don't use objc_retainBlock for block pointers, because we @@ -1712,21 +1721,21 @@ void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg, if (lt == Qualifiers::OCL_Weak) { EmitARCInitWeak(DeclPtr, Arg); - doStore = false; // The weak init is a store, no need to do two. + DoStore = false; // The weak init is a store, no need to do two. } } // Enter the cleanup scope. EmitAutoVarWithLifetime(*this, D, DeclPtr, lt); } - - // Store the initial value into the alloca. - if (doStore) - EmitStoreOfScalar(Arg, lv, /* isInitialization */ true); } + // Store the initial value into the alloca. + if (DoStore) + EmitStoreOfScalar(Arg, lv, /* isInitialization */ true); + llvm::Value *&DMEntry = LocalDeclMap[&D]; - assert(DMEntry == 0 && "Decl already exists in localdeclmap!"); + assert(!DMEntry && "Decl already exists in localdeclmap!"); DMEntry = DeclPtr; // Emit debug info for param declaration. |