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_IR_GLOBALVARIABLE_H 21 #define LLVM_IR_GLOBALVARIABLE_H 22 23 #include "llvm/ADT/Twine.h" 24 #include "llvm/ADT/ilist_node.h" 25 #include "llvm/IR/GlobalObject.h" 26 #include "llvm/IR/OperandTraits.h" 27 28 namespace llvm { 29 30 class Module; 31 class Constant; 32 template <typename ValueSubClass> class SymbolTableListTraits; 33 34 class GlobalVariable : public GlobalObject, public ilist_node<GlobalVariable> { 35 friend class SymbolTableListTraits<GlobalVariable>; 36 void *operator new(size_t, unsigned) = delete; 37 void operator=(const GlobalVariable &) = delete; 38 GlobalVariable(const GlobalVariable &) = delete; 39 40 void setParent(Module *parent); 41 42 bool isConstantGlobal : 1; // Is this a global constant? 43 bool isExternallyInitializedConstant : 1; // Is this a global whose value 44 // can change from its initial 45 // value before global 46 // initializers are run? 47 public: 48 // allocate space for exactly one operand new(size_t s)49 void *operator new(size_t s) { 50 return User::operator new(s, 1); 51 } 52 53 /// GlobalVariable ctor - If a parent module is specified, the global is 54 /// automatically inserted into the end of the specified modules global list. 55 GlobalVariable(Type *Ty, bool isConstant, LinkageTypes Linkage, 56 Constant *Initializer = nullptr, const Twine &Name = "", 57 ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0, 58 bool isExternallyInitialized = false); 59 /// GlobalVariable ctor - This creates a global and inserts it before the 60 /// specified other global. 61 GlobalVariable(Module &M, Type *Ty, bool isConstant, 62 LinkageTypes Linkage, Constant *Initializer, 63 const Twine &Name = "", GlobalVariable *InsertBefore = nullptr, 64 ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0, 65 bool isExternallyInitialized = false); 66 ~GlobalVariable()67 ~GlobalVariable() override { 68 dropAllReferences(); 69 70 // FIXME: needed by operator delete 71 setGlobalVariableNumOperands(1); 72 } 73 74 /// Provide fast operand accessors 75 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 76 77 /// Definitions have initializers, declarations don't. 78 /// hasInitializer()79 inline bool hasInitializer() const { return !isDeclaration(); } 80 81 /// hasDefinitiveInitializer - Whether the global variable has an initializer, 82 /// and any other instances of the global (this can happen due to weak 83 /// linkage) are guaranteed to have the same initializer. 84 /// 85 /// Note that if you want to transform a global, you must use 86 /// hasUniqueInitializer() instead, because of the *_odr linkage type. 87 /// 88 /// Example: 89 /// 90 /// @a = global SomeType* null - Initializer is both definitive and unique. 91 /// 92 /// @b = global weak SomeType* null - Initializer is neither definitive nor 93 /// unique. 94 /// 95 /// @c = global weak_odr SomeType* null - Initializer is definitive, but not 96 /// unique. hasDefinitiveInitializer()97 inline bool hasDefinitiveInitializer() const { 98 return hasInitializer() && 99 // The initializer of a global variable may change to something arbitrary 100 // at link time. 101 !isInterposable() && 102 // The initializer of a global variable with the externally_initialized 103 // marker may change at runtime before C++ initializers are evaluated. 104 !isExternallyInitialized(); 105 } 106 107 /// hasUniqueInitializer - Whether the global variable has an initializer, and 108 /// any changes made to the initializer will turn up in the final executable. hasUniqueInitializer()109 inline bool hasUniqueInitializer() const { 110 return 111 // We need to be sure this is the definition that will actually be used 112 isStrongDefinitionForLinker() && 113 // It is not safe to modify initializers of global variables with the 114 // external_initializer marker since the value may be changed at runtime 115 // before C++ initializers are evaluated. 116 !isExternallyInitialized(); 117 } 118 119 /// getInitializer - Return the initializer for this global variable. It is 120 /// illegal to call this method if the global is external, because we cannot 121 /// tell what the value is initialized to! 122 /// getInitializer()123 inline const Constant *getInitializer() const { 124 assert(hasInitializer() && "GV doesn't have initializer!"); 125 return static_cast<Constant*>(Op<0>().get()); 126 } getInitializer()127 inline Constant *getInitializer() { 128 assert(hasInitializer() && "GV doesn't have initializer!"); 129 return static_cast<Constant*>(Op<0>().get()); 130 } 131 /// setInitializer - Sets the initializer for this global variable, removing 132 /// any existing initializer if InitVal==NULL. If this GV has type T*, the 133 /// initializer must have type T. 134 void setInitializer(Constant *InitVal); 135 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 /// isConstant()140 bool isConstant() const { return isConstantGlobal; } setConstant(bool Val)141 void setConstant(bool Val) { isConstantGlobal = Val; } 142 isExternallyInitialized()143 bool isExternallyInitialized() const { 144 return isExternallyInitializedConstant; 145 } setExternallyInitialized(bool Val)146 void setExternallyInitialized(bool Val) { 147 isExternallyInitializedConstant = Val; 148 } 149 150 /// copyAttributesFrom - copy all additional attributes (those not needed to 151 /// create a GlobalVariable) from the GlobalVariable Src to this one. 152 void copyAttributesFrom(const GlobalValue *Src) override; 153 154 /// removeFromParent - This method unlinks 'this' from the containing module, 155 /// but does not delete it. 156 /// 157 void removeFromParent() override; 158 159 /// eraseFromParent - This method unlinks 'this' from the containing module 160 /// and deletes it. 161 /// 162 void eraseFromParent() override; 163 164 /// Drop all references in preparation to destroy the GlobalVariable. This 165 /// drops not only the reference to the initializer but also to any metadata. 166 void dropAllReferences(); 167 168 // Methods for support type inquiry through isa, cast, and dyn_cast: classof(const Value * V)169 static inline bool classof(const Value *V) { 170 return V->getValueID() == Value::GlobalVariableVal; 171 } 172 }; 173 174 template <> 175 struct OperandTraits<GlobalVariable> : 176 public OptionalOperandTraits<GlobalVariable> { 177 }; 178 179 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value) 180 181 } // End llvm namespace 182 183 #endif 184