diff options
author | Philip Pfaffe <philip.pfaffe@gmail.com> | 2019-01-02 15:41:47 +0000 |
---|---|---|
committer | Philip Pfaffe <philip.pfaffe@gmail.com> | 2019-01-02 15:41:47 +0000 |
commit | 6bc98ad7e879fb2c56bc998b57320db3405fd5af (patch) | |
tree | c5516520caf48e01c01f11b76c083dbd59133549 /llvm/lib/IR/Module.cpp | |
parent | 0682afbaeed4471483ed6069a080f0cfd922767b (diff) | |
download | llvm-6bc98ad7e879fb2c56bc998b57320db3405fd5af.zip llvm-6bc98ad7e879fb2c56bc998b57320db3405fd5af.tar.gz llvm-6bc98ad7e879fb2c56bc998b57320db3405fd5af.tar.bz2 |
Extend Module::getOrInsertGlobal to control the construction of the
GlobalVariable
Summary:
Extend Module::getOrInsertGlobal to accept a callback for creating a new
GlobalVariable if necessary instead of calling the GV constructor
directly using default arguments. Additionally overload
getOrInsertGlobal for the previous default behavior.
Reviewers: chandlerc
Subscribers: hiraditya, llvm-commits, bollu
Differential Revision: https://reviews.llvm.org/D56130
llvm-svn: 350219
Diffstat (limited to 'llvm/lib/IR/Module.cpp')
-rw-r--r-- | llvm/lib/IR/Module.cpp | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/llvm/lib/IR/Module.cpp b/llvm/lib/IR/Module.cpp index 70a16cb..93f2730 100644 --- a/llvm/lib/IR/Module.cpp +++ b/llvm/lib/IR/Module.cpp @@ -203,16 +203,14 @@ GlobalVariable *Module::getGlobalVariable(StringRef Name, /// with a constantexpr cast to the right type. /// 3. Finally, if the existing global is the correct declaration, return the /// existing global. -Constant *Module::getOrInsertGlobal(StringRef Name, Type *Ty) { +Constant *Module::getOrInsertGlobal( + StringRef Name, Type *Ty, + function_ref<GlobalVariable *()> CreateGlobalCallback) { // See if we have a definition for the specified global already. GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(getNamedValue(Name)); - if (!GV) { - // Nope, add it - GlobalVariable *New = - new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage, - nullptr, Name); - return New; // Return the new declaration. - } + if (!GV) + GV = CreateGlobalCallback(); + assert(GV && "The CreateGlobalCallback is expected to create a global"); // If the variable exists but has the wrong type, return a bitcast to the // right type. @@ -225,6 +223,14 @@ Constant *Module::getOrInsertGlobal(StringRef Name, Type *Ty) { return GV; } +// Overload to construct a global variable using its constructor's defaults. +Constant *Module::getOrInsertGlobal(StringRef Name, Type *Ty) { + return getOrInsertGlobal(Name, Ty, [&] { + return new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage, + nullptr, Name); + }); +} + //===----------------------------------------------------------------------===// // Methods for easy access to the global variables in the module. // |