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> class SymbolTableListTraits;
34 class GlobalVariable : public GlobalObject, public ilist_node<GlobalVariable> {
35 friend class SymbolTableListTraits<GlobalVariable>;
36 void *operator new(size_t, unsigned) = delete;
37 void operator=(const GlobalVariable &) = delete;
38 GlobalVariable(const GlobalVariable &) = delete;
40 void setParent(Module *parent);
42 bool isConstantGlobal : 1; // Is this a global constant?
43 bool isExternallyInitializedConstant : 1; // Is this a global whose value
44 // can change from its initial
45 // value before global
46 // initializers are run?
48 // allocate space for exactly one operand
49 void *operator new(size_t s) {
50 return User::operator new(s, 1);
53 /// GlobalVariable ctor - If a parent module is specified, the global is
54 /// automatically inserted into the end of the specified modules global list.
55 GlobalVariable(Type *Ty, bool isConstant, LinkageTypes Linkage,
56 Constant *Initializer = nullptr, const Twine &Name = "",
57 ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0,
58 bool isExternallyInitialized = false);
59 /// GlobalVariable ctor - This creates a global and inserts it before the
60 /// specified other global.
61 GlobalVariable(Module &M, Type *Ty, bool isConstant,
62 LinkageTypes Linkage, Constant *Initializer,
63 const Twine &Name = "", GlobalVariable *InsertBefore = nullptr,
64 ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0,
65 bool isExternallyInitialized = false);
67 ~GlobalVariable() override {
68 // FIXME: needed by operator delete
69 setGlobalVariableNumOperands(1);
72 /// Provide fast operand accessors
73 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
75 /// Definitions have initializers, declarations don't.
77 inline bool hasInitializer() const { return !isDeclaration(); }
79 /// hasDefinitiveInitializer - Whether the global variable has an initializer,
80 /// and any other instances of the global (this can happen due to weak
81 /// linkage) are guaranteed to have the same initializer.
83 /// Note that if you want to transform a global, you must use
84 /// hasUniqueInitializer() instead, because of the *_odr linkage type.
88 /// @a = global SomeType* null - Initializer is both definitive and unique.
90 /// @b = global weak SomeType* null - Initializer is neither definitive nor
93 /// @c = global weak_odr SomeType* null - Initializer is definitive, but not
95 inline bool hasDefinitiveInitializer() const {
96 return hasInitializer() &&
97 // The initializer of a global variable with weak linkage may change at
100 // The initializer of a global variable with the externally_initialized
101 // marker may change at runtime before C++ initializers are evaluated.
102 !isExternallyInitialized();
105 /// hasUniqueInitializer - Whether the global variable has an initializer, and
106 /// any changes made to the initializer will turn up in the final executable.
107 inline bool hasUniqueInitializer() const {
109 // We need to be sure this is the definition that will actually be used
110 isStrongDefinitionForLinker() &&
111 // It is not safe to modify initializers of global variables with the
112 // external_initializer marker since the value may be changed at runtime
113 // before C++ initializers are evaluated.
114 !isExternallyInitialized();
117 /// getInitializer - Return the initializer for this global variable. It is
118 /// illegal to call this method if the global is external, because we cannot
119 /// tell what the value is initialized to!
121 inline const Constant *getInitializer() const {
122 assert(hasInitializer() && "GV doesn't have initializer!");
123 return static_cast<Constant*>(Op<0>().get());
125 inline Constant *getInitializer() {
126 assert(hasInitializer() && "GV doesn't have initializer!");
127 return static_cast<Constant*>(Op<0>().get());
129 /// setInitializer - Sets the initializer for this global variable, removing
130 /// any existing initializer if InitVal==NULL. If this GV has type T*, the
131 /// initializer must have type T.
132 void setInitializer(Constant *InitVal);
134 /// If the value is a global constant, its value is immutable throughout the
135 /// runtime execution of the program. Assigning a value into the constant
136 /// leads to undefined behavior.
138 bool isConstant() const { return isConstantGlobal; }
139 void setConstant(bool Val) { isConstantGlobal = Val; }
141 bool isExternallyInitialized() const {
142 return isExternallyInitializedConstant;
144 void setExternallyInitialized(bool Val) {
145 isExternallyInitializedConstant = Val;
148 /// copyAttributesFrom - copy all additional attributes (those not needed to
149 /// create a GlobalVariable) from the GlobalVariable Src to this one.
150 void copyAttributesFrom(const GlobalValue *Src) override;
152 /// removeFromParent - This method unlinks 'this' from the containing module,
153 /// but does not delete it.
155 void removeFromParent() override;
157 /// eraseFromParent - This method unlinks 'this' from the containing module
160 void eraseFromParent() override;
162 // Methods for support type inquiry through isa, cast, and dyn_cast:
163 static inline bool classof(const Value *V) {
164 return V->getValueID() == Value::GlobalVariableVal;
169 struct OperandTraits<GlobalVariable> :
170 public OptionalOperandTraits<GlobalVariable> {
173 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value)
175 } // End llvm namespace