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.

192 lines
7.4 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_GLOBAL_VARIABLE_H
  20. #define LLVM_GLOBAL_VARIABLE_H
  21. #include "llvm/GlobalValue.h"
  22. #include "llvm/OperandTraits.h"
  23. #include "llvm/ADT/ilist_node.h"
  24. #include "llvm/ADT/Twine.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 model?
  39. public:
  40. // allocate space for exactly one operand
  41. void *operator new(size_t s) {
  42. return User::operator new(s, 1);
  43. }
  44. enum ThreadLocalMode {
  45. NotThreadLocal = 0,
  46. GeneralDynamicTLSModel,
  47. LocalDynamicTLSModel,
  48. InitialExecTLSModel,
  49. LocalExecTLSModel
  50. };
  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 = 0, const Twine &Name = "",
  55. ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0);
  56. /// GlobalVariable ctor - This creates a global and inserts it before the
  57. /// specified other global.
  58. GlobalVariable(Module &M, Type *Ty, bool isConstant,
  59. LinkageTypes Linkage, Constant *Initializer,
  60. const Twine &Name = "",
  61. GlobalVariable *InsertBefore = 0,
  62. ThreadLocalMode = NotThreadLocal,
  63. unsigned AddressSpace = 0);
  64. ~GlobalVariable() {
  65. NumOperands = 1; // FIXME: needed by operator delete
  66. }
  67. /// Provide fast operand accessors
  68. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  69. /// hasInitializer - Unless a global variable isExternal(), it has an
  70. /// initializer. The initializer for the global variable/constant is held by
  71. /// Initializer if an initializer is specified.
  72. ///
  73. inline bool hasInitializer() const { return !isDeclaration(); }
  74. /// hasDefinitiveInitializer - Whether the global variable has an initializer,
  75. /// and any other instances of the global (this can happen due to weak
  76. /// linkage) are guaranteed to have the same initializer.
  77. ///
  78. /// Note that if you want to transform a global, you must use
  79. /// hasUniqueInitializer() instead, because of the *_odr linkage type.
  80. ///
  81. /// Example:
  82. ///
  83. /// @a = global SomeType* null - Initializer is both definitive and unique.
  84. ///
  85. /// @b = global weak SomeType* null - Initializer is neither definitive nor
  86. /// unique.
  87. ///
  88. /// @c = global weak_odr SomeType* null - Initializer is definitive, but not
  89. /// unique.
  90. inline bool hasDefinitiveInitializer() const {
  91. return hasInitializer() &&
  92. // The initializer of a global variable with weak linkage may change at
  93. // link time.
  94. !mayBeOverridden();
  95. }
  96. /// hasUniqueInitializer - Whether the global variable has an initializer, and
  97. /// any changes made to the initializer will turn up in the final executable.
  98. inline bool hasUniqueInitializer() const {
  99. return hasInitializer() &&
  100. // It's not safe to modify initializers of global variables with weak
  101. // linkage, because the linker might choose to discard the initializer and
  102. // use the initializer from another instance of the global variable
  103. // instead. It is wrong to modify the initializer of a global variable
  104. // with *_odr linkage because then different instances of the global may
  105. // have different initializers, breaking the One Definition Rule.
  106. !isWeakForLinker();
  107. }
  108. /// getInitializer - Return the initializer for this global variable. It is
  109. /// illegal to call this method if the global is external, because we cannot
  110. /// tell what the value is initialized to!
  111. ///
  112. inline const Constant *getInitializer() const {
  113. assert(hasInitializer() && "GV doesn't have initializer!");
  114. return static_cast<Constant*>(Op<0>().get());
  115. }
  116. inline Constant *getInitializer() {
  117. assert(hasInitializer() && "GV doesn't have initializer!");
  118. return static_cast<Constant*>(Op<0>().get());
  119. }
  120. /// setInitializer - Sets the initializer for this global variable, removing
  121. /// any existing initializer if InitVal==NULL. If this GV has type T*, the
  122. /// initializer must have type T.
  123. void setInitializer(Constant *InitVal);
  124. /// If the value is a global constant, its value is immutable throughout the
  125. /// runtime execution of the program. Assigning a value into the constant
  126. /// leads to undefined behavior.
  127. ///
  128. bool isConstant() const { return isConstantGlobal; }
  129. void setConstant(bool Val) { isConstantGlobal = Val; }
  130. /// If the value is "Thread Local", its value isn't shared by the threads.
  131. bool isThreadLocal() const { return threadLocalMode != NotThreadLocal; }
  132. void setThreadLocal(bool Val) {
  133. threadLocalMode = Val ? GeneralDynamicTLSModel : NotThreadLocal;
  134. }
  135. void setThreadLocalMode(ThreadLocalMode Val) { threadLocalMode = Val; }
  136. ThreadLocalMode getThreadLocalMode() const {
  137. return static_cast<ThreadLocalMode>(threadLocalMode);
  138. }
  139. /// copyAttributesFrom - copy all additional attributes (those not needed to
  140. /// create a GlobalVariable) from the GlobalVariable Src to this one.
  141. void copyAttributesFrom(const GlobalValue *Src);
  142. /// removeFromParent - This method unlinks 'this' from the containing module,
  143. /// but does not delete it.
  144. ///
  145. virtual void removeFromParent();
  146. /// eraseFromParent - This method unlinks 'this' from the containing module
  147. /// and deletes it.
  148. ///
  149. virtual void eraseFromParent();
  150. /// Override Constant's implementation of this method so we can
  151. /// replace constant initializers.
  152. virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
  153. // Methods for support type inquiry through isa, cast, and dyn_cast:
  154. static inline bool classof(const GlobalVariable *) { return true; }
  155. static inline bool classof(const Value *V) {
  156. return V->getValueID() == Value::GlobalVariableVal;
  157. }
  158. };
  159. template <>
  160. struct OperandTraits<GlobalVariable> :
  161. public OptionalOperandTraits<GlobalVariable> {
  162. };
  163. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value)
  164. } // End llvm namespace
  165. #endif