[PATCH] D56130: Extend Module::getOrInsertGlobal to also take the default arguments accepted by the GlobalVariable constructor.
Philip Pfaffe via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 2 03:27:17 PST 2019
philip.pfaffe updated this revision to Diff 179830.
philip.pfaffe marked an inline comment as done.
philip.pfaffe added a comment.
Follow @chandlerc's proposal for a much more narrow API.
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D56130/new/
https://reviews.llvm.org/D56130
Files:
llvm/include/llvm/IR/Module.h
llvm/lib/IR/Module.cpp
Index: llvm/lib/IR/Module.cpp
===================================================================
--- llvm/lib/IR/Module.cpp
+++ llvm/lib/IR/Module.cpp
@@ -203,16 +203,13 @@
/// 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)
+ return CreateGlobalCallback();
// If the variable exists but has the wrong type, return a bitcast to the
// right type.
@@ -225,6 +222,14 @@
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.
//
Index: llvm/include/llvm/IR/Module.h
===================================================================
--- llvm/include/llvm/IR/Module.h
+++ llvm/include/llvm/IR/Module.h
@@ -403,16 +403,23 @@
}
/// 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.
+ /// 1. If it does not exist, invoke to callback to 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
+ /// 3. Finally, if the existing global has the correct type, return
/// the existing global.
+ 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);
-/// @}
-/// @name Global Alias Accessors
-/// @{
+ /// @}
+ /// @name Global Alias Accessors
+ /// @{
/// Return the global alias in the module with the specified name, of
/// arbitrary type. This method returns null if a global with the specified
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D56130.179830.patch
Type: text/x-patch
Size: 3001 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190102/c76001a4/attachment.bin>
More information about the llvm-commits
mailing list