diff options
author | Anders Carlsson <andersca@mac.com> | 2010-03-23 18:18:41 +0000 |
---|---|---|
committer | Anders Carlsson <andersca@mac.com> | 2010-03-23 18:18:41 +0000 |
commit | 7986ad5f8de58987a3f3f5ec27d6e765f9e14c9a (patch) | |
tree | 8fc5b0d0a943ef5e2329ac87b6e6f3d2c41710fc /lib/CodeGen/CGVtable.cpp | |
parent | dfb1eb208e401a0e5ecf2910e1a73b09aa69fd26 (diff) |
More work on thunks - don't assert if there's a variable with the same name as the thunk already.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@99303 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/CodeGen/CGVtable.cpp')
-rw-r--r-- | lib/CodeGen/CGVtable.cpp | 34 |
1 files changed, 33 insertions, 1 deletions
diff --git a/lib/CodeGen/CGVtable.cpp b/lib/CodeGen/CGVtable.cpp index 6fee2a566c..9204e4e565 100644 --- a/lib/CodeGen/CGVtable.cpp +++ b/lib/CodeGen/CGVtable.cpp @@ -3669,8 +3669,40 @@ llvm::Constant *CodeGenModule::GetAddrOfThunk(GlobalDecl GD, void CodeGenVTables::EmitThunk(GlobalDecl GD, const ThunkInfo &Thunk) { llvm::Constant *ThunkFn = CGM.GetAddrOfThunk(GD, Thunk); + const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); + + // Strip off a bitcast if we got one back. + if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(ThunkFn)) { + assert(CE->getOpcode() == llvm::Instruction::BitCast); + ThunkFn = CE->getOperand(0); + } - (void)ThunkFn; + const llvm::Type *ThunkFnTy = + cast<llvm::GlobalValue>(ThunkFn)->getType()->getElementType(); + + // There's already a declaration with the same name, check if it has the same + // type or if we need to replace it. + if (ThunkFnTy != CGM.getTypes().GetFunctionTypeForVtable(MD)) { + llvm::GlobalValue *OldThunkFn = cast<llvm::GlobalValue>(ThunkFn); + + // If the types mismatch then we have to rewrite the definition. + assert(OldThunkFn->isDeclaration() && + "Shouldn't replace non-declaration"); + + // Remove the name from the old thunk function and get a new thunk. + OldThunkFn->setName(llvm::StringRef()); + ThunkFn = CGM.GetAddrOfThunk(GD, Thunk); + + // If needed, replace the old thunk with a bitcast. + if (!OldThunkFn->use_empty()) { + llvm::Constant *NewPtrForOldDecl = + llvm::ConstantExpr::getBitCast(ThunkFn, OldThunkFn->getType()); + OldThunkFn->replaceAllUsesWith(NewPtrForOldDecl); + } + + // Remove the old thunk. + OldThunkFn->eraseFromParent(); + } } void CodeGenVTables::EmitThunks(GlobalDecl GD) |