1 //===-- llvm/GlobalVariable.h - GlobalVariable class ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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"
30 template<typename ValueSubClass, typename ItemParentClass>
31 class SymbolTableListTraits;
33 class GlobalVariable : public GlobalValue {
34 friend class SymbolTableListTraits<GlobalVariable, Module>;
35 void operator=(const GlobalVariable &); // Do not implement
36 GlobalVariable(const GlobalVariable &); // Do not implement
38 void setParent(Module *parent);
40 GlobalVariable *Prev, *Next;
41 void setNext(GlobalVariable *N) { Next = N; }
42 void setPrev(GlobalVariable *N) { Prev = N; }
44 bool isConstantGlobal : 1; // Is this a global constant?
45 bool isThreadLocalSymbol : 1; // Is this symbol "Thread Local"?
49 static void destroyThis(GlobalVariable*v) {
50 GlobalValue::destroyThis(v);
54 /// GlobalVariable ctor - If a parent module is specified, the global is
55 /// automatically inserted into the end of the specified modules global list.
56 GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
57 Constant *Initializer = 0, const std::string &Name = "",
58 Module *Parent = 0, bool ThreadLocal = false);
59 /// GlobalVariable ctor - This creates a global and inserts it before the
60 /// specified other global.
61 GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
62 Constant *Initializer, const std::string &Name,
63 GlobalVariable *InsertBefore, bool ThreadLocal = false);
65 /// isDeclaration - Is this global variable lacking an initializer? If so,
66 /// the global variable is defined in some other translation unit, and is thus
67 /// only a declaration here.
68 virtual bool isDeclaration() const { return getNumOperands() == 0; }
70 /// hasInitializer - Unless a global variable isExternal(), it has an
71 /// initializer. The initializer for the global variable/constant is held by
72 /// Initializer if an initializer is specified.
74 inline bool hasInitializer() const { return !isDeclaration(); }
76 /// getInitializer - Return the initializer for this global variable. It is
77 /// illegal to call this method if the global is external, because we cannot
78 /// tell what the value is initialized to!
80 inline Constant *getInitializer() const {
81 assert(hasInitializer() && "GV doesn't have initializer!");
82 return reinterpret_cast<Constant*>(Initializer.get());
84 inline Constant *getInitializer() {
85 assert(hasInitializer() && "GV doesn't have initializer!");
86 return reinterpret_cast<Constant*>(Initializer.get());
88 inline void setInitializer(Constant *CPV) {
90 if (hasInitializer()) {
95 if (!hasInitializer())
101 /// If the value is a global constant, its value is immutable throughout the
102 /// runtime execution of the program. Assigning a value into the constant
103 /// leads to undefined behavior.
105 bool isConstant() const { return isConstantGlobal; }
106 void setConstant(bool Value) { isConstantGlobal = Value; }
108 /// If the value is "Thread Local", its value isn't shared by the threads.
109 bool isThreadLocal() const { return isThreadLocalSymbol; }
110 void setThreadLocal(bool Value) { isThreadLocalSymbol = Value; }
112 /// removeFromParent - This method unlinks 'this' from the containing module,
113 /// but does not delete it.
115 void removeFromParent();
117 /// eraseFromParent - This method unlinks 'this' from the containing module
120 void eraseFromParent();
122 /// Override Constant's implementation of this method so we can
123 /// replace constant initializers.
124 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
126 virtual void print(std::ostream &OS) const;
127 void print(std::ostream *OS) const { if (OS) print(*OS); }
129 // Methods for support type inquiry through isa, cast, and dyn_cast:
130 static inline bool classof(const GlobalVariable *) { return true; }
131 static inline bool classof(const Value *V) {
132 return V->getValueID() == Value::GlobalVariableVal;
135 // getNext/Prev - Return the next or previous global variable in the list.
136 GlobalVariable *getNext() { return Next; }
137 const GlobalVariable *getNext() const { return Next; }
138 GlobalVariable *getPrev() { return Prev; }
139 const GlobalVariable *getPrev() const { return Prev; }
142 } // End llvm namespace