Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
8.3 KiB

  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/GlobalValue.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 GlobalValue, public ilist_node<GlobalVariable> {
  31. friend class SymbolTableListTraits<GlobalVariable, Module>;
  32. void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
  33. void operator=(const GlobalVariable &) LLVM_DELETED_FUNCTION;
  34. GlobalVariable(const GlobalVariable &) LLVM_DELETED_FUNCTION;
  35. void setParent(Module *parent);
  36. bool isConstantGlobal : 1; // Is this a global constant?
  37. unsigned threadLocalMode : 3; // Is this symbol "Thread Local",
  38. // if so, what is the desired
  39. // model?
  40. bool isExternallyInitializedConstant : 1; // Is this a global whose value
  41. // can change from its initial
  42. // value before global
  43. // initializers are run?
  44. public:
  45. // allocate space for exactly one operand
  46. void *operator new(size_t s) {
  47. return User::operator new(s, 1);
  48. }
  49. enum ThreadLocalMode {
  50. NotThreadLocal = 0,
  51. GeneralDynamicTLSModel,
  52. LocalDynamicTLSModel,
  53. InitialExecTLSModel,
  54. LocalExecTLSModel
  55. };
  56. /// GlobalVariable ctor - If a parent module is specified, the global is
  57. /// automatically inserted into the end of the specified modules global list.
  58. GlobalVariable(Type *Ty, bool isConstant, LinkageTypes Linkage,
  59. Constant *Initializer = 0, const Twine &Name = "",
  60. ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0,
  61. bool isExternallyInitialized = false);
  62. /// GlobalVariable ctor - This creates a global and inserts it before the
  63. /// specified other global.
  64. GlobalVariable(Module &M, Type *Ty, bool isConstant,
  65. LinkageTypes Linkage, Constant *Initializer,
  66. const Twine &Name = "", GlobalVariable *InsertBefore = 0,
  67. ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0,
  68. bool isExternallyInitialized = false);
  69. ~GlobalVariable() {
  70. NumOperands = 1; // FIXME: needed by operator delete
  71. }
  72. /// Provide fast operand accessors
  73. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  74. /// hasInitializer - Unless a global variable isExternal(), it has an
  75. /// initializer. The initializer for the global variable/constant is held by
  76. /// Initializer if an initializer is specified.
  77. ///
  78. inline bool hasInitializer() const { return !isDeclaration(); }
  79. /// hasDefinitiveInitializer - Whether the global variable has an initializer,
  80. /// and any other instances of the global (this can happen due to weak
  81. /// linkage) are guaranteed to have the same initializer.
  82. ///
  83. /// Note that if you want to transform a global, you must use
  84. /// hasUniqueInitializer() instead, because of the *_odr linkage type.
  85. ///
  86. /// Example:
  87. ///
  88. /// @a = global SomeType* null - Initializer is both definitive and unique.
  89. ///
  90. /// @b = global weak SomeType* null - Initializer is neither definitive nor
  91. /// unique.
  92. ///
  93. /// @c = global weak_odr SomeType* null - Initializer is definitive, but not
  94. /// unique.
  95. inline bool hasDefinitiveInitializer() const {
  96. return hasInitializer() &&
  97. // The initializer of a global variable with weak linkage may change at
  98. // link time.
  99. !mayBeOverridden() &&
  100. // The initializer of a global variable with the externally_initialized
  101. // marker may change at runtime before C++ initializers are evaluated.
  102. !isExternallyInitialized();
  103. }
  104. /// hasUniqueInitializer - Whether the global variable has an initializer, and
  105. /// any changes made to the initializer will turn up in the final executable.
  106. inline bool hasUniqueInitializer() const {
  107. return hasInitializer() &&
  108. // It's not safe to modify initializers of global variables with weak
  109. // linkage, because the linker might choose to discard the initializer and
  110. // use the initializer from another instance of the global variable
  111. // instead. It is wrong to modify the initializer of a global variable
  112. // with *_odr linkage because then different instances of the global may
  113. // have different initializers, breaking the One Definition Rule.
  114. !isWeakForLinker() &&
  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. /// If the value is "Thread Local", its value isn't shared by the threads.
  143. bool isThreadLocal() const { return threadLocalMode != NotThreadLocal; }
  144. void setThreadLocal(bool Val) {
  145. threadLocalMode = Val ? GeneralDynamicTLSModel : NotThreadLocal;
  146. }
  147. void setThreadLocalMode(ThreadLocalMode Val) { threadLocalMode = Val; }
  148. ThreadLocalMode getThreadLocalMode() const {
  149. return static_cast<ThreadLocalMode>(threadLocalMode);
  150. }
  151. bool isExternallyInitialized() const {
  152. return isExternallyInitializedConstant;
  153. }
  154. void setExternallyInitialized(bool Val) {
  155. isExternallyInitializedConstant = Val;
  156. }
  157. /// copyAttributesFrom - copy all additional attributes (those not needed to
  158. /// create a GlobalVariable) from the GlobalVariable Src to this one.
  159. void copyAttributesFrom(const GlobalValue *Src);
  160. /// removeFromParent - This method unlinks 'this' from the containing module,
  161. /// but does not delete it.
  162. ///
  163. virtual void removeFromParent();
  164. /// eraseFromParent - This method unlinks 'this' from the containing module
  165. /// and deletes it.
  166. ///
  167. virtual void eraseFromParent();
  168. /// Override Constant's implementation of this method so we can
  169. /// replace constant initializers.
  170. virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
  171. // Methods for support type inquiry through isa, cast, and dyn_cast:
  172. static inline bool classof(const Value *V) {
  173. return V->getValueID() == Value::GlobalVariableVal;
  174. }
  175. };
  176. template <>
  177. struct OperandTraits<GlobalVariable> :
  178. public OptionalOperandTraits<GlobalVariable> {
  179. };
  180. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value)
  181. } // End llvm namespace
  182. #endif