GlobalVariable.h revision 261991
1249259Sdim//===-- llvm/GlobalVariable.h - GlobalVariable class ------------*- C++ -*-===//
2249259Sdim//
3249259Sdim//                     The LLVM Compiler Infrastructure
4249259Sdim//
5249259Sdim// This file is distributed under the University of Illinois Open Source
6249259Sdim// License. See LICENSE.TXT for details.
7249259Sdim//
8249259Sdim//===----------------------------------------------------------------------===//
9249259Sdim//
10249259Sdim// This file contains the declaration of the GlobalVariable class, which
11249259Sdim// represents a single global variable (or constant) in the VM.
12249259Sdim//
13249259Sdim// Global variables are constant pointers that refer to hunks of space that are
14249259Sdim// allocated by either the VM, or by the linker in a static compiler.  A global
15249259Sdim// variable may have an initial value, which is copied into the executables .data
16249259Sdim// area.  Global Constants are required to have initializers.
17249259Sdim//
18249259Sdim//===----------------------------------------------------------------------===//
19249259Sdim
20249259Sdim#ifndef LLVM_IR_GLOBALVARIABLE_H
21249259Sdim#define LLVM_IR_GLOBALVARIABLE_H
22249259Sdim
23249259Sdim#include "llvm/ADT/Twine.h"
24249259Sdim#include "llvm/ADT/ilist_node.h"
25249259Sdim#include "llvm/IR/GlobalValue.h"
26249259Sdim#include "llvm/IR/OperandTraits.h"
27249259Sdim
28249259Sdimnamespace llvm {
29249259Sdim
30249259Sdimclass Module;
31249259Sdimclass Constant;
32249259Sdimtemplate<typename ValueSubClass, typename ItemParentClass>
33249259Sdim  class SymbolTableListTraits;
34249259Sdim
35249259Sdimclass GlobalVariable : public GlobalValue, public ilist_node<GlobalVariable> {
36249259Sdim  friend class SymbolTableListTraits<GlobalVariable, Module>;
37249259Sdim  void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
38249259Sdim  void operator=(const GlobalVariable &) LLVM_DELETED_FUNCTION;
39249259Sdim  GlobalVariable(const GlobalVariable &) LLVM_DELETED_FUNCTION;
40249259Sdim
41249259Sdim  void setParent(Module *parent);
42249259Sdim
43249259Sdim  bool isConstantGlobal : 1;                   // Is this a global constant?
44249259Sdim  unsigned threadLocalMode : 3;                // Is this symbol "Thread Local",
45249259Sdim                                               // if so, what is the desired
46249259Sdim                                               // model?
47249259Sdim  bool isExternallyInitializedConstant : 1;    // Is this a global whose value
48249259Sdim                                               // can change from its initial
49249259Sdim                                               // value before global
50249259Sdim                                               // initializers are run?
51249259Sdim
52249259Sdimpublic:
53249259Sdim  // allocate space for exactly one operand
54249259Sdim  void *operator new(size_t s) {
55249259Sdim    return User::operator new(s, 1);
56249259Sdim  }
57249259Sdim
58249259Sdim  enum ThreadLocalMode {
59249259Sdim    NotThreadLocal = 0,
60249259Sdim    GeneralDynamicTLSModel,
61249259Sdim    LocalDynamicTLSModel,
62249259Sdim    InitialExecTLSModel,
63249259Sdim    LocalExecTLSModel
64249259Sdim  };
65249259Sdim
66249259Sdim  /// GlobalVariable ctor - If a parent module is specified, the global is
67249259Sdim  /// automatically inserted into the end of the specified modules global list.
68249259Sdim  GlobalVariable(Type *Ty, bool isConstant, LinkageTypes Linkage,
69249259Sdim                 Constant *Initializer = 0, const Twine &Name = "",
70249259Sdim                 ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0,
71249259Sdim                 bool isExternallyInitialized = false);
72249259Sdim  /// GlobalVariable ctor - This creates a global and inserts it before the
73249259Sdim  /// specified other global.
74249259Sdim  GlobalVariable(Module &M, Type *Ty, bool isConstant,
75249259Sdim                 LinkageTypes Linkage, Constant *Initializer,
76249259Sdim                 const Twine &Name = "", GlobalVariable *InsertBefore = 0,
77249259Sdim                 ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0,
78249259Sdim                 bool isExternallyInitialized = false);
79249259Sdim
80249259Sdim  ~GlobalVariable() {
81249259Sdim    NumOperands = 1; // FIXME: needed by operator delete
82249259Sdim  }
83249259Sdim
84249259Sdim  /// Provide fast operand accessors
85249259Sdim  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
86249259Sdim
87261991Sdim  /// Definitions have initializers, declarations don't.
88249259Sdim  ///
89249259Sdim  inline bool hasInitializer() const { return !isDeclaration(); }
90249259Sdim
91249259Sdim  /// hasDefinitiveInitializer - Whether the global variable has an initializer,
92249259Sdim  /// and any other instances of the global (this can happen due to weak
93249259Sdim  /// linkage) are guaranteed to have the same initializer.
94249259Sdim  ///
95249259Sdim  /// Note that if you want to transform a global, you must use
96249259Sdim  /// hasUniqueInitializer() instead, because of the *_odr linkage type.
97249259Sdim  ///
98249259Sdim  /// Example:
99249259Sdim  ///
100249259Sdim  /// @a = global SomeType* null - Initializer is both definitive and unique.
101249259Sdim  ///
102249259Sdim  /// @b = global weak SomeType* null - Initializer is neither definitive nor
103249259Sdim  /// unique.
104249259Sdim  ///
105249259Sdim  /// @c = global weak_odr SomeType* null - Initializer is definitive, but not
106249259Sdim  /// unique.
107249259Sdim  inline bool hasDefinitiveInitializer() const {
108249259Sdim    return hasInitializer() &&
109249259Sdim      // The initializer of a global variable with weak linkage may change at
110249259Sdim      // link time.
111249259Sdim      !mayBeOverridden() &&
112249259Sdim      // The initializer of a global variable with the externally_initialized
113249259Sdim      // marker may change at runtime before C++ initializers are evaluated.
114249259Sdim      !isExternallyInitialized();
115249259Sdim  }
116249259Sdim
117249259Sdim  /// hasUniqueInitializer - Whether the global variable has an initializer, and
118249259Sdim  /// any changes made to the initializer will turn up in the final executable.
119249259Sdim  inline bool hasUniqueInitializer() const {
120249259Sdim    return hasInitializer() &&
121249259Sdim      // It's not safe to modify initializers of global variables with weak
122249259Sdim      // linkage, because the linker might choose to discard the initializer and
123249259Sdim      // use the initializer from another instance of the global variable
124249259Sdim      // instead. It is wrong to modify the initializer of a global variable
125249259Sdim      // with *_odr linkage because then different instances of the global may
126249259Sdim      // have different initializers, breaking the One Definition Rule.
127249259Sdim      !isWeakForLinker() &&
128249259Sdim      // It is not safe to modify initializers of global variables with the
129249259Sdim      // external_initializer marker since the value may be changed at runtime
130249259Sdim      // before C++ initializers are evaluated.
131249259Sdim      !isExternallyInitialized();
132249259Sdim  }
133249259Sdim
134249259Sdim  /// getInitializer - Return the initializer for this global variable.  It is
135249259Sdim  /// illegal to call this method if the global is external, because we cannot
136249259Sdim  /// tell what the value is initialized to!
137249259Sdim  ///
138249259Sdim  inline const Constant *getInitializer() const {
139249259Sdim    assert(hasInitializer() && "GV doesn't have initializer!");
140249259Sdim    return static_cast<Constant*>(Op<0>().get());
141249259Sdim  }
142249259Sdim  inline Constant *getInitializer() {
143249259Sdim    assert(hasInitializer() && "GV doesn't have initializer!");
144249259Sdim    return static_cast<Constant*>(Op<0>().get());
145249259Sdim  }
146249259Sdim  /// setInitializer - Sets the initializer for this global variable, removing
147249259Sdim  /// any existing initializer if InitVal==NULL.  If this GV has type T*, the
148249259Sdim  /// initializer must have type T.
149249259Sdim  void setInitializer(Constant *InitVal);
150249259Sdim
151249259Sdim  /// If the value is a global constant, its value is immutable throughout the
152249259Sdim  /// runtime execution of the program.  Assigning a value into the constant
153249259Sdim  /// leads to undefined behavior.
154249259Sdim  ///
155249259Sdim  bool isConstant() const { return isConstantGlobal; }
156249259Sdim  void setConstant(bool Val) { isConstantGlobal = Val; }
157249259Sdim
158249259Sdim  /// If the value is "Thread Local", its value isn't shared by the threads.
159249259Sdim  bool isThreadLocal() const { return threadLocalMode != NotThreadLocal; }
160249259Sdim  void setThreadLocal(bool Val) {
161249259Sdim    threadLocalMode = Val ? GeneralDynamicTLSModel : NotThreadLocal;
162249259Sdim  }
163249259Sdim  void setThreadLocalMode(ThreadLocalMode Val) { threadLocalMode = Val; }
164249259Sdim  ThreadLocalMode getThreadLocalMode() const {
165249259Sdim    return static_cast<ThreadLocalMode>(threadLocalMode);
166249259Sdim  }
167249259Sdim
168249259Sdim  bool isExternallyInitialized() const {
169249259Sdim    return isExternallyInitializedConstant;
170249259Sdim  }
171249259Sdim  void setExternallyInitialized(bool Val) {
172249259Sdim    isExternallyInitializedConstant = Val;
173249259Sdim  }
174249259Sdim
175249259Sdim  /// copyAttributesFrom - copy all additional attributes (those not needed to
176249259Sdim  /// create a GlobalVariable) from the GlobalVariable Src to this one.
177249259Sdim  void copyAttributesFrom(const GlobalValue *Src);
178249259Sdim
179249259Sdim  /// removeFromParent - This method unlinks 'this' from the containing module,
180249259Sdim  /// but does not delete it.
181249259Sdim  ///
182249259Sdim  virtual void removeFromParent();
183249259Sdim
184249259Sdim  /// eraseFromParent - This method unlinks 'this' from the containing module
185249259Sdim  /// and deletes it.
186249259Sdim  ///
187249259Sdim  virtual void eraseFromParent();
188249259Sdim
189249259Sdim  /// Override Constant's implementation of this method so we can
190249259Sdim  /// replace constant initializers.
191249259Sdim  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
192249259Sdim
193249259Sdim  // Methods for support type inquiry through isa, cast, and dyn_cast:
194249259Sdim  static inline bool classof(const Value *V) {
195249259Sdim    return V->getValueID() == Value::GlobalVariableVal;
196249259Sdim  }
197249259Sdim};
198249259Sdim
199249259Sdimtemplate <>
200249259Sdimstruct OperandTraits<GlobalVariable> :
201249259Sdim  public OptionalOperandTraits<GlobalVariable> {
202249259Sdim};
203249259Sdim
204249259SdimDEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value)
205249259Sdim
206249259Sdim} // End llvm namespace
207249259Sdim
208249259Sdim#endif
209