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