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 intial value, which is copied into the executables .data
16 // area. Global Constants are required to have initializers.
18 //===----------------------------------------------------------------------===//
20 #ifndef LLVM_GLOBAL_VARIABLE_H
21 #define LLVM_GLOBAL_VARIABLE_H
23 #include "llvm/GlobalValue.h"
24 #include "llvm/OperandTraits.h"
25 #include "llvm/ADT/ilist_node.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); // Do not implement
38 void operator=(const GlobalVariable &); // Do not implement
39 GlobalVariable(const GlobalVariable &); // Do not implement
41 void setParent(Module *parent);
43 bool isConstantGlobal : 1; // Is this a global constant?
44 bool isThreadLocalSymbol : 1; // Is this symbol "Thread Local"?
47 // allocate space for exactly one operand
48 void *operator new(size_t s) {
49 return User::operator new(s, 1);
51 /// GlobalVariable ctor - If a parent module is specified, the global is
52 /// automatically inserted into the end of the specified modules global list.
53 GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
54 Constant *Initializer = 0, const std::string &Name = "",
55 Module *Parent = 0, bool ThreadLocal = false,
56 unsigned AddressSpace = 0);
57 /// GlobalVariable ctor - This creates a global and inserts it before the
58 /// specified other global.
59 GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
60 Constant *Initializer, const std::string &Name,
61 GlobalVariable *InsertBefore, bool ThreadLocal = false,
62 unsigned AddressSpace = 0);
65 NumOperands = 1; // FIXME: needed by operator delete
68 /// Provide fast operand accessors
69 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
71 /// isDeclaration - Is this global variable lacking an initializer? If so,
72 /// the global variable is defined in some other translation unit, and is thus
73 /// only a declaration here.
74 virtual bool isDeclaration() const { return getNumOperands() == 0; }
76 /// hasInitializer - Unless a global variable isExternal(), it has an
77 /// initializer. The initializer for the global variable/constant is held by
78 /// Initializer if an initializer is specified.
80 inline bool hasInitializer() const { return !isDeclaration(); }
82 /// getInitializer - Return the initializer for this global variable. It is
83 /// illegal to call this method if the global is external, because we cannot
84 /// tell what the value is initialized to!
86 inline /*const FIXME*/ Constant *getInitializer() const {
87 assert(hasInitializer() && "GV doesn't have initializer!");
88 return static_cast<Constant*>(Op<0>().get());
90 inline Constant *getInitializer() {
91 assert(hasInitializer() && "GV doesn't have initializer!");
92 return static_cast<Constant*>(Op<0>().get());
94 inline void setInitializer(Constant *CPV) {
96 if (hasInitializer()) {
101 if (!hasInitializer())
107 /// If the value is a global constant, its value is immutable throughout the
108 /// runtime execution of the program. Assigning a value into the constant
109 /// leads to undefined behavior.
111 bool isConstant() const { return isConstantGlobal; }
112 void setConstant(bool Val) { isConstantGlobal = Val; }
114 /// If the value is "Thread Local", its value isn't shared by the threads.
115 bool isThreadLocal() const { return isThreadLocalSymbol; }
116 void setThreadLocal(bool Val) { isThreadLocalSymbol = Val; }
118 /// copyAttributesFrom - copy all additional attributes (those not needed to
119 /// create a GlobalVariable) from the GlobalVariable Src to this one.
120 void copyAttributesFrom(const GlobalValue *Src);
122 /// removeFromParent - This method unlinks 'this' from the containing module,
123 /// but does not delete it.
125 void removeFromParent();
127 /// eraseFromParent - This method unlinks 'this' from the containing module
130 void eraseFromParent();
132 /// Override Constant's implementation of this method so we can
133 /// replace constant initializers.
134 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
136 virtual void print(std::ostream &OS) const;
137 void print(std::ostream *OS) const { if (OS) print(*OS); }
139 // Methods for support type inquiry through isa, cast, and dyn_cast:
140 static inline bool classof(const GlobalVariable *) { return true; }
141 static inline bool classof(const Value *V) {
142 return V->getValueID() == Value::GlobalVariableVal;
147 struct OperandTraits<GlobalVariable> : OptionalOperandTraits<> {
150 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value)
152 } // End llvm namespace