GlobalVariable.h 9.8 KB

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