[cfe-commits] r99303 - in /cfe/trunk/lib/CodeGen: CGVtable.cpp CGVtable.h

Anders Carlsson andersca at mac.com
Tue Mar 23 11:18:41 PDT 2010


Author: andersca
Date: Tue Mar 23 13:18:41 2010
New Revision: 99303

URL: http://llvm.org/viewvc/llvm-project?rev=99303&view=rev
Log:
More work on thunks - don't assert if there's a variable with the same name as the thunk already.

Modified:
    cfe/trunk/lib/CodeGen/CGVtable.cpp
    cfe/trunk/lib/CodeGen/CGVtable.h

Modified: cfe/trunk/lib/CodeGen/CGVtable.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGVtable.cpp?rev=99303&r1=99302&r2=99303&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGVtable.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGVtable.cpp Tue Mar 23 13:18:41 2010
@@ -3669,8 +3669,40 @@
 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)

Modified: cfe/trunk/lib/CodeGen/CGVtable.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGVtable.h?rev=99303&r1=99302&r2=99303&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGVtable.h (original)
+++ cfe/trunk/lib/CodeGen/CGVtable.h Tue Mar 23 13:18:41 2010
@@ -341,7 +341,7 @@
   // have, as well as the vtable itself if the global decl is the key function.
   void EmitVTableRelatedData(GlobalDecl GD);
 
-  /// GenerateClassData - Generate all the class data requires to be generated
+  /// GenerateClassData - Generate all the class data required to be generated
   /// upon definition of a KeyFunction.  This includes the vtable, the
   /// rtti data structure and the VTT.
   ///





More information about the cfe-commits mailing list