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 initial value, which is copied into the executables .data
16 // area. Global Constants are required to have initializers.
18 //===----------------------------------------------------------------------===//
20 #ifndef LLVM_IR_GLOBALVARIABLE_H
21 #define LLVM_IR_GLOBALVARIABLE_H
23 #include "llvm/ADT/Twine.h"
24 #include "llvm/ADT/ilist_node.h"
25 #include "llvm/IR/GlobalObject.h"
26 #include "llvm/IR/OperandTraits.h"
32 template<typename ValueSubClass, typename ItemParentClass>
33 class SymbolTableListTraits;
35 class GlobalVariable : public GlobalObject, public ilist_node<GlobalVariable> {
36 friend class SymbolTableListTraits<GlobalVariable, Module>;
37 void *operator new(size_t, unsigned) = delete;
38 void operator=(const GlobalVariable &) = delete;
39 GlobalVariable(const GlobalVariable &) = delete;
41 void setParent(Module *parent);
43 bool isConstantGlobal : 1; // Is this a global constant?
44 bool isExternallyInitializedConstant : 1; // Is this a global whose value
45 // can change from its initial
46 // value before global
47 // initializers are run?
50 // allocate space for exactly one operand
51 void *operator new(size_t s) {
52 return User::operator new(s, 1);
55 /// GlobalVariable ctor - If a parent module is specified, the global is
56 /// automatically inserted into the end of the specified modules global list.
57 GlobalVariable(Type *Ty, bool isConstant, LinkageTypes Linkage,
58 Constant *Initializer = nullptr, const Twine &Name = "",
59 ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0,
60 bool isExternallyInitialized = false);
61 /// GlobalVariable ctor - This creates a global and inserts it before the
62 /// specified other global.
63 GlobalVariable(Module &M, Type *Ty, bool isConstant,
64 LinkageTypes Linkage, Constant *Initializer,
65 const Twine &Name = "", GlobalVariable *InsertBefore = nullptr,
66 ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0,
67 bool isExternallyInitialized = false);
69 ~GlobalVariable() override {
70 // FIXME: needed by operator delete
71 setGlobalVariableNumOperands(1);
74 /// Provide fast operand accessors
75 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
77 /// Definitions have initializers, declarations don't.
79 inline bool hasInitializer() const { return !isDeclaration(); }
81 /// hasDefinitiveInitializer - Whether the global variable has an initializer,
82 /// and any other instances of the global (this can happen due to weak
83 /// linkage) are guaranteed to have the same initializer.
85 /// Note that if you want to transform a global, you must use
86 /// hasUniqueInitializer() instead, because of the *_odr linkage type.
90 /// @a = global SomeType* null - Initializer is both definitive and unique.
92 /// @b = global weak SomeType* null - Initializer is neither definitive nor
95 /// @c = global weak_odr SomeType* null - Initializer is definitive, but not
97 inline bool hasDefinitiveInitializer() const {
98 return hasInitializer() &&
99 // The initializer of a global variable with weak linkage may change at
101 !mayBeOverridden() &&
102 // The initializer of a global variable with the externally_initialized
103 // marker may change at runtime before C++ initializers are evaluated.
104 !isExternallyInitialized();
107 /// hasUniqueInitializer - Whether the global variable has an initializer, and
108 /// any changes made to the initializer will turn up in the final executable.
109 inline bool hasUniqueInitializer() const {
110 return hasInitializer() &&
111 // It's not safe to modify initializers of global variables with weak
112 // linkage, because the linker might choose to discard the initializer and
113 // use the initializer from another instance of the global variable
114 // instead. It is wrong to modify the initializer of a global variable
115 // with *_odr linkage because then different instances of the global may
116 // have different initializers, breaking the One Definition Rule.
117 !isWeakForLinker() &&
118 // It is not safe to modify initializers of global variables with the
119 // external_initializer marker since the value may be changed at runtime
120 // before C++ initializers are evaluated.
121 !isExternallyInitialized();
124 /// getInitializer - Return the initializer for this global variable. It is
125 /// illegal to call this method if the global is external, because we cannot
126 /// tell what the value is initialized to!
128 inline const Constant *getInitializer() const {
129 assert(hasInitializer() && "GV doesn't have initializer!");
130 return static_cast<Constant*>(Op<0>().get());
132 inline Constant *getInitializer() {
133 assert(hasInitializer() && "GV doesn't have initializer!");
134 return static_cast<Constant*>(Op<0>().get());
136 /// setInitializer - Sets the initializer for this global variable, removing
137 /// any existing initializer if InitVal==NULL. If this GV has type T*, the
138 /// initializer must have type T.
139 void setInitializer(Constant *InitVal);
141 /// If the value is a global constant, its value is immutable throughout the
142 /// runtime execution of the program. Assigning a value into the constant
143 /// leads to undefined behavior.
145 bool isConstant() const { return isConstantGlobal; }
146 void setConstant(bool Val) { isConstantGlobal = Val; }
148 bool isExternallyInitialized() const {
149 return isExternallyInitializedConstant;
151 void setExternallyInitialized(bool Val) {
152 isExternallyInitializedConstant = Val;
155 /// copyAttributesFrom - copy all additional attributes (those not needed to
156 /// create a GlobalVariable) from the GlobalVariable Src to this one.
157 void copyAttributesFrom(const GlobalValue *Src) override;
159 /// removeFromParent - This method unlinks 'this' from the containing module,
160 /// but does not delete it.
162 void removeFromParent() override;
164 /// eraseFromParent - This method unlinks 'this' from the containing module
167 void eraseFromParent() override;
169 /// Override Constant's implementation of this method so we can
170 /// replace constant initializers.
171 void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override;
173 // Methods for support type inquiry through isa, cast, and dyn_cast:
174 static inline bool classof(const Value *V) {
175 return V->getValueID() == Value::GlobalVariableVal;
180 struct OperandTraits<GlobalVariable> :
181 public OptionalOperandTraits<GlobalVariable> {
184 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value)