GlobalVariable.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- llvm/GlobalVariable.h - GlobalVariable class ------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file contains the declaration of the GlobalVariable class, which
  15. // represents a single global variable (or constant) in the VM.
  16. //
  17. // Global variables are constant pointers that refer to hunks of space that are
  18. // allocated by either the VM, or by the linker in a static compiler. A global
  19. // variable may have an initial value, which is copied into the executables .data
  20. // area. Global Constants are required to have initializers.
  21. //
  22. //===----------------------------------------------------------------------===//
  23. #ifndef LLVM_IR_GLOBALVARIABLE_H
  24. #define LLVM_IR_GLOBALVARIABLE_H
  25. #include "llvm/ADT/Twine.h"
  26. #include "llvm/ADT/ilist_node.h"
  27. #include "llvm/IR/Attributes.h"
  28. #include "llvm/IR/GlobalObject.h"
  29. #include "llvm/IR/OperandTraits.h"
  30. #include "llvm/IR/Value.h"
  31. #include <cassert>
  32. #include <cstddef>
  33. namespace llvm {
  34. class Constant;
  35. class Module;
  36. template <typename ValueSubClass> class SymbolTableListTraits;
  37. class DIGlobalVariableExpression;
  38. class GlobalVariable : public GlobalObject, public ilist_node<GlobalVariable> {
  39. friend class SymbolTableListTraits<GlobalVariable>;
  40. AttributeSet Attrs;
  41. bool isConstantGlobal : 1; // Is this a global constant?
  42. bool isExternallyInitializedConstant : 1; // Is this a global whose value
  43. // can change from its initial
  44. // value before global
  45. // initializers are run?
  46. public:
  47. /// GlobalVariable ctor - If a parent module is specified, the global is
  48. /// automatically inserted into the end of the specified modules global list.
  49. GlobalVariable(Type *Ty, bool isConstant, LinkageTypes Linkage,
  50. Constant *Initializer = nullptr, const Twine &Name = "",
  51. ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0,
  52. bool isExternallyInitialized = false);
  53. /// GlobalVariable ctor - This creates a global and inserts it before the
  54. /// specified other global.
  55. GlobalVariable(Module &M, Type *Ty, bool isConstant, LinkageTypes Linkage,
  56. Constant *Initializer, const Twine &Name = "",
  57. GlobalVariable *InsertBefore = nullptr,
  58. ThreadLocalMode = NotThreadLocal,
  59. std::optional<unsigned> AddressSpace = std::nullopt,
  60. bool isExternallyInitialized = false);
  61. GlobalVariable(const GlobalVariable &) = delete;
  62. GlobalVariable &operator=(const GlobalVariable &) = delete;
  63. ~GlobalVariable() {
  64. dropAllReferences();
  65. }
  66. // allocate space for exactly one operand
  67. void *operator new(size_t s) {
  68. return User::operator new(s, 1);
  69. }
  70. // delete space for exactly one operand as created in the corresponding new operator
  71. void operator delete(void *ptr){
  72. assert(ptr != nullptr && "must not be nullptr");
  73. User *Obj = static_cast<User *>(ptr);
  74. // Number of operands can be set to 0 after construction and initialization. Make sure
  75. // that number of operands is reset to 1, as this is needed in User::operator delete
  76. Obj->setGlobalVariableNumOperands(1);
  77. User::operator delete(Obj);
  78. }
  79. /// Provide fast operand accessors
  80. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  81. /// Definitions have initializers, declarations don't.
  82. ///
  83. inline bool hasInitializer() const { return !isDeclaration(); }
  84. /// hasDefinitiveInitializer - Whether the global variable has an initializer,
  85. /// and any other instances of the global (this can happen due to weak
  86. /// linkage) are guaranteed to have the same initializer.
  87. ///
  88. /// Note that if you want to transform a global, you must use
  89. /// hasUniqueInitializer() instead, because of the *_odr linkage type.
  90. ///
  91. /// Example:
  92. ///
  93. /// @a = global SomeType* null - Initializer is both definitive and unique.
  94. ///
  95. /// @b = global weak SomeType* null - Initializer is neither definitive nor
  96. /// unique.
  97. ///
  98. /// @c = global weak_odr SomeType* null - Initializer is definitive, but not
  99. /// unique.
  100. inline bool hasDefinitiveInitializer() const {
  101. return hasInitializer() &&
  102. // The initializer of a global variable may change to something arbitrary
  103. // at link time.
  104. !isInterposable() &&
  105. // The initializer of a global variable with the externally_initialized
  106. // marker may change at runtime before C++ initializers are evaluated.
  107. !isExternallyInitialized();
  108. }
  109. /// hasUniqueInitializer - Whether the global variable has an initializer, and
  110. /// any changes made to the initializer will turn up in the final executable.
  111. inline bool hasUniqueInitializer() const {
  112. return
  113. // We need to be sure this is the definition that will actually be used
  114. isStrongDefinitionForLinker() &&
  115. // It is not safe to modify initializers of global variables with the
  116. // external_initializer marker since the value may be changed at runtime
  117. // before C++ initializers are evaluated.
  118. !isExternallyInitialized();
  119. }
  120. /// getInitializer - Return the initializer for this global variable. It is
  121. /// illegal to call this method if the global is external, because we cannot
  122. /// tell what the value is initialized to!
  123. ///
  124. inline const Constant *getInitializer() const {
  125. assert(hasInitializer() && "GV doesn't have initializer!");
  126. return static_cast<Constant*>(Op<0>().get());
  127. }
  128. inline Constant *getInitializer() {
  129. assert(hasInitializer() && "GV doesn't have initializer!");
  130. return static_cast<Constant*>(Op<0>().get());
  131. }
  132. /// setInitializer - Sets the initializer for this global variable, removing
  133. /// any existing initializer if InitVal==NULL. If this GV has type T*, the
  134. /// initializer must have type T.
  135. void setInitializer(Constant *InitVal);
  136. /// If the value is a global constant, its value is immutable throughout the
  137. /// runtime execution of the program. Assigning a value into the constant
  138. /// leads to undefined behavior.
  139. ///
  140. bool isConstant() const { return isConstantGlobal; }
  141. void setConstant(bool Val) { isConstantGlobal = Val; }
  142. bool isExternallyInitialized() const {
  143. return isExternallyInitializedConstant;
  144. }
  145. void setExternallyInitialized(bool Val) {
  146. isExternallyInitializedConstant = Val;
  147. }
  148. /// copyAttributesFrom - copy all additional attributes (those not needed to
  149. /// create a GlobalVariable) from the GlobalVariable Src to this one.
  150. void copyAttributesFrom(const GlobalVariable *Src);
  151. /// removeFromParent - This method unlinks 'this' from the containing module,
  152. /// but does not delete it.
  153. ///
  154. void removeFromParent();
  155. /// eraseFromParent - This method unlinks 'this' from the containing module
  156. /// and deletes it.
  157. ///
  158. void eraseFromParent();
  159. /// Drop all references in preparation to destroy the GlobalVariable. This
  160. /// drops not only the reference to the initializer but also to any metadata.
  161. void dropAllReferences();
  162. /// Attach a DIGlobalVariableExpression.
  163. void addDebugInfo(DIGlobalVariableExpression *GV);
  164. /// Fill the vector with all debug info attachements.
  165. void getDebugInfo(SmallVectorImpl<DIGlobalVariableExpression *> &GVs) const;
  166. /// Add attribute to this global.
  167. void addAttribute(Attribute::AttrKind Kind) {
  168. Attrs = Attrs.addAttribute(getContext(), Kind);
  169. }
  170. /// Add attribute to this global.
  171. void addAttribute(StringRef Kind, StringRef Val = StringRef()) {
  172. Attrs = Attrs.addAttribute(getContext(), Kind, Val);
  173. }
  174. /// Return true if the attribute exists.
  175. bool hasAttribute(Attribute::AttrKind Kind) const {
  176. return Attrs.hasAttribute(Kind);
  177. }
  178. /// Return true if the attribute exists.
  179. bool hasAttribute(StringRef Kind) const {
  180. return Attrs.hasAttribute(Kind);
  181. }
  182. /// Return true if any attributes exist.
  183. bool hasAttributes() const {
  184. return Attrs.hasAttributes();
  185. }
  186. /// Return the attribute object.
  187. Attribute getAttribute(Attribute::AttrKind Kind) const {
  188. return Attrs.getAttribute(Kind);
  189. }
  190. /// Return the attribute object.
  191. Attribute getAttribute(StringRef Kind) const {
  192. return Attrs.getAttribute(Kind);
  193. }
  194. /// Return the attribute set for this global
  195. AttributeSet getAttributes() const {
  196. return Attrs;
  197. }
  198. /// Return attribute set as list with index.
  199. /// FIXME: This may not be required once ValueEnumerators
  200. /// in bitcode-writer can enumerate attribute-set.
  201. AttributeList getAttributesAsList(unsigned index) const {
  202. if (!hasAttributes())
  203. return AttributeList();
  204. std::pair<unsigned, AttributeSet> AS[1] = {{index, Attrs}};
  205. return AttributeList::get(getContext(), AS);
  206. }
  207. /// Set attribute list for this global
  208. void setAttributes(AttributeSet A) {
  209. Attrs = A;
  210. }
  211. /// Check if section name is present
  212. bool hasImplicitSection() const {
  213. return getAttributes().hasAttribute("bss-section") ||
  214. getAttributes().hasAttribute("data-section") ||
  215. getAttributes().hasAttribute("relro-section") ||
  216. getAttributes().hasAttribute("rodata-section");
  217. }
  218. // Methods for support type inquiry through isa, cast, and dyn_cast:
  219. static bool classof(const Value *V) {
  220. return V->getValueID() == Value::GlobalVariableVal;
  221. }
  222. };
  223. template <>
  224. struct OperandTraits<GlobalVariable> :
  225. public OptionalOperandTraits<GlobalVariable> {
  226. };
  227. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value)
  228. } // end namespace llvm
  229. #endif // LLVM_IR_GLOBALVARIABLE_H
  230. #ifdef __GNUC__
  231. #pragma GCC diagnostic pop
  232. #endif