]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/llvm/include/llvm/IR/GlobalVariable.h
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / llvm / include / llvm / IR / 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_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/GlobalValue.h"
26 #include "llvm/IR/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
46                                                // model?
47   bool isExternallyInitializedConstant : 1;    // Is this a global whose value
48                                                // can change from its initial
49                                                // value before global
50                                                // initializers are run?
51
52 public:
53   // allocate space for exactly one operand
54   void *operator new(size_t s) {
55     return User::operator new(s, 1);
56   }
57
58   enum ThreadLocalMode {
59     NotThreadLocal = 0,
60     GeneralDynamicTLSModel,
61     LocalDynamicTLSModel,
62     InitialExecTLSModel,
63     LocalExecTLSModel
64   };
65
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);
79
80   ~GlobalVariable() {
81     NumOperands = 1; // FIXME: needed by operator delete
82   }
83
84   /// Provide fast operand accessors
85   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
86
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.
90   ///
91   inline bool hasInitializer() const { return !isDeclaration(); }
92
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.
96   ///
97   /// Note that if you want to transform a global, you must use
98   /// hasUniqueInitializer() instead, because of the *_odr linkage type.
99   ///
100   /// Example:
101   ///
102   /// @a = global SomeType* null - Initializer is both definitive and unique.
103   ///
104   /// @b = global weak SomeType* null - Initializer is neither definitive nor
105   /// unique.
106   ///
107   /// @c = global weak_odr SomeType* null - Initializer is definitive, but not
108   /// unique.
109   inline bool hasDefinitiveInitializer() const {
110     return hasInitializer() &&
111       // The initializer of a global variable with weak linkage may change at
112       // link time.
113       !mayBeOverridden() &&
114       // The initializer of a global variable with the externally_initialized
115       // marker may change at runtime before C++ initializers are evaluated.
116       !isExternallyInitialized();
117   }
118
119   /// hasUniqueInitializer - Whether the global variable has an initializer, and
120   /// any changes made to the initializer will turn up in the final executable.
121   inline bool hasUniqueInitializer() const {
122     return hasInitializer() &&
123       // It's not safe to modify initializers of global variables with weak
124       // linkage, because the linker might choose to discard the initializer and
125       // use the initializer from another instance of the global variable
126       // instead. It is wrong to modify the initializer of a global variable
127       // with *_odr linkage because then different instances of the global may
128       // have different initializers, breaking the One Definition Rule.
129       !isWeakForLinker() &&
130       // It is not safe to modify initializers of global variables with the
131       // external_initializer marker since the value may be changed at runtime
132       // before C++ initializers are evaluated.
133       !isExternallyInitialized();
134   }
135
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!
139   ///
140   inline const Constant *getInitializer() const {
141     assert(hasInitializer() && "GV doesn't have initializer!");
142     return static_cast<Constant*>(Op<0>().get());
143   }
144   inline Constant *getInitializer() {
145     assert(hasInitializer() && "GV doesn't have initializer!");
146     return static_cast<Constant*>(Op<0>().get());
147   }
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);
152
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.
156   ///
157   bool isConstant() const { return isConstantGlobal; }
158   void setConstant(bool Val) { isConstantGlobal = Val; }
159
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;
164   }
165   void setThreadLocalMode(ThreadLocalMode Val) { threadLocalMode = Val; }
166   ThreadLocalMode getThreadLocalMode() const {
167     return static_cast<ThreadLocalMode>(threadLocalMode);
168   }
169
170   bool isExternallyInitialized() const {
171     return isExternallyInitializedConstant;
172   }
173   void setExternallyInitialized(bool Val) {
174     isExternallyInitializedConstant = Val;
175   }
176
177   /// copyAttributesFrom - copy all additional attributes (those not needed to
178   /// create a GlobalVariable) from the GlobalVariable Src to this one.
179   void copyAttributesFrom(const GlobalValue *Src);
180
181   /// removeFromParent - This method unlinks 'this' from the containing module,
182   /// but does not delete it.
183   ///
184   virtual void removeFromParent();
185
186   /// eraseFromParent - This method unlinks 'this' from the containing module
187   /// and deletes it.
188   ///
189   virtual void eraseFromParent();
190
191   /// Override Constant's implementation of this method so we can
192   /// replace constant initializers.
193   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
194
195   // Methods for support type inquiry through isa, cast, and dyn_cast:
196   static inline bool classof(const Value *V) {
197     return V->getValueID() == Value::GlobalVariableVal;
198   }
199 };
200
201 template <>
202 struct OperandTraits<GlobalVariable> :
203   public OptionalOperandTraits<GlobalVariable> {
204 };
205
206 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value)
207
208 } // End llvm namespace
209
210 #endif