[llvm] r350219 - Extend Module::getOrInsertGlobal to control the construction of the
Philip Pfaffe via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 2 07:41:47 PST 2019
Author: pfaffe
Date: Wed Jan 2 07:41:47 2019
New Revision: 350219
URL: http://llvm.org/viewvc/llvm-project?rev=350219&view=rev
Log:
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
Modified:
llvm/trunk/include/llvm/IR/Module.h
llvm/trunk/lib/IR/Module.cpp
Modified: llvm/trunk/include/llvm/IR/Module.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/Module.h?rev=350219&r1=350218&r2=350219&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/Module.h (original)
+++ llvm/trunk/include/llvm/IR/Module.h Wed Jan 2 07:41:47 2019
@@ -403,11 +403,15 @@ public:
}
/// Look up the specified global in the module symbol table.
- /// 1. If it does not exist, add a declaration of the global and return it.
- /// 2. Else, the global exists but has the wrong type: return the function
- /// with a constantexpr cast to the right type.
- /// 3. Finally, if the existing global is the correct declaration, return
- /// the existing global.
+ /// If it does not exist, invoke a callback to create a declaration of the
+ /// global and return it. The global is constantexpr casted to the expected
+ /// type if necessary.
+ Constant *
+ getOrInsertGlobal(StringRef Name, Type *Ty,
+ function_ref<GlobalVariable *()> CreateGlobalCallback);
+
+ /// Look up the specified global in the module symbol table. If required, this
+ /// overload constructs the global variable using its constructor's defaults.
Constant *getOrInsertGlobal(StringRef Name, Type *Ty);
/// @}
Modified: llvm/trunk/lib/IR/Module.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Module.cpp?rev=350219&r1=350218&r2=350219&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Module.cpp (original)
+++ llvm/trunk/lib/IR/Module.cpp Wed Jan 2 07:41:47 2019
@@ -203,16 +203,14 @@ GlobalVariable *Module::getGlobalVariabl
/// 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(Stri
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.
//
More information about the llvm-commits
mailing list