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 // TODO: Remove these once Clang is updated.
62 GlobalVariable(Type *Ty, bool isConstant, LinkageTypes Linkage,
63 Constant *Initializer = 0, const Twine &Name = "",
64 bool ThreadLocal = false, unsigned AddressSpace = 0);
65 GlobalVariable(Module &M, Type *Ty, bool isConstant,
66 LinkageTypes Linkage, Constant *Initializer,
67 const Twine &Name = "",
68 GlobalVariable *InsertBefore = 0, bool ThreadLocal = false,
69 unsigned AddressSpace = 0);
71 /// GlobalVariable ctor - If a parent module is specified, the global is
72 /// automatically inserted into the end of the specified modules global list.
73 // TODO: Put default param values back when ctors above are removed.
74 GlobalVariable(Type *Ty, bool isConstant, LinkageTypes Linkage,
75 Constant *Initializer, const Twine &Name,
76 ThreadLocalMode, unsigned AddressSpace = 0);
77 /// GlobalVariable ctor - This creates a global and inserts it before the
78 /// specified other global.
79 // TODO: Put default param values back when ctors above are removed.
80 GlobalVariable(Module &M, Type *Ty, bool isConstant,
81 LinkageTypes Linkage, Constant *Initializer,
83 GlobalVariable *InsertBefore,
85 unsigned AddressSpace = 0);
88 NumOperands = 1; // FIXME: needed by operator delete
91 /// Provide fast operand accessors
92 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
94 /// hasInitializer - Unless a global variable isExternal(), it has an
95 /// initializer. The initializer for the global variable/constant is held by
96 /// Initializer if an initializer is specified.
98 inline bool hasInitializer() const { return !isDeclaration(); }
100 /// hasDefinitiveInitializer - Whether the global variable has an initializer,
101 /// and any other instances of the global (this can happen due to weak
102 /// linkage) are guaranteed to have the same initializer.
104 /// Note that if you want to transform a global, you must use
105 /// hasUniqueInitializer() instead, because of the *_odr linkage type.
109 /// @a = global SomeType* null - Initializer is both definitive and unique.
111 /// @b = global weak SomeType* null - Initializer is neither definitive nor
114 /// @c = global weak_odr SomeType* null - Initializer is definitive, but not
116 inline bool hasDefinitiveInitializer() const {
117 return hasInitializer() &&
118 // The initializer of a global variable with weak linkage may change at
123 /// hasUniqueInitializer - Whether the global variable has an initializer, and
124 /// any changes made to the initializer will turn up in the final executable.
125 inline bool hasUniqueInitializer() const {
126 return hasInitializer() &&
127 // It's not safe to modify initializers of global variables with weak
128 // linkage, because the linker might choose to discard the initializer and
129 // use the initializer from another instance of the global variable
130 // instead. It is wrong to modify the initializer of a global variable
131 // with *_odr linkage because then different instances of the global may
132 // have different initializers, breaking the One Definition Rule.
136 /// getInitializer - Return the initializer for this global variable. It is
137 /// illegal to call this method if the global is external, because we cannot
138 /// tell what the value is initialized to!
140 inline const Constant *getInitializer() const {
141 assert(hasInitializer() && "GV doesn't have initializer!");
142 return static_cast<Constant*>(Op<0>().get());
144 inline Constant *getInitializer() {
145 assert(hasInitializer() && "GV doesn't have initializer!");
146 return static_cast<Constant*>(Op<0>().get());
148 /// setInitializer - Sets the initializer for this global variable, removing
149 /// any existing initializer if InitVal==NULL. If this GV has type T*, the
150 /// initializer must have type T.
151 void setInitializer(Constant *InitVal);
153 /// If the value is a global constant, its value is immutable throughout the
154 /// runtime execution of the program. Assigning a value into the constant
155 /// leads to undefined behavior.
157 bool isConstant() const { return isConstantGlobal; }
158 void setConstant(bool Val) { isConstantGlobal = Val; }
160 /// If the value is "Thread Local", its value isn't shared by the threads.
161 bool isThreadLocal() const { return threadLocalMode != NotThreadLocal; }
162 void setThreadLocal(bool Val) {
163 threadLocalMode = Val ? GeneralDynamicTLSModel : NotThreadLocal;
165 void setThreadLocalMode(ThreadLocalMode Val) { threadLocalMode = Val; }
166 ThreadLocalMode getThreadLocalMode() const {
167 return static_cast<ThreadLocalMode>(threadLocalMode);
170 /// copyAttributesFrom - copy all additional attributes (those not needed to
171 /// create a GlobalVariable) from the GlobalVariable Src to this one.
172 void copyAttributesFrom(const GlobalValue *Src);
174 /// removeFromParent - This method unlinks 'this' from the containing module,
175 /// but does not delete it.
177 virtual void removeFromParent();
179 /// eraseFromParent - This method unlinks 'this' from the containing module
182 virtual void eraseFromParent();
184 /// Override Constant's implementation of this method so we can
185 /// replace constant initializers.
186 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
188 // Methods for support type inquiry through isa, cast, and dyn_cast:
189 static inline bool classof(const GlobalVariable *) { return true; }
190 static inline bool classof(const Value *V) {
191 return V->getValueID() == Value::GlobalVariableVal;
196 struct OperandTraits<GlobalVariable> :
197 public OptionalOperandTraits<GlobalVariable> {
200 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value)
202 } // End llvm namespace