diff options
author | Anders Carlsson <andersca@mac.com> | 2011-01-29 18:20:20 +0000 |
---|---|---|
committer | Anders Carlsson <andersca@mac.com> | 2011-01-29 18:20:20 +0000 |
commit | da80af3681feb019e8362f1e21012156dccdd874 (patch) | |
tree | 8ad6f30ea1818b2ecf9d9cfe4d553cf175dfbcfd /clang/lib/CodeGen/CodeGenModule.cpp | |
parent | a45a70cf735cc0639d8ea3063db92645a782c959 (diff) | |
download | llvm-da80af3681feb019e8362f1e21012156dccdd874.zip llvm-da80af3681feb019e8362f1e21012156dccdd874.tar.gz llvm-da80af3681feb019e8362f1e21012156dccdd874.tar.bz2 |
Add a new function, to be used by CGRTTI, CGVTables and CGVTT (which each has their own copy of this code).
llvm-svn: 124537
Diffstat (limited to 'clang/lib/CodeGen/CodeGenModule.cpp')
-rw-r--r-- | clang/lib/CodeGen/CodeGenModule.cpp | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 80d0f10..c261cca 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -985,6 +985,45 @@ CodeGenModule::GetOrCreateLLVMGlobal(llvm::StringRef MangledName, } +llvm::GlobalVariable * +CodeGenModule::CreateOrReplaceCXXRuntimeVariable(llvm::StringRef Name, + const llvm::Type *Ty, + llvm::GlobalValue::LinkageTypes Linkage) { + llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name); + llvm::GlobalVariable *OldGV = 0; + + + if (GV) { + // Check if the variable has the right type. + if (GV->getType()->getElementType() == Ty) + return GV; + + // Because C++ name mangling, the only way we can end up with an already + // existing global with the same name is if it has been declared extern "C". + assert(GV->isDeclaration() && "Declaration has wrong type!"); + OldGV = GV; + } + + // Create a new variable. + GV = new llvm::GlobalVariable(getModule(), Ty, /*isConstant=*/true, + Linkage, 0, Name); + + if (OldGV) { + // Replace occurrences of the old variable if needed. + GV->takeName(OldGV); + + if (!OldGV->use_empty()) { + llvm::Constant *NewPtrForOldDecl = + llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); + OldGV->replaceAllUsesWith(NewPtrForOldDecl); + } + + OldGV->eraseFromParent(); + } + + return GV; +} + /// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the /// given global variable. If Ty is non-null and if the global doesn't exist, /// then it will be greated with the specified type instead of whatever the |