1 //===-- llvm/GlobalVariable.h - GlobalVariable class ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the declaration of the GlobalVariable class, which
11 // represents a single global variable (or constant) in the VM.
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.
18 //===----------------------------------------------------------------------===//
20 #ifndef LLVM_GLOBAL_VARIABLE_H
21 #define LLVM_GLOBAL_VARIABLE_H
23 #include "llvm/GlobalValue.h"
24 #include "llvm/OperandTraits.h"
25 #include "llvm/ADT/ilist_node.h"
26 #include "llvm/ADT/Twine.h"
32 template<typename ValueSubClass, typename ItemParentClass>
33 class SymbolTableListTraits;
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
41 void setParent(Module *parent);
43 bool isConstantGlobal : 1; // Is this a global constant?
44 unsigned threadLocalMode : 3; // Is this symbol "Thread Local",
45 // if so, what is the desired model?
48 // allocate space for exactly one operand
49 void *operator new(size_t s) {
50 return User::operator new(s, 1);
53 enum ThreadLocalMode {
55 GeneralDynamicTLSModel,
61 /// GlobalVariable ctor - If a parent module is specified, the global is
62 /// automatically inserted into the end of the specified modules global list.
63 GlobalVariable(Type *Ty, bool isConstant, LinkageTypes Linkage,
64 Constant *Initializer = 0, const Twine &Name = "",
65 ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0);
66 /// GlobalVariable ctor - This creates a global and inserts it before the
67 /// specified other global.
68 GlobalVariable(Module &M, Type *Ty, bool isConstant,
69 LinkageTypes Linkage, Constant *Initializer,
70 const Twine &Name = "",
71 GlobalVariable *InsertBefore = 0,
72 ThreadLocalMode = NotThreadLocal,
73 unsigned AddressSpace = 0);
76 NumOperands = 1; // FIXME: needed by operator delete
79 /// Provide fast operand accessors
80 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
82 /// hasInitializer - Unless a global variable isExternal(), it has an
83 /// initializer. The initializer for the global variable/constant is held by
84 /// Initializer if an initializer is specified.
86 inline bool hasInitializer() const { return !isDeclaration(); }
88 /// hasDefinitiveInitializer - Whether the global variable has an initializer,
89 /// and any other instances of the global (this can happen due to weak
90 /// linkage) are guaranteed to have the same initializer.
92 /// Note that if you want to transform a global, you must use
93 /// hasUniqueInitializer() instead, because of the *_odr linkage type.
97 /// @a = global SomeType* null - Initializer is both definitive and unique.
99 /// @b = global weak SomeType* null - Initializer is neither definitive nor
102 /// @c = global weak_odr SomeType* null - Initializer is definitive, but not
104 inline bool hasDefinitiveInitializer() const {
105 return hasInitializer() &&
106 // The initializer of a global variable with weak linkage may change at
111 /// hasUniqueInitializer - Whether the global variable has an initializer, and
112 /// any changes made to the initializer will turn up in the final executable.
113 inline bool hasUniqueInitializer() const {
114 return hasInitializer() &&
115 // It's not safe to modify initializers of global variables with weak
116 // linkage, because the linker might choose to discard the initializer and
117 // use the initializer from another instance of the global variable
118 // instead. It is wrong to modify the initializer of a global variable
119 // with *_odr linkage because then different instances of the global may
120 // have different initializers, breaking the One Definition Rule.
124 /// getInitializer - Return the initializer for this global variable. It is
125 /// illegal to call this method if the global is external, because we cannot
126 /// tell what the value is initialized to!
128 inline const Constant *getInitializer() const {
129 assert(hasInitializer() && "GV doesn't have initializer!");
130 return static_cast<Constant*>(Op<0>().get());
132 inline Constant *getInitializer() {
133 assert(hasInitializer() && "GV doesn't have initializer!");
134 return static_cast<Constant*>(Op<0>().get());
136 /// setInitializer - Sets the initializer for this global variable, removing
137 /// any existing initializer if InitVal==NULL. If this GV has type T*, the
138 /// initializer must have type T.
139 void setInitializer(Constant *InitVal);
141 /// If the value is a global constant, its value is immutable throughout the
142 /// runtime execution of the program. Assigning a value into the constant
143 /// leads to undefined behavior.
145 bool isConstant() const { return isConstantGlobal; }
146 void setConstant(bool Val) { isConstantGlobal = Val; }
148 /// If the value is "Thread Local", its value isn't shared by the threads.
149 bool isThreadLocal() const { return threadLocalMode != NotThreadLocal; }
150 void setThreadLocal(bool Val) {
151 threadLocalMode = Val ? GeneralDynamicTLSModel : NotThreadLocal;
153 void setThreadLocalMode(ThreadLocalMode Val) { threadLocalMode = Val; }
154 ThreadLocalMode getThreadLocalMode() const {
155 return static_cast<ThreadLocalMode>(threadLocalMode);
158 /// copyAttributesFrom - copy all additional attributes (those not needed to
159 /// create a GlobalVariable) from the GlobalVariable Src to this one.
160 void copyAttributesFrom(const GlobalValue *Src);
162 /// removeFromParent - This method unlinks 'this' from the containing module,
163 /// but does not delete it.
165 virtual void removeFromParent();
167 /// eraseFromParent - This method unlinks 'this' from the containing module
170 virtual void eraseFromParent();
172 /// Override Constant's implementation of this method so we can
173 /// replace constant initializers.
174 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
176 // Methods for support type inquiry through isa, cast, and dyn_cast:
177 static inline bool classof(const GlobalVariable *) { return true; }
178 static inline bool classof(const Value *V) {
179 return V->getValueID() == Value::GlobalVariableVal;
184 struct OperandTraits<GlobalVariable> :
185 public OptionalOperandTraits<GlobalVariable> {
188 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value)
190 } // End llvm namespace