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/PointerUnion.h"
24 #include "llvm/ADT/Twine.h"
25 #include "llvm/ADT/ilist_node.h"
26 #include "llvm/IR/Attributes.h"
27 #include "llvm/IR/GlobalObject.h"
28 #include "llvm/IR/OperandTraits.h"
29 #include "llvm/IR/Value.h"
38 template <typename ValueSubClass> class SymbolTableListTraits;
39 class DIGlobalVariable;
40 class DIGlobalVariableExpression;
42 class GlobalVariable : public GlobalObject, public ilist_node<GlobalVariable> {
43 friend class SymbolTableListTraits<GlobalVariable>;
46 bool isConstantGlobal : 1; // Is this a global constant?
47 bool isExternallyInitializedConstant : 1; // Is this a global whose value
48 // can change from its initial
49 // value before global
50 // initializers are run?
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);
66 GlobalVariable(const GlobalVariable &) = delete;
67 GlobalVariable &operator=(const GlobalVariable &) = delete;
72 // FIXME: needed by operator delete
73 setGlobalVariableNumOperands(1);
76 // allocate space for exactly one operand
77 void *operator new(size_t s) {
78 return User::operator new(s, 1);
81 /// Provide fast operand accessors
82 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
84 /// Definitions have initializers, declarations don't.
86 inline bool hasInitializer() const { return !isDeclaration(); }
88 /// hasDefinitiveInitializer - Whether the global variable has an initializer,
89 /// and any other instances of the global (this can happen due to weak
90 /// linkage) are guaranteed to have the same initializer.
92 /// Note that if you want to transform a global, you must use
93 /// hasUniqueInitializer() instead, because of the *_odr linkage type.
97 /// @a = global SomeType* null - Initializer is both definitive and unique.
99 /// @b = global weak SomeType* null - Initializer is neither definitive nor
102 /// @c = global weak_odr SomeType* null - Initializer is definitive, but not
104 inline bool hasDefinitiveInitializer() const {
105 return hasInitializer() &&
106 // The initializer of a global variable may change to something arbitrary
109 // The initializer of a global variable with the externally_initialized
110 // marker may change at runtime before C++ initializers are evaluated.
111 !isExternallyInitialized();
114 /// hasUniqueInitializer - Whether the global variable has an initializer, and
115 /// any changes made to the initializer will turn up in the final executable.
116 inline bool hasUniqueInitializer() const {
118 // We need to be sure this is the definition that will actually be used
119 isStrongDefinitionForLinker() &&
120 // It is not safe to modify initializers of global variables with the
121 // external_initializer marker since the value may be changed at runtime
122 // before C++ initializers are evaluated.
123 !isExternallyInitialized();
126 /// getInitializer - Return the initializer for this global variable. It is
127 /// illegal to call this method if the global is external, because we cannot
128 /// tell what the value is initialized to!
130 inline const Constant *getInitializer() const {
131 assert(hasInitializer() && "GV doesn't have initializer!");
132 return static_cast<Constant*>(Op<0>().get());
134 inline Constant *getInitializer() {
135 assert(hasInitializer() && "GV doesn't have initializer!");
136 return static_cast<Constant*>(Op<0>().get());
138 /// setInitializer - Sets the initializer for this global variable, removing
139 /// any existing initializer if InitVal==NULL. If this GV has type T*, the
140 /// initializer must have type T.
141 void setInitializer(Constant *InitVal);
143 /// If the value is a global constant, its value is immutable throughout the
144 /// runtime execution of the program. Assigning a value into the constant
145 /// leads to undefined behavior.
147 bool isConstant() const { return isConstantGlobal; }
148 void setConstant(bool Val) { isConstantGlobal = Val; }
150 bool isExternallyInitialized() const {
151 return isExternallyInitializedConstant;
153 void setExternallyInitialized(bool Val) {
154 isExternallyInitializedConstant = Val;
157 /// copyAttributesFrom - copy all additional attributes (those not needed to
158 /// create a GlobalVariable) from the GlobalVariable Src to this one.
159 void copyAttributesFrom(const GlobalVariable *Src);
161 /// removeFromParent - This method unlinks 'this' from the containing module,
162 /// but does not delete it.
164 void removeFromParent();
166 /// eraseFromParent - This method unlinks 'this' from the containing module
169 void eraseFromParent();
171 /// Drop all references in preparation to destroy the GlobalVariable. This
172 /// drops not only the reference to the initializer but also to any metadata.
173 void dropAllReferences();
175 /// Attach a DIGlobalVariableExpression.
176 void addDebugInfo(DIGlobalVariableExpression *GV);
178 /// Fill the vector with all debug info attachements.
179 void getDebugInfo(SmallVectorImpl<DIGlobalVariableExpression *> &GVs) const;
181 /// Add attribute to this global.
182 void addAttribute(Attribute::AttrKind Kind) {
183 Attrs = Attrs.addAttribute(getContext(), Kind);
186 /// Add attribute to this global.
187 void addAttribute(StringRef Kind, StringRef Val = StringRef()) {
188 Attrs = Attrs.addAttribute(getContext(), Kind, Val);
191 /// Return true if the attribute exists.
192 bool hasAttribute(Attribute::AttrKind Kind) const {
193 return Attrs.hasAttribute(Kind);
196 /// Return true if the attribute exists.
197 bool hasAttribute(StringRef Kind) const {
198 return Attrs.hasAttribute(Kind);
201 /// Return true if any attributes exist.
202 bool hasAttributes() const {
203 return Attrs.hasAttributes();
206 /// Return the attribute object.
207 Attribute getAttribute(Attribute::AttrKind Kind) const {
208 return Attrs.getAttribute(Kind);
211 /// Return the attribute object.
212 Attribute getAttribute(StringRef Kind) const {
213 return Attrs.getAttribute(Kind);
216 /// Return the attribute set for this global
217 AttributeSet getAttributes() const {
221 /// Return attribute set as list with index.
222 /// FIXME: This may not be required once ValueEnumerators
223 /// in bitcode-writer can enumerate attribute-set.
224 AttributeList getAttributesAsList(unsigned index) const {
225 if (!hasAttributes())
226 return AttributeList();
227 std::pair<unsigned, AttributeSet> AS[1] = {{index, Attrs}};
228 return AttributeList::get(getContext(), AS);
231 /// Set attribute list for this global
232 void setAttributes(AttributeSet A) {
236 /// Check if section name is present
237 bool hasImplicitSection() const {
238 return getAttributes().hasAttribute("bss-section") ||
239 getAttributes().hasAttribute("data-section") ||
240 getAttributes().hasAttribute("rodata-section");
243 // Methods for support type inquiry through isa, cast, and dyn_cast:
244 static bool classof(const Value *V) {
245 return V->getValueID() == Value::GlobalVariableVal;
250 struct OperandTraits<GlobalVariable> :
251 public OptionalOperandTraits<GlobalVariable> {
254 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value)
256 } // end namespace llvm
258 #endif // LLVM_IR_GLOBALVARIABLE_H