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_IR_GLOBALVARIABLE_H
21 #define LLVM_IR_GLOBALVARIABLE_H
23 #include "llvm/ADT/Twine.h"
24 #include "llvm/ADT/ilist_node.h"
25 #include "llvm/IR/GlobalValue.h"
26 #include "llvm/IR/OperandTraits.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) LLVM_DELETED_FUNCTION;
38 void operator=(const GlobalVariable &) LLVM_DELETED_FUNCTION;
39 GlobalVariable(const GlobalVariable &) LLVM_DELETED_FUNCTION;
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
47 bool isExternallyInitializedConstant : 1; // Is this a global whose value
48 // can change from its initial
49 // value before global
50 // initializers are run?
53 // allocate space for exactly one operand
54 void *operator new(size_t s) {
55 return User::operator new(s, 1);
58 enum ThreadLocalMode {
60 GeneralDynamicTLSModel,
66 /// GlobalVariable ctor - If a parent module is specified, the global is
67 /// automatically inserted into the end of the specified modules global list.
68 GlobalVariable(Type *Ty, bool isConstant, LinkageTypes Linkage,
69 Constant *Initializer = 0, const Twine &Name = "",
70 ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0,
71 bool isExternallyInitialized = false);
72 /// GlobalVariable ctor - This creates a global and inserts it before the
73 /// specified other global.
74 GlobalVariable(Module &M, Type *Ty, bool isConstant,
75 LinkageTypes Linkage, Constant *Initializer,
76 const Twine &Name = "", GlobalVariable *InsertBefore = 0,
77 ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0,
78 bool isExternallyInitialized = false);
81 NumOperands = 1; // FIXME: needed by operator delete
84 /// Provide fast operand accessors
85 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
87 /// hasInitializer - Unless a global variable isExternal(), it has an
88 /// initializer. The initializer for the global variable/constant is held by
89 /// Initializer if an initializer is specified.
91 inline bool hasInitializer() const { return !isDeclaration(); }
93 /// hasDefinitiveInitializer - Whether the global variable has an initializer,
94 /// and any other instances of the global (this can happen due to weak
95 /// linkage) are guaranteed to have the same initializer.
97 /// Note that if you want to transform a global, you must use
98 /// hasUniqueInitializer() instead, because of the *_odr linkage type.
102 /// @a = global SomeType* null - Initializer is both definitive and unique.
104 /// @b = global weak SomeType* null - Initializer is neither definitive nor
107 /// @c = global weak_odr SomeType* null - Initializer is definitive, but not
109 inline bool hasDefinitiveInitializer() const {
110 return hasInitializer() &&
111 // The initializer of a global variable with weak linkage may change at
116 /// hasUniqueInitializer - Whether the global variable has an initializer, and
117 /// any changes made to the initializer will turn up in the final executable.
118 inline bool hasUniqueInitializer() const {
119 return hasInitializer() &&
120 // It's not safe to modify initializers of global variables with weak
121 // linkage, because the linker might choose to discard the initializer and
122 // use the initializer from another instance of the global variable
123 // instead. It is wrong to modify the initializer of a global variable
124 // with *_odr linkage because then different instances of the global may
125 // have different initializers, breaking the One Definition Rule.
129 /// getInitializer - Return the initializer for this global variable. It is
130 /// illegal to call this method if the global is external, because we cannot
131 /// tell what the value is initialized to!
133 inline const Constant *getInitializer() const {
134 assert(hasInitializer() && "GV doesn't have initializer!");
135 return static_cast<Constant*>(Op<0>().get());
137 inline Constant *getInitializer() {
138 assert(hasInitializer() && "GV doesn't have initializer!");
139 return static_cast<Constant*>(Op<0>().get());
141 /// setInitializer - Sets the initializer for this global variable, removing
142 /// any existing initializer if InitVal==NULL. If this GV has type T*, the
143 /// initializer must have type T.
144 void setInitializer(Constant *InitVal);
146 /// If the value is a global constant, its value is immutable throughout the
147 /// runtime execution of the program. Assigning a value into the constant
148 /// leads to undefined behavior.
150 bool isConstant() const { return isConstantGlobal; }
151 void setConstant(bool Val) { isConstantGlobal = Val; }
153 /// If the value is "Thread Local", its value isn't shared by the threads.
154 bool isThreadLocal() const { return threadLocalMode != NotThreadLocal; }
155 void setThreadLocal(bool Val) {
156 threadLocalMode = Val ? GeneralDynamicTLSModel : NotThreadLocal;
158 void setThreadLocalMode(ThreadLocalMode Val) { threadLocalMode = Val; }
159 ThreadLocalMode getThreadLocalMode() const {
160 return static_cast<ThreadLocalMode>(threadLocalMode);
163 bool isExternallyInitialized() const {
164 return isExternallyInitializedConstant;
166 void setExternallyInitialized(bool Val) {
167 isExternallyInitializedConstant = Val;
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 Value *V) {
190 return V->getValueID() == Value::GlobalVariableVal;
195 struct OperandTraits<GlobalVariable> :
196 public OptionalOperandTraits<GlobalVariable> {
199 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value)
201 } // End llvm namespace