aboutsummaryrefslogtreecommitdiffstats
path: root/lib/VMCore/Module.cpp
diff options
context:
space:
mode:
authorBill Wendling <isanbard@gmail.com>2008-11-04 22:51:24 +0000
committerBill Wendling <isanbard@gmail.com>2008-11-04 22:51:24 +0000
commit38d8b692b1e0f2bf02aa01b374d87fb89398599e (patch)
tree49a98464696bc8346153399ac199678be10758fe /lib/VMCore/Module.cpp
parent39e964c006a818b0e7066c9079c15b20e1ce7ec7 (diff)
downloadexternal_llvm-38d8b692b1e0f2bf02aa01b374d87fb89398599e.zip
external_llvm-38d8b692b1e0f2bf02aa01b374d87fb89398599e.tar.gz
external_llvm-38d8b692b1e0f2bf02aa01b374d87fb89398599e.tar.bz2
- Add a "getOrInsertGlobal" method to the Module class. This acts similarly to
"getOrInsertFunction" in that it either adds a new declaration of the global and returns it, or returns the current one -- optionally casting it to the correct type. - Use the new getOrInsertGlobal in the stack protector code. - Use "splitBasicBlock" in the stack protector code. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@58727 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/VMCore/Module.cpp')
-rw-r--r--lib/VMCore/Module.cpp22
1 files changed, 22 insertions, 0 deletions
diff --git a/lib/VMCore/Module.cpp b/lib/VMCore/Module.cpp
index b95f6e3..d4432c6 100644
--- a/lib/VMCore/Module.cpp
+++ b/lib/VMCore/Module.cpp
@@ -224,6 +224,28 @@ GlobalVariable *Module::getGlobalVariable(const std::string &Name,
return 0;
}
+Constant *Module::getOrInsertGlobal(const std::string &Name, const Type *Ty) {
+ ValueSymbolTable &SymTab = getValueSymbolTable();
+
+ // See if we have a definition for the specified global already.
+ GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(SymTab.lookup(Name));
+ if (GV == 0) {
+ // Nope, add it
+ GlobalVariable *New =
+ new GlobalVariable(Ty, false, GlobalVariable::ExternalLinkage, 0, Name);
+ GlobalList.push_back(New);
+ return New; // Return the new declaration.
+ }
+
+ // If the variable exists but has the wrong type, return a bitcast to the
+ // right type.
+ if (GV->getType() != PointerType::getUnqual(Ty))
+ return ConstantExpr::getBitCast(GV, PointerType::getUnqual(Ty));
+
+ // Otherwise, we just found the existing function or a prototype.
+ return GV;
+}
+
//===----------------------------------------------------------------------===//
// Methods for easy access to the global variables in the module.
//