From: Anders Carlsson Date: Wed, 24 Mar 2010 00:57:54 +0000 (+0000) Subject: Remove old thunks code. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f532f3b4a642ad5d9704c4a1d17b61cc52ab2f65;p=clang Remove old thunks code. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@99374 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/CodeGen/CGCXX.cpp b/lib/CodeGen/CGCXX.cpp index d007827ee9..7fb5ad4852 100644 --- a/lib/CodeGen/CGCXX.cpp +++ b/lib/CodeGen/CGCXX.cpp @@ -297,165 +297,6 @@ void CodeGenModule::getMangledCXXDtorName(MangleBuffer &Name, getMangleContext().mangleCXXDtor(D, Type, Name.getBuffer()); } -llvm::Constant * -CodeGenFunction::GenerateThunk(llvm::Function *Fn, GlobalDecl GD, - bool Extern, - const ThunkAdjustment &ThisAdjustment) { - return GenerateCovariantThunk(Fn, GD, Extern, - CovariantThunkAdjustment(ThisAdjustment, - ThunkAdjustment())); -} - -llvm::Value * -CodeGenFunction::DynamicTypeAdjust(llvm::Value *V, - const ThunkAdjustment &Adjustment) { - const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext); - - const llvm::Type *OrigTy = V->getType(); - if (Adjustment.NonVirtual) { - // Do the non-virtual adjustment - V = Builder.CreateBitCast(V, Int8PtrTy); - V = Builder.CreateConstInBoundsGEP1_64(V, Adjustment.NonVirtual); - V = Builder.CreateBitCast(V, OrigTy); - } - - if (!Adjustment.Virtual) - return V; - - assert(Adjustment.Virtual % (LLVMPointerWidth / 8) == 0 && - "vtable entry unaligned"); - - // Do the virtual this adjustment - const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType()); - const llvm::Type *PtrDiffPtrTy = PtrDiffTy->getPointerTo(); - - llvm::Value *ThisVal = Builder.CreateBitCast(V, Int8PtrTy); - V = Builder.CreateBitCast(V, PtrDiffPtrTy->getPointerTo()); - V = Builder.CreateLoad(V, "vtable"); - - llvm::Value *VTablePtr = V; - uint64_t VirtualAdjustment = Adjustment.Virtual / (LLVMPointerWidth / 8); - V = Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment); - V = Builder.CreateLoad(V); - V = Builder.CreateGEP(ThisVal, V); - - return Builder.CreateBitCast(V, OrigTy); -} - -llvm::Constant * -CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn, - GlobalDecl GD, bool Extern, - const CovariantThunkAdjustment &Adjustment) { - const CXXMethodDecl *MD = cast(GD.getDecl()); - const FunctionProtoType *FPT = MD->getType()->getAs(); - QualType ResultType = FPT->getResultType(); - - FunctionArgList Args; - ImplicitParamDecl *ThisDecl = - ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, - MD->getThisType(getContext())); - Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType())); - for (FunctionDecl::param_const_iterator i = MD->param_begin(), - e = MD->param_end(); - i != e; ++i) { - ParmVarDecl *D = *i; - Args.push_back(std::make_pair(D, D->getType())); - } - IdentifierInfo *II - = &CGM.getContext().Idents.get("__thunk_named_foo_"); - FunctionDecl *FD = FunctionDecl::Create(getContext(), - getContext().getTranslationUnitDecl(), - SourceLocation(), II, ResultType, 0, - Extern - ? FunctionDecl::Extern - : FunctionDecl::Static, - false, true); - StartFunction(FD, ResultType, Fn, Args, SourceLocation()); - - // generate body - const llvm::Type *Ty = - CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), - FPT->isVariadic()); - llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty); - - CallArgList CallArgs; - - bool ShouldAdjustReturnPointer = true; - QualType ArgType = MD->getThisType(getContext()); - llvm::Value *Arg = Builder.CreateLoad(LocalDeclMap[ThisDecl], "this"); - if (!Adjustment.ThisAdjustment.isEmpty()) { - // Do the this adjustment. - const llvm::Type *OrigTy = Callee->getType(); - Arg = DynamicTypeAdjust(Arg, Adjustment.ThisAdjustment); - - if (!Adjustment.ReturnAdjustment.isEmpty()) { - const CovariantThunkAdjustment &ReturnAdjustment = - CovariantThunkAdjustment(ThunkAdjustment(), - Adjustment.ReturnAdjustment); - - Callee = CGM.BuildCovariantThunk(GD, Extern, ReturnAdjustment); - - Callee = Builder.CreateBitCast(Callee, OrigTy); - ShouldAdjustReturnPointer = false; - } - } - - CallArgs.push_back(std::make_pair(RValue::get(Arg), ArgType)); - - for (FunctionDecl::param_const_iterator i = MD->param_begin(), - e = MD->param_end(); - i != e; ++i) { - ParmVarDecl *D = *i; - QualType ArgType = D->getType(); - - // llvm::Value *Arg = CGF.GetAddrOfLocalVar(Dst); - Expr *Arg = new (getContext()) DeclRefExpr(D, ArgType.getNonReferenceType(), - SourceLocation()); - CallArgs.push_back(std::make_pair(EmitCallArg(Arg, ArgType), ArgType)); - } - - RValue RV = EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs, - FPT->getCallConv(), - FPT->getNoReturnAttr()), - Callee, ReturnValueSlot(), CallArgs, MD); - if (ShouldAdjustReturnPointer && !Adjustment.ReturnAdjustment.isEmpty()) { - bool CanBeZero = !(ResultType->isReferenceType() - // FIXME: attr nonnull can't be zero either - /* || ResultType->hasAttr() */ ); - // Do the return result adjustment. - if (CanBeZero) { - llvm::BasicBlock *NonZeroBlock = createBasicBlock(); - llvm::BasicBlock *ZeroBlock = createBasicBlock(); - llvm::BasicBlock *ContBlock = createBasicBlock(); - - const llvm::Type *Ty = RV.getScalarVal()->getType(); - llvm::Value *Zero = llvm::Constant::getNullValue(Ty); - Builder.CreateCondBr(Builder.CreateICmpNE(RV.getScalarVal(), Zero), - NonZeroBlock, ZeroBlock); - EmitBlock(NonZeroBlock); - llvm::Value *NZ = - DynamicTypeAdjust(RV.getScalarVal(), Adjustment.ReturnAdjustment); - EmitBranch(ContBlock); - EmitBlock(ZeroBlock); - llvm::Value *Z = RV.getScalarVal(); - EmitBlock(ContBlock); - llvm::PHINode *RVOrZero = Builder.CreatePHI(Ty); - RVOrZero->reserveOperandSpace(2); - RVOrZero->addIncoming(NZ, NonZeroBlock); - RVOrZero->addIncoming(Z, ZeroBlock); - RV = RValue::get(RVOrZero); - } else - RV = RValue::get(DynamicTypeAdjust(RV.getScalarVal(), - Adjustment.ReturnAdjustment)); - } - - if (!ResultType->isVoidType()) - EmitReturnOfRValue(RV, ResultType); - - FinishFunction(); - return Fn; -} - llvm::Constant * CodeGenModule::GetAddrOfThunk(GlobalDecl GD, const ThunkAdjustment &ThisAdjustment) { @@ -488,127 +329,6 @@ CodeGenModule::GetAddrOfCovariantThunk(GlobalDecl GD, return GetOrCreateLLVMFunction(Name, Ty, GlobalDecl()); } -void CodeGenModule::BuildThunksForVirtual(GlobalDecl GD) { - const CXXMethodDecl *MD = cast(GD.getDecl()); - - if (const CXXDestructorDecl *DD = dyn_cast(MD)) - GD = GlobalDecl(cast(DD->getCanonicalDecl()), - GD.getDtorType()); - else - GD = MD->getCanonicalDecl(); - - CodeGenVTables::AdjustmentVectorTy *AdjPtr = getVTables().getAdjustments(GD); - if (!AdjPtr) - return; - CodeGenVTables::AdjustmentVectorTy &Adj = *AdjPtr; - for (unsigned i = 0; i < Adj.size(); i++) { - GlobalDecl OGD = Adj[i].first; - const CXXMethodDecl *OMD = cast(OGD.getDecl()); - QualType nc_oret = OMD->getType()->getAs()->getResultType(); - CanQualType oret = getContext().getCanonicalType(nc_oret); - QualType nc_ret = MD->getType()->getAs()->getResultType(); - CanQualType ret = getContext().getCanonicalType(nc_ret); - ThunkAdjustment ReturnAdjustment; - if (oret != ret) { - QualType qD = nc_ret->getPointeeType(); - QualType qB = nc_oret->getPointeeType(); - CXXRecordDecl *D = cast(qD->getAs()->getDecl()); - CXXRecordDecl *B = cast(qB->getAs()->getDecl()); - ReturnAdjustment = ComputeThunkAdjustment(D, B); - } - ThunkAdjustment ThisAdjustment = Adj[i].second; - bool Extern = !cast(OMD->getDeclContext())->isInAnonymousNamespace(); - if (!ReturnAdjustment.isEmpty() || !ThisAdjustment.isEmpty()) { - CovariantThunkAdjustment CoAdj(ThisAdjustment, ReturnAdjustment); - llvm::Constant *FnConst; - if (!ReturnAdjustment.isEmpty()) - FnConst = GetAddrOfCovariantThunk(GD, CoAdj); - else - FnConst = GetAddrOfThunk(GD, ThisAdjustment); - if (!isa(FnConst)) { - llvm::Constant *SubExpr = - cast(FnConst)->getOperand(0); - llvm::Function *OldFn = cast(SubExpr); - llvm::Constant *NewFnConst; - if (!ReturnAdjustment.isEmpty()) - NewFnConst = GetAddrOfCovariantThunk(GD, CoAdj); - else - NewFnConst = GetAddrOfThunk(GD, ThisAdjustment); - llvm::Function *NewFn = cast(NewFnConst); - NewFn->takeName(OldFn); - llvm::Constant *NewPtrForOldDecl = - llvm::ConstantExpr::getBitCast(NewFn, OldFn->getType()); - OldFn->replaceAllUsesWith(NewPtrForOldDecl); - OldFn->eraseFromParent(); - FnConst = NewFn; - } - llvm::Function *Fn = cast(FnConst); - if (Fn->isDeclaration()) { - llvm::GlobalVariable::LinkageTypes linktype; - linktype = llvm::GlobalValue::WeakAnyLinkage; - if (!Extern) - linktype = llvm::GlobalValue::InternalLinkage; - Fn->setLinkage(linktype); - if (!Features.Exceptions && !Features.ObjCNonFragileABI) - Fn->addFnAttr(llvm::Attribute::NoUnwind); - Fn->setAlignment(2); - CodeGenFunction(*this).GenerateCovariantThunk(Fn, GD, Extern, CoAdj); - } - } - } -} - -llvm::Constant * -CodeGenModule::BuildThunk(GlobalDecl GD, bool Extern, - const ThunkAdjustment &ThisAdjustment) { - const CXXMethodDecl *MD = cast(GD.getDecl()); - llvm::SmallString<256> OutName; - if (const CXXDestructorDecl *D = dyn_cast(MD)) { - getMangleContext().mangleCXXDtorThunk(D, GD.getDtorType(), ThisAdjustment, - OutName); - } else - getMangleContext().mangleThunk(MD, ThisAdjustment, OutName); - - llvm::GlobalVariable::LinkageTypes linktype; - linktype = llvm::GlobalValue::WeakAnyLinkage; - if (!Extern) - linktype = llvm::GlobalValue::InternalLinkage; - llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0); - const FunctionProtoType *FPT = MD->getType()->getAs(); - const llvm::FunctionType *FTy = - getTypes().GetFunctionType(getTypes().getFunctionInfo(MD), - FPT->isVariadic()); - - llvm::Function *Fn = llvm::Function::Create(FTy, linktype, OutName.str(), - &getModule()); - CodeGenFunction(*this).GenerateThunk(Fn, GD, Extern, ThisAdjustment); - llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty); - return m; -} - -llvm::Constant * -CodeGenModule::BuildCovariantThunk(const GlobalDecl &GD, bool Extern, - const CovariantThunkAdjustment &Adjustment) { - const CXXMethodDecl *MD = cast(GD.getDecl()); - llvm::SmallString<256> OutName; - getMangleContext().mangleCovariantThunk(MD, Adjustment, OutName); - llvm::GlobalVariable::LinkageTypes linktype; - linktype = llvm::GlobalValue::WeakAnyLinkage; - if (!Extern) - linktype = llvm::GlobalValue::InternalLinkage; - llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0); - const FunctionProtoType *FPT = MD->getType()->getAs(); - const llvm::FunctionType *FTy = - getTypes().GetFunctionType(getTypes().getFunctionInfo(MD), - FPT->isVariadic()); - - llvm::Function *Fn = llvm::Function::Create(FTy, linktype, OutName.str(), - &getModule()); - CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, Adjustment); - llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty); - return m; -} - static llvm::Value *BuildVirtualCall(CodeGenFunction &CGF, uint64_t VtableIndex, llvm::Value *This, const llvm::Type *Ty) { Ty = Ty->getPointerTo()->getPointerTo()->getPointerTo(); diff --git a/lib/CodeGen/CGVtable.cpp b/lib/CodeGen/CGVtable.cpp index 47509616c5..f4921699be 100644 --- a/lib/CodeGen/CGVtable.cpp +++ b/lib/CodeGen/CGVtable.cpp @@ -3515,34 +3515,6 @@ uint64_t CodeGenVTables::getMethodVtableIndex(GlobalDecl GD) { return I->second; } -CodeGenVTables::AdjustmentVectorTy* -CodeGenVTables::getAdjustments(GlobalDecl GD) { - SavedAdjustmentsTy::iterator I = SavedAdjustments.find(GD); - if (I != SavedAdjustments.end()) - return &I->second; - - const CXXRecordDecl *RD = cast(GD.getDecl()->getDeclContext()); - if (!SavedAdjustmentRecords.insert(RD).second) - return 0; - - AddressPointsMapTy AddressPoints; - OldVtableBuilder b(RD, RD, 0, CGM, false, AddressPoints); - D1(printf("vtable %s\n", RD->getNameAsCString())); - b.GenerateVtableForBase(RD); - b.GenerateVtableForVBases(RD); - - for (OldVtableBuilder::SavedAdjustmentsVectorTy::iterator - i = b.getSavedAdjustments().begin(), - e = b.getSavedAdjustments().end(); i != e; i++) - SavedAdjustments[i->first].push_back(i->second); - - I = SavedAdjustments.find(GD); - if (I != SavedAdjustments.end()) - return &I->second; - - return 0; -} - int64_t CodeGenVTables::getVirtualBaseOffsetOffset(const CXXRecordDecl *RD, const CXXRecordDecl *VBase) { ClassPairTy ClassPair(RD, VBase); diff --git a/lib/CodeGen/CGVtable.h b/lib/CodeGen/CGVtable.h index cc4cb87f7f..ebccb514e9 100644 --- a/lib/CodeGen/CGVtable.h +++ b/lib/CodeGen/CGVtable.h @@ -251,10 +251,6 @@ private: /// pointers in the vtable for a given record decl. llvm::DenseMap NumVirtualFunctionPointers; - typedef llvm::DenseMap SavedAdjustmentsTy; - SavedAdjustmentsTy SavedAdjustments; - llvm::DenseSet SavedAdjustmentRecords; - typedef llvm::SmallVector ThunkInfoVectorTy; typedef llvm::DenseMap ThunksMapTy; @@ -316,8 +312,6 @@ public: int64_t getVirtualBaseOffsetOffset(const CXXRecordDecl *RD, const CXXRecordDecl *VBase); - AdjustmentVectorTy *getAdjustments(GlobalDecl GD); - llvm::GlobalVariable *getVtable(const CXXRecordDecl *RD); /// CtorVtableInfo - Information about a constructor vtable. diff --git a/lib/CodeGen/CodeGenFunction.h b/lib/CodeGen/CodeGenFunction.h index 8b01623f30..cc5104516d 100644 --- a/lib/CodeGen/CodeGenFunction.h +++ b/lib/CodeGen/CodeGenFunction.h @@ -504,23 +504,9 @@ public: /// legal to call this function even if there is no current insertion point. void FinishFunction(SourceLocation EndLoc=SourceLocation()); - /// DynamicTypeAdjust - Do the non-virtual and virtual adjustments on an - /// object pointer to alter the dynamic type of the pointer. Used by - /// GenerateCovariantThunk for building thunks. - llvm::Value *DynamicTypeAdjust(llvm::Value *V, - const ThunkAdjustment &Adjustment); - /// GenerateThunk - Generate a thunk for the given method. void GenerateThunk(llvm::Function *Fn, GlobalDecl GD, const ThunkInfo &Thunk); - llvm::Constant *GenerateThunk(llvm::Function *Fn, GlobalDecl GD, - bool Extern, - const ThunkAdjustment &ThisAdjustment); - llvm::Constant * - GenerateCovariantThunk(llvm::Function *Fn, GlobalDecl GD, - bool Extern, - const CovariantThunkAdjustment &Adjustment); - void EmitCtorPrologue(const CXXConstructorDecl *CD, CXXCtorType Type); void InitializeVtablePtrs(const CXXRecordDecl *ClassDecl); diff --git a/lib/CodeGen/CodeGenModule.h b/lib/CodeGen/CodeGenModule.h index a8170c17db..5946a6fd94 100644 --- a/lib/CodeGen/CodeGenModule.h +++ b/lib/CodeGen/CodeGenModule.h @@ -232,21 +232,10 @@ public: const ThunkAdjustment &ThisAdjustment); llvm::Constant *GetAddrOfCovariantThunk(GlobalDecl GD, const CovariantThunkAdjustment &ThisAdjustment); - void BuildThunksForVirtual(GlobalDecl GD); - void BuildThunksForVirtualRecursive(GlobalDecl GD, GlobalDecl BaseOGD); /// GetWeakRefReference - Get a reference to the target of VD. llvm::Constant *GetWeakRefReference(const ValueDecl *VD); - /// BuildThunk - Build a thunk for the given method. - llvm::Constant *BuildThunk(GlobalDecl GD, bool Extern, - const ThunkAdjustment &ThisAdjustment); - - /// BuildCoVariantThunk - Build a thunk for the given method - llvm::Constant * - BuildCovariantThunk(const GlobalDecl &GD, bool Extern, - const CovariantThunkAdjustment &Adjustment); - /// GetNonVirtualBaseClassOffset - Returns the offset from a derived class to /// its base class. Returns null if the offset is 0. llvm::Constant *