79463fbdf0ff9729098f645fec3038527d551040
[oota-llvm.git] / include / llvm / GlobalVariable.h
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/ADT/Twine.h"
24 #include "llvm/ADT/ilist_node.h"
25 #include "llvm/GlobalValue.h"
26 #include "llvm/OperandTraits.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) LLVM_DELETED_FUNCTION;
38   void operator=(const GlobalVariable &) LLVM_DELETED_FUNCTION;
39   GlobalVariable(const GlobalVariable &) LLVM_DELETED_FUNCTION;
40
41   void setParent(Module *parent);
42
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?
46
47 public:
48   // allocate space for exactly one operand
49   void *operator new(size_t s) {
50     return User::operator new(s, 1);
51   }
52
53   enum ThreadLocalMode {
54     NotThreadLocal = 0,
55     GeneralDynamicTLSModel,
56     LocalDynamicTLSModel,
57     InitialExecTLSModel,
58     LocalExecTLSModel
59   };
60
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);
74
75   ~GlobalVariable() {
76     NumOperands = 1; // FIXME: needed by operator delete
77   }
78
79   /// Provide fast operand accessors
80   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
81
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.
85   ///
86   inline bool hasInitializer() const { return !isDeclaration(); }
87
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.
91   ///
92   /// Note that if you want to transform a global, you must use
93   /// hasUniqueInitializer() instead, because of the *_odr linkage type.
94   ///
95   /// Example:
96   ///
97   /// @a = global SomeType* null - Initializer is both definitive and unique.
98   ///
99   /// @b = global weak SomeType* null - Initializer is neither definitive nor
100   /// unique.
101   ///
102   /// @c = global weak_odr SomeType* null - Initializer is definitive, but not
103   /// unique.
104   inline bool hasDefinitiveInitializer() const {
105     return hasInitializer() &&
106       // The initializer of a global variable with weak linkage may change at
107       // link time.
108       !mayBeOverridden();
109   }
110
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.
121       !isWeakForLinker();
122   }
123
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!
127   ///
128   inline const Constant *getInitializer() const {
129     assert(hasInitializer() && "GV doesn't have initializer!");
130     return static_cast<Constant*>(Op<0>().get());
131   }
132   inline Constant *getInitializer() {
133     assert(hasInitializer() && "GV doesn't have initializer!");
134     return static_cast<Constant*>(Op<0>().get());
135   }
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);
140
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.
144   ///
145   bool isConstant() const { return isConstantGlobal; }
146   void setConstant(bool Val) { isConstantGlobal = Val; }
147
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;
152   }
153   void setThreadLocalMode(ThreadLocalMode Val) { threadLocalMode = Val; }
154   ThreadLocalMode getThreadLocalMode() const {
155     return static_cast<ThreadLocalMode>(threadLocalMode);
156   }
157
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);
161
162   /// removeFromParent - This method unlinks 'this' from the containing module,
163   /// but does not delete it.
164   ///
165   virtual void removeFromParent();
166
167   /// eraseFromParent - This method unlinks 'this' from the containing module
168   /// and deletes it.
169   ///
170   virtual void eraseFromParent();
171
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);
175
176   // Methods for support type inquiry through isa, cast, and dyn_cast:
177   static inline bool classof(const Value *V) {
178     return V->getValueID() == Value::GlobalVariableVal;
179   }
180 };
181
182 template <>
183 struct OperandTraits<GlobalVariable> :
184   public OptionalOperandTraits<GlobalVariable> {
185 };
186
187 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value)
188
189 } // End llvm namespace
190
191 #endif