LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GlobalVariable.h
Go to the documentation of this file.
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> {
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 
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.
69  Constant *Initializer = 0, const Twine &Name = "",
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,
78  bool isExternallyInitialized = false);
79 
81  NumOperands = 1; // FIXME: needed by operator delete
82  }
83 
84  /// Provide fast operand accessors
86 
87  /// Definitions have initializers, declarations don't.
88  ///
89  inline bool hasInitializer() const { return !isDeclaration(); }
90 
91  /// hasDefinitiveInitializer - Whether the global variable has an initializer,
92  /// and any other instances of the global (this can happen due to weak
93  /// linkage) are guaranteed to have the same initializer.
94  ///
95  /// Note that if you want to transform a global, you must use
96  /// hasUniqueInitializer() instead, because of the *_odr linkage type.
97  ///
98  /// Example:
99  ///
100  /// @a = global SomeType* null - Initializer is both definitive and unique.
101  ///
102  /// @b = global weak SomeType* null - Initializer is neither definitive nor
103  /// unique.
104  ///
105  /// @c = global weak_odr SomeType* null - Initializer is definitive, but not
106  /// unique.
107  inline bool hasDefinitiveInitializer() const {
108  return hasInitializer() &&
109  // The initializer of a global variable with weak linkage may change at
110  // link time.
111  !mayBeOverridden() &&
112  // The initializer of a global variable with the externally_initialized
113  // marker may change at runtime before C++ initializers are evaluated.
115  }
116 
117  /// hasUniqueInitializer - Whether the global variable has an initializer, and
118  /// any changes made to the initializer will turn up in the final executable.
119  inline bool hasUniqueInitializer() const {
120  return hasInitializer() &&
121  // It's not safe to modify initializers of global variables with weak
122  // linkage, because the linker might choose to discard the initializer and
123  // use the initializer from another instance of the global variable
124  // instead. It is wrong to modify the initializer of a global variable
125  // with *_odr linkage because then different instances of the global may
126  // have different initializers, breaking the One Definition Rule.
127  !isWeakForLinker() &&
128  // It is not safe to modify initializers of global variables with the
129  // external_initializer marker since the value may be changed at runtime
130  // before C++ initializers are evaluated.
132  }
133 
134  /// getInitializer - Return the initializer for this global variable. It is
135  /// illegal to call this method if the global is external, because we cannot
136  /// tell what the value is initialized to!
137  ///
138  inline const Constant *getInitializer() const {
139  assert(hasInitializer() && "GV doesn't have initializer!");
140  return static_cast<Constant*>(Op<0>().get());
141  }
143  assert(hasInitializer() && "GV doesn't have initializer!");
144  return static_cast<Constant*>(Op<0>().get());
145  }
146  /// setInitializer - Sets the initializer for this global variable, removing
147  /// any existing initializer if InitVal==NULL. If this GV has type T*, the
148  /// initializer must have type T.
149  void setInitializer(Constant *InitVal);
150 
151  /// If the value is a global constant, its value is immutable throughout the
152  /// runtime execution of the program. Assigning a value into the constant
153  /// leads to undefined behavior.
154  ///
155  bool isConstant() const { return isConstantGlobal; }
156  void setConstant(bool Val) { isConstantGlobal = Val; }
157 
158  /// If the value is "Thread Local", its value isn't shared by the threads.
159  bool isThreadLocal() const { return threadLocalMode != NotThreadLocal; }
160  void setThreadLocal(bool Val) {
161  threadLocalMode = Val ? GeneralDynamicTLSModel : NotThreadLocal;
162  }
163  void setThreadLocalMode(ThreadLocalMode Val) { threadLocalMode = Val; }
165  return static_cast<ThreadLocalMode>(threadLocalMode);
166  }
167 
168  bool isExternallyInitialized() const {
169  return isExternallyInitializedConstant;
170  }
171  void setExternallyInitialized(bool Val) {
172  isExternallyInitializedConstant = Val;
173  }
174 
175  /// copyAttributesFrom - copy all additional attributes (those not needed to
176  /// create a GlobalVariable) from the GlobalVariable Src to this one.
177  void copyAttributesFrom(const GlobalValue *Src);
178 
179  /// removeFromParent - This method unlinks 'this' from the containing module,
180  /// but does not delete it.
181  ///
182  virtual void removeFromParent();
183 
184  /// eraseFromParent - This method unlinks 'this' from the containing module
185  /// and deletes it.
186  ///
187  virtual void eraseFromParent();
188 
189  /// Override Constant's implementation of this method so we can
190  /// replace constant initializers.
191  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
192 
193  // Methods for support type inquiry through isa, cast, and dyn_cast:
194  static inline bool classof(const Value *V) {
195  return V->getValueID() == Value::GlobalVariableVal;
196  }
197 };
198 
199 template <>
201  public OptionalOperandTraits<GlobalVariable> {
202 };
203 
205 
206 } // End llvm namespace
207 
208 #endif
ThreadLocalMode getThreadLocalMode() const
static bool classof(const Value *V)
The main container class for the LLVM Intermediate Representation.
Definition: Module.h:112
const Constant * getInitializer() const
unsigned NumOperands
Definition: User.h:49
void setInitializer(Constant *InitVal)
Definition: Globals.cpp:166
bool isThreadLocal() const
If the value is "Thread Local", its value isn't shared by the threads.
Definition: Use.h:60
bool hasUniqueInitializer() const
#define DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CLASS, VALUECLASS)
Macro for generating out-of-class operand accessor definitions.
LinkageTypes Linkage
Definition: GlobalValue.h:68
void setThreadLocal(bool Val)
bool hasDefinitiveInitializer() const
bool isWeakForLinker() const
Definition: GlobalValue.h:226
bool isExternallyInitialized() const
LLVM Constant Representation.
Definition: Constant.h:41
virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U)
Definition: Globals.cpp:146
void setExternallyInitialized(bool Val)
virtual void eraseFromParent()
Definition: Globals.cpp:142
unsigned getValueID() const
Definition: Value.h:233
void setConstant(bool Val)
virtual void removeFromParent()
Definition: Globals.cpp:138
void setThreadLocalMode(ThreadLocalMode Val)
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)
Provide fast operand accessors.
void copyAttributesFrom(const GlobalValue *Src)
Definition: Globals.cpp:183
AddressSpace
Definition: NVPTXBaseInfo.h:22
#define LLVM_DELETED_FUNCTION
Definition: Compiler.h:137
bool hasInitializer() const
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:33
bool isConstant() const
bool isDeclaration() const
Definition: Globals.cpp:66
LLVM Value Representation.
Definition: Value.h:66
bool mayBeOverridden() const
Definition: GlobalValue.h:224
Constant * getInitializer()