GlobalVariable.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //===-- llvm/GlobalVariable.h - GlobalVariable class ------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file contains the declaration of the GlobalVariable class, which
  11. // represents a single global variable (or constant) in the VM.
  12. //
  13. // Global variables are constant pointers that refer to hunks of space that are
  14. // allocated by either the VM, or by the linker in a static compiler. A global
  15. // variable may have an initial value, which is copied into the executables .data
  16. // area. Global Constants are required to have initializers.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_IR_GLOBALVARIABLE_H
  20. #define LLVM_IR_GLOBALVARIABLE_H
  21. #include "llvm/ADT/Twine.h"
  22. #include "llvm/ADT/ilist_node.h"
  23. #include "llvm/IR/GlobalObject.h"
  24. #include "llvm/IR/OperandTraits.h"
  25. namespace llvm {
  26. class Module;
  27. class Constant;
  28. template<typename ValueSubClass, typename ItemParentClass>
  29. class SymbolTableListTraits;
  30. class GlobalVariable : public GlobalObject, public ilist_node<GlobalVariable> {
  31. friend class SymbolTableListTraits<GlobalVariable, Module>;
  32. void *operator new(size_t, unsigned) = delete;
  33. void operator=(const GlobalVariable &) = delete;
  34. GlobalVariable(const GlobalVariable &) = delete;
  35. void setParent(Module *parent);
  36. bool isConstantGlobal : 1; // Is this a global constant?
  37. bool isExternallyInitializedConstant : 1; // Is this a global whose value
  38. // can change from its initial
  39. // value before global
  40. // initializers are run?
  41. public:
  42. // allocate space for exactly one operand
  43. void *operator new(size_t s) {
  44. return User::operator new(s, 1);
  45. }
  46. /// GlobalVariable ctor - If a parent module is specified, the global is
  47. /// automatically inserted into the end of the specified modules global list.
  48. GlobalVariable(Type *Ty, bool isConstant, LinkageTypes Linkage,
  49. Constant *Initializer = nullptr, const Twine &Name = "",
  50. ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0,
  51. bool isExternallyInitialized = false);
  52. /// GlobalVariable ctor - This creates a global and inserts it before the
  53. /// specified other global.
  54. GlobalVariable(Module &M, Type *Ty, bool isConstant,
  55. LinkageTypes Linkage, Constant *Initializer,
  56. const Twine &Name = "", GlobalVariable *InsertBefore = nullptr,
  57. ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0,
  58. bool isExternallyInitialized = false);
  59. ~GlobalVariable() override {
  60. // FIXME: needed by operator delete
  61. setGlobalVariableNumOperands(1);
  62. }
  63. /// Provide fast operand accessors
  64. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  65. /// Definitions have initializers, declarations don't.
  66. ///
  67. inline bool hasInitializer() const { return !isDeclaration(); }
  68. /// hasDefinitiveInitializer - Whether the global variable has an initializer,
  69. /// and any other instances of the global (this can happen due to weak
  70. /// linkage) are guaranteed to have the same initializer.
  71. ///
  72. /// Note that if you want to transform a global, you must use
  73. /// hasUniqueInitializer() instead, because of the *_odr linkage type.
  74. ///
  75. /// Example:
  76. ///
  77. /// @a = global SomeType* null - Initializer is both definitive and unique.
  78. ///
  79. /// @b = global weak SomeType* null - Initializer is neither definitive nor
  80. /// unique.
  81. ///
  82. /// @c = global weak_odr SomeType* null - Initializer is definitive, but not
  83. /// unique.
  84. inline bool hasDefinitiveInitializer() const {
  85. return hasInitializer() &&
  86. // The initializer of a global variable with weak linkage may change at
  87. // link time.
  88. !mayBeOverridden() &&
  89. // The initializer of a global variable with the externally_initialized
  90. // marker may change at runtime before C++ initializers are evaluated.
  91. !isExternallyInitialized();
  92. }
  93. /// hasUniqueInitializer - Whether the global variable has an initializer, and
  94. /// any changes made to the initializer will turn up in the final executable.
  95. inline bool hasUniqueInitializer() const {
  96. return hasInitializer() &&
  97. // It's not safe to modify initializers of global variables with weak
  98. // linkage, because the linker might choose to discard the initializer and
  99. // use the initializer from another instance of the global variable
  100. // instead. It is wrong to modify the initializer of a global variable
  101. // with *_odr linkage because then different instances of the global may
  102. // have different initializers, breaking the One Definition Rule.
  103. !isWeakForLinker() &&
  104. // It is not safe to modify initializers of global variables with the
  105. // external_initializer marker since the value may be changed at runtime
  106. // before C++ initializers are evaluated.
  107. !isExternallyInitialized();
  108. }
  109. /// getInitializer - Return the initializer for this global variable. It is
  110. /// illegal to call this method if the global is external, because we cannot
  111. /// tell what the value is initialized to!
  112. ///
  113. inline const Constant *getInitializer() const {
  114. assert(hasInitializer() && "GV doesn't have initializer!");
  115. return static_cast<Constant*>(Op<0>().get());
  116. }
  117. inline Constant *getInitializer() {
  118. assert(hasInitializer() && "GV doesn't have initializer!");
  119. return static_cast<Constant*>(Op<0>().get());
  120. }
  121. /// setInitializer - Sets the initializer for this global variable, removing
  122. /// any existing initializer if InitVal==NULL. If this GV has type T*, the
  123. /// initializer must have type T.
  124. void setInitializer(Constant *InitVal);
  125. /// If the value is a global constant, its value is immutable throughout the
  126. /// runtime execution of the program. Assigning a value into the constant
  127. /// leads to undefined behavior.
  128. ///
  129. bool isConstant() const { return isConstantGlobal; }
  130. void setConstant(bool Val) { isConstantGlobal = Val; }
  131. bool isExternallyInitialized() const {
  132. return isExternallyInitializedConstant;
  133. }
  134. void setExternallyInitialized(bool Val) {
  135. isExternallyInitializedConstant = Val;
  136. }
  137. /// copyAttributesFrom - copy all additional attributes (those not needed to
  138. /// create a GlobalVariable) from the GlobalVariable Src to this one.
  139. void copyAttributesFrom(const GlobalValue *Src) override;
  140. /// removeFromParent - This method unlinks 'this' from the containing module,
  141. /// but does not delete it.
  142. ///
  143. void removeFromParent() override;
  144. /// eraseFromParent - This method unlinks 'this' from the containing module
  145. /// and deletes it.
  146. ///
  147. void eraseFromParent() override;
  148. // Methods for support type inquiry through isa, cast, and dyn_cast:
  149. static inline bool classof(const Value *V) {
  150. return V->getValueID() == Value::GlobalVariableVal;
  151. }
  152. };
  153. template <>
  154. struct OperandTraits<GlobalVariable> :
  155. public OptionalOperandTraits<GlobalVariable> {
  156. };
  157. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value)
  158. } // End llvm namespace
  159. #endif