1 //===-- llvm/GlobalVariable.h - GlobalVariable class ------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file contains the declaration of the GlobalVariable class, which
10 // represents a single global variable (or constant) in the VM.
12 // Global variables are constant pointers that refer to hunks of space that are
13 // allocated by either the VM, or by the linker in a static compiler. A global
14 // variable may have an initial value, which is copied into the executables .data
15 // area. Global Constants are required to have initializers.
17 //===----------------------------------------------------------------------===//
19 #ifndef LLVM_IR_GLOBALVARIABLE_H
20 #define LLVM_IR_GLOBALVARIABLE_H
22 #include "llvm/ADT/PointerUnion.h"
23 #include "llvm/ADT/Twine.h"
24 #include "llvm/ADT/ilist_node.h"
25 #include "llvm/IR/Attributes.h"
26 #include "llvm/IR/GlobalObject.h"
27 #include "llvm/IR/OperandTraits.h"
28 #include "llvm/IR/Value.h"
37 template <typename ValueSubClass
> class SymbolTableListTraits
;
38 class DIGlobalVariable
;
39 class DIGlobalVariableExpression
;
41 class GlobalVariable
: public GlobalObject
, public ilist_node
<GlobalVariable
> {
42 friend class SymbolTableListTraits
<GlobalVariable
>;
45 bool isConstantGlobal
: 1; // Is this a global constant?
46 bool isExternallyInitializedConstant
: 1; // Is this a global whose value
47 // can change from its initial
48 // value before global
49 // initializers are run?
52 /// GlobalVariable ctor - If a parent module is specified, the global is
53 /// automatically inserted into the end of the specified modules global list.
54 GlobalVariable(Type
*Ty
, bool isConstant
, LinkageTypes Linkage
,
55 Constant
*Initializer
= nullptr, const Twine
&Name
= "",
56 ThreadLocalMode
= NotThreadLocal
, unsigned AddressSpace
= 0,
57 bool isExternallyInitialized
= false);
58 /// GlobalVariable ctor - This creates a global and inserts it before the
59 /// specified other global.
60 GlobalVariable(Module
&M
, Type
*Ty
, bool isConstant
,
61 LinkageTypes Linkage
, Constant
*Initializer
,
62 const Twine
&Name
= "", GlobalVariable
*InsertBefore
= nullptr,
63 ThreadLocalMode
= NotThreadLocal
, unsigned AddressSpace
= 0,
64 bool isExternallyInitialized
= false);
65 GlobalVariable(const GlobalVariable
&) = delete;
66 GlobalVariable
&operator=(const GlobalVariable
&) = delete;
72 // allocate space for exactly one operand
73 void *operator new(size_t s
) {
74 return User::operator new(s
, 1);
77 // delete space for exactly one operand as created in the corresponding new operator
78 void operator delete(void *ptr
){
79 assert(ptr
!= nullptr && "must not be nullptr");
80 User
*Obj
= static_cast<User
*>(ptr
);
81 // Number of operands can be set to 0 after construction and initialization. Make sure
82 // that number of operands is reset to 1, as this is needed in User::operator delete
83 Obj
->setGlobalVariableNumOperands(1);
84 User::operator delete(Obj
);
87 /// Provide fast operand accessors
88 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value
);
90 /// Definitions have initializers, declarations don't.
92 inline bool hasInitializer() const { return !isDeclaration(); }
94 /// hasDefinitiveInitializer - Whether the global variable has an initializer,
95 /// and any other instances of the global (this can happen due to weak
96 /// linkage) are guaranteed to have the same initializer.
98 /// Note that if you want to transform a global, you must use
99 /// hasUniqueInitializer() instead, because of the *_odr linkage type.
103 /// @a = global SomeType* null - Initializer is both definitive and unique.
105 /// @b = global weak SomeType* null - Initializer is neither definitive nor
108 /// @c = global weak_odr SomeType* null - Initializer is definitive, but not
110 inline bool hasDefinitiveInitializer() const {
111 return hasInitializer() &&
112 // The initializer of a global variable may change to something arbitrary
115 // The initializer of a global variable with the externally_initialized
116 // marker may change at runtime before C++ initializers are evaluated.
117 !isExternallyInitialized();
120 /// hasUniqueInitializer - Whether the global variable has an initializer, and
121 /// any changes made to the initializer will turn up in the final executable.
122 inline bool hasUniqueInitializer() const {
124 // We need to be sure this is the definition that will actually be used
125 isStrongDefinitionForLinker() &&
126 // It is not safe to modify initializers of global variables with the
127 // external_initializer marker since the value may be changed at runtime
128 // before C++ initializers are evaluated.
129 !isExternallyInitialized();
132 /// getInitializer - Return the initializer for this global variable. It is
133 /// illegal to call this method if the global is external, because we cannot
134 /// tell what the value is initialized to!
136 inline const Constant
*getInitializer() const {
137 assert(hasInitializer() && "GV doesn't have initializer!");
138 return static_cast<Constant
*>(Op
<0>().get());
140 inline Constant
*getInitializer() {
141 assert(hasInitializer() && "GV doesn't have initializer!");
142 return static_cast<Constant
*>(Op
<0>().get());
144 /// setInitializer - Sets the initializer for this global variable, removing
145 /// any existing initializer if InitVal==NULL. If this GV has type T*, the
146 /// initializer must have type T.
147 void setInitializer(Constant
*InitVal
);
149 /// If the value is a global constant, its value is immutable throughout the
150 /// runtime execution of the program. Assigning a value into the constant
151 /// leads to undefined behavior.
153 bool isConstant() const { return isConstantGlobal
; }
154 void setConstant(bool Val
) { isConstantGlobal
= Val
; }
156 bool isExternallyInitialized() const {
157 return isExternallyInitializedConstant
;
159 void setExternallyInitialized(bool Val
) {
160 isExternallyInitializedConstant
= Val
;
163 /// copyAttributesFrom - copy all additional attributes (those not needed to
164 /// create a GlobalVariable) from the GlobalVariable Src to this one.
165 void copyAttributesFrom(const GlobalVariable
*Src
);
167 /// removeFromParent - This method unlinks 'this' from the containing module,
168 /// but does not delete it.
170 void removeFromParent();
172 /// eraseFromParent - This method unlinks 'this' from the containing module
175 void eraseFromParent();
177 /// Drop all references in preparation to destroy the GlobalVariable. This
178 /// drops not only the reference to the initializer but also to any metadata.
179 void dropAllReferences();
181 /// Attach a DIGlobalVariableExpression.
182 void addDebugInfo(DIGlobalVariableExpression
*GV
);
184 /// Fill the vector with all debug info attachements.
185 void getDebugInfo(SmallVectorImpl
<DIGlobalVariableExpression
*> &GVs
) const;
187 /// Add attribute to this global.
188 void addAttribute(Attribute::AttrKind Kind
) {
189 Attrs
= Attrs
.addAttribute(getContext(), Kind
);
192 /// Add attribute to this global.
193 void addAttribute(StringRef Kind
, StringRef Val
= StringRef()) {
194 Attrs
= Attrs
.addAttribute(getContext(), Kind
, Val
);
197 /// Return true if the attribute exists.
198 bool hasAttribute(Attribute::AttrKind Kind
) const {
199 return Attrs
.hasAttribute(Kind
);
202 /// Return true if the attribute exists.
203 bool hasAttribute(StringRef Kind
) const {
204 return Attrs
.hasAttribute(Kind
);
207 /// Return true if any attributes exist.
208 bool hasAttributes() const {
209 return Attrs
.hasAttributes();
212 /// Return the attribute object.
213 Attribute
getAttribute(Attribute::AttrKind Kind
) const {
214 return Attrs
.getAttribute(Kind
);
217 /// Return the attribute object.
218 Attribute
getAttribute(StringRef Kind
) const {
219 return Attrs
.getAttribute(Kind
);
222 /// Return the attribute set for this global
223 AttributeSet
getAttributes() const {
227 /// Return attribute set as list with index.
228 /// FIXME: This may not be required once ValueEnumerators
229 /// in bitcode-writer can enumerate attribute-set.
230 AttributeList
getAttributesAsList(unsigned index
) const {
231 if (!hasAttributes())
232 return AttributeList();
233 std::pair
<unsigned, AttributeSet
> AS
[1] = {{index
, Attrs
}};
234 return AttributeList::get(getContext(), AS
);
237 /// Set attribute list for this global
238 void setAttributes(AttributeSet A
) {
242 /// Check if section name is present
243 bool hasImplicitSection() const {
244 return getAttributes().hasAttribute("bss-section") ||
245 getAttributes().hasAttribute("data-section") ||
246 getAttributes().hasAttribute("rodata-section");
249 // Methods for support type inquiry through isa, cast, and dyn_cast:
250 static bool classof(const Value
*V
) {
251 return V
->getValueID() == Value::GlobalVariableVal
;
256 struct OperandTraits
<GlobalVariable
> :
257 public OptionalOperandTraits
<GlobalVariable
> {
260 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable
, Value
)
262 } // end namespace llvm
264 #endif // LLVM_IR_GLOBALVARIABLE_H