GlobalVariable.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. // HLSL Change Begin: Match operator new/delete
  47. void operator delete(void* Ptr) {
  48. User::operator delete(Ptr, 1);
  49. }
  50. // HLSL Change End
  51. /// GlobalVariable ctor - If a parent module is specified, the global is
  52. /// automatically inserted into the end of the specified modules global list.
  53. GlobalVariable(Type *Ty, bool isConstant, LinkageTypes Linkage,
  54. Constant *Initializer = nullptr, const Twine &Name = "",
  55. ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0,
  56. bool isExternallyInitialized = false);
  57. /// GlobalVariable ctor - This creates a global and inserts it before the
  58. /// specified other global.
  59. GlobalVariable(Module &M, Type *Ty, bool isConstant,
  60. LinkageTypes Linkage, Constant *Initializer,
  61. const Twine &Name = "", GlobalVariable *InsertBefore = nullptr,
  62. ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0,
  63. bool isExternallyInitialized = false);
  64. ~GlobalVariable() override {
  65. // FIXME: needed by operator delete
  66. setGlobalVariableNumOperands(1);
  67. }
  68. /// Provide fast operand accessors
  69. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  70. /// Definitions have initializers, declarations don't.
  71. ///
  72. inline bool hasInitializer() const { return !isDeclaration(); }
  73. /// hasDefinitiveInitializer - Whether the global variable has an initializer,
  74. /// and any other instances of the global (this can happen due to weak
  75. /// linkage) are guaranteed to have the same initializer.
  76. ///
  77. /// Note that if you want to transform a global, you must use
  78. /// hasUniqueInitializer() instead, because of the *_odr linkage type.
  79. ///
  80. /// Example:
  81. ///
  82. /// @a = global SomeType* null - Initializer is both definitive and unique.
  83. ///
  84. /// @b = global weak SomeType* null - Initializer is neither definitive nor
  85. /// unique.
  86. ///
  87. /// @c = global weak_odr SomeType* null - Initializer is definitive, but not
  88. /// unique.
  89. inline bool hasDefinitiveInitializer() const {
  90. return hasInitializer() &&
  91. // The initializer of a global variable with weak linkage may change at
  92. // link time.
  93. !mayBeOverridden() &&
  94. // The initializer of a global variable with the externally_initialized
  95. // marker may change at runtime before C++ initializers are evaluated.
  96. !isExternallyInitialized();
  97. }
  98. /// hasUniqueInitializer - Whether the global variable has an initializer, and
  99. /// any changes made to the initializer will turn up in the final executable.
  100. inline bool hasUniqueInitializer() const {
  101. return hasInitializer() &&
  102. // It's not safe to modify initializers of global variables with weak
  103. // linkage, because the linker might choose to discard the initializer and
  104. // use the initializer from another instance of the global variable
  105. // instead. It is wrong to modify the initializer of a global variable
  106. // with *_odr linkage because then different instances of the global may
  107. // have different initializers, breaking the One Definition Rule.
  108. !isWeakForLinker() &&
  109. // It is not safe to modify initializers of global variables with the
  110. // external_initializer marker since the value may be changed at runtime
  111. // before C++ initializers are evaluated.
  112. !isExternallyInitialized();
  113. }
  114. /// getInitializer - Return the initializer for this global variable. It is
  115. /// illegal to call this method if the global is external, because we cannot
  116. /// tell what the value is initialized to!
  117. ///
  118. inline const Constant *getInitializer() const {
  119. assert(hasInitializer() && "GV doesn't have initializer!");
  120. return static_cast<Constant*>(Op<0>().get());
  121. }
  122. inline Constant *getInitializer() {
  123. assert(hasInitializer() && "GV doesn't have initializer!");
  124. return static_cast<Constant*>(Op<0>().get());
  125. }
  126. /// setInitializer - Sets the initializer for this global variable, removing
  127. /// any existing initializer if InitVal==NULL. If this GV has type T*, the
  128. /// initializer must have type T.
  129. void setInitializer(Constant *InitVal);
  130. /// If the value is a global constant, its value is immutable throughout the
  131. /// runtime execution of the program. Assigning a value into the constant
  132. /// leads to undefined behavior.
  133. ///
  134. bool isConstant() const { return isConstantGlobal; }
  135. void setConstant(bool Val) { isConstantGlobal = Val; }
  136. bool isExternallyInitialized() const {
  137. return isExternallyInitializedConstant;
  138. }
  139. void setExternallyInitialized(bool Val) {
  140. isExternallyInitializedConstant = Val;
  141. }
  142. /// copyAttributesFrom - copy all additional attributes (those not needed to
  143. /// create a GlobalVariable) from the GlobalVariable Src to this one.
  144. void copyAttributesFrom(const GlobalValue *Src) override;
  145. /// removeFromParent - This method unlinks 'this' from the containing module,
  146. /// but does not delete it.
  147. ///
  148. void removeFromParent() override;
  149. /// eraseFromParent - This method unlinks 'this' from the containing module
  150. /// and deletes it.
  151. ///
  152. void eraseFromParent() override;
  153. // Methods for support type inquiry through isa, cast, and dyn_cast:
  154. static inline bool classof(const Value *V) {
  155. return V->getValueID() == Value::GlobalVariableVal;
  156. }
  157. };
  158. template <>
  159. struct OperandTraits<GlobalVariable> :
  160. public OptionalOperandTraits<GlobalVariable> {
  161. };
  162. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value)
  163. } // End llvm namespace
  164. #endif