LLVM  10.0.0svn
GlobalVariable.h
Go to the documentation of this file.
1 //===-- llvm/GlobalVariable.h - GlobalVariable class ------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains the declaration of the GlobalVariable class, which
10 // represents a single global variable (or constant) in the VM.
11 //
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.
16 //
17 //===----------------------------------------------------------------------===//
18 
19 #ifndef LLVM_IR_GLOBALVARIABLE_H
20 #define LLVM_IR_GLOBALVARIABLE_H
21 
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"
29 #include <cassert>
30 #include <cstddef>
31 
32 namespace llvm {
33 
34 class Constant;
35 class Module;
36 
37 template <typename ValueSubClass> class SymbolTableListTraits;
38 class DIGlobalVariable;
39 class DIGlobalVariableExpression;
40 
41 class GlobalVariable : public GlobalObject, public ilist_node<GlobalVariable> {
43 
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?
50 
51 public:
52  /// GlobalVariable ctor - If a parent module is specified, the global is
53  /// automatically inserted into the end of the specified modules global list.
55  Constant *Initializer = nullptr, const Twine &Name = "",
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,
64  bool isExternallyInitialized = false);
65  GlobalVariable(const GlobalVariable &) = delete;
66  GlobalVariable &operator=(const GlobalVariable &) = delete;
67 
70  }
71 
72  // allocate space for exactly one operand
73  void *operator new(size_t s) {
74  return User::operator new(s, 1);
75  }
76 
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
84  User::operator delete(Obj);
85  }
86 
87  /// Provide fast operand accessors
89 
90  /// Definitions have initializers, declarations don't.
91  ///
92  inline bool hasInitializer() const { return !isDeclaration(); }
93 
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.
97  ///
98  /// Note that if you want to transform a global, you must use
99  /// hasUniqueInitializer() instead, because of the *_odr linkage type.
100  ///
101  /// Example:
102  ///
103  /// @a = global SomeType* null - Initializer is both definitive and unique.
104  ///
105  /// @b = global weak SomeType* null - Initializer is neither definitive nor
106  /// unique.
107  ///
108  /// @c = global weak_odr SomeType* null - Initializer is definitive, but not
109  /// unique.
110  inline bool hasDefinitiveInitializer() const {
111  return hasInitializer() &&
112  // The initializer of a global variable may change to something arbitrary
113  // at link time.
114  !isInterposable() &&
115  // The initializer of a global variable with the externally_initialized
116  // marker may change at runtime before C++ initializers are evaluated.
118  }
119 
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 {
123  return
124  // We need to be sure this is the definition that will actually be used
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.
130  }
131 
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!
135  ///
136  inline const Constant *getInitializer() const {
137  assert(hasInitializer() && "GV doesn't have initializer!");
138  return static_cast<Constant*>(Op<0>().get());
139  }
141  assert(hasInitializer() && "GV doesn't have initializer!");
142  return static_cast<Constant*>(Op<0>().get());
143  }
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);
148 
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.
152  ///
153  bool isConstant() const { return isConstantGlobal; }
154  void setConstant(bool Val) { isConstantGlobal = Val; }
155 
156  bool isExternallyInitialized() const {
157  return isExternallyInitializedConstant;
158  }
159  void setExternallyInitialized(bool Val) {
160  isExternallyInitializedConstant = Val;
161  }
162 
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);
166 
167  /// removeFromParent - This method unlinks 'this' from the containing module,
168  /// but does not delete it.
169  ///
170  void removeFromParent();
171 
172  /// eraseFromParent - This method unlinks 'this' from the containing module
173  /// and deletes it.
174  ///
175  void eraseFromParent();
176 
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();
180 
181  /// Attach a DIGlobalVariableExpression.
183 
184  /// Fill the vector with all debug info attachements.
186 
187  /// Add attribute to this global.
189  Attrs = Attrs.addAttribute(getContext(), Kind);
190  }
191 
192  /// Add attribute to this global.
194  Attrs = Attrs.addAttribute(getContext(), Kind, Val);
195  }
196 
197  /// Return true if the attribute exists.
199  return Attrs.hasAttribute(Kind);
200  }
201 
202  /// Return true if the attribute exists.
203  bool hasAttribute(StringRef Kind) const {
204  return Attrs.hasAttribute(Kind);
205  }
206 
207  /// Return true if any attributes exist.
208  bool hasAttributes() const {
209  return Attrs.hasAttributes();
210  }
211 
212  /// Return the attribute object.
214  return Attrs.getAttribute(Kind);
215  }
216 
217  /// Return the attribute object.
219  return Attrs.getAttribute(Kind);
220  }
221 
222  /// Return the attribute set for this global
224  return Attrs;
225  }
226 
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);
235  }
236 
237  /// Set attribute list for this global
239  Attrs = A;
240  }
241 
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");
247  }
248 
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;
252  }
253 };
254 
255 template <>
257  public OptionalOperandTraits<GlobalVariable> {
258 };
259 
261 
262 } // end namespace llvm
263 
264 #endif // LLVM_IR_GLOBALVARIABLE_H
AttributeSet getAttributes() const
Return the attribute set for this global.
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
unsigned getValueID() const
Return an ID for the concrete type of this object.
Definition: Value.h:483
static bool classof(const Value *V)
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
bool hasAttributes() const
Return true if any attributes exist.
void addAttribute(Attribute::AttrKind Kind)
Add attribute to this global.
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:733
bool isInterposable() const
Return true if this global&#39;s definition can be substituted with an arbitrary definition at link time...
Definition: GlobalValue.h:429
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
void setInitializer(Constant *InitVal)
setInitializer - Sets the initializer for this global variable, removing any existing initializer if ...
Definition: Globals.cpp:388
GlobalVariable & operator=(const GlobalVariable &)=delete
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:41
This file contains the simple types necessary to represent the attributes associated with functions a...
void addAttribute(StringRef Kind, StringRef Val=StringRef())
Add attribute to this global.
#define DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CLASS, VALUECLASS)
Macro for generating out-of-class operand accessor definitions.
bool hasImplicitSection() const
Check if section name is present.
Attribute getAttribute(Attribute::AttrKind Kind) const
Return the attribute object.
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
GlobalVariable(Type *Ty, bool isConstant, LinkageTypes Linkage, Constant *Initializer=nullptr, const Twine &Name="", ThreadLocalMode=NotThreadLocal, unsigned AddressSpace=0, bool isExternallyInitialized=false)
GlobalVariable ctor - If a parent module is specified, the global is automatically inserted into the ...
Definition: Globals.cpp:336
bool hasAttribute(Attribute::AttrKind Kind) const
Return true if the attribute exists in this set.
Definition: Attributes.cpp:657
AttributeList getAttributesAsList(unsigned index) const
Return attribute set as list with index.
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
This is an important base class in LLVM.
Definition: Constant.h:41
void setExternallyInitialized(bool Val)
void getDebugInfo(SmallVectorImpl< DIGlobalVariableExpression *> &GVs) const
Fill the vector with all debug info attachements.
Definition: Metadata.cpp:1521
bool hasUniqueInitializer() const
hasUniqueInitializer - Whether the global variable has an initializer, and any changes made to the in...
void eraseFromParent()
eraseFromParent - This method unlinks &#39;this&#39; from the containing module and deletes it...
Definition: Globals.cpp:384
A pair of DIGlobalVariable and DIExpression.
OptionalOperandTraits - when the number of operands may change at runtime.
Definition: OperandTraits.h:53
bool hasAttribute(Attribute::AttrKind Kind) const
Return true if the attribute exists.
void setGlobalVariableNumOperands(unsigned NumOps)
Set the number of operands on a GlobalVariable.
Definition: User.h:207
void setConstant(bool Val)
void removeFromParent()
removeFromParent - This method unlinks &#39;this&#39; from the containing module, but does not delete it...
Definition: Globals.cpp:380
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)
Provide fast operand accessors.
AddressSpace
Definition: NVPTXBaseInfo.h:21
unsigned Linkage
Definition: GlobalValue.h:94
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:47
void setAttributes(AttributeSet A)
Set attribute list for this global.
void dropAllReferences()
Drop all references in preparation to destroy the GlobalVariable.
Definition: Globals.cpp:418
bool isStrongDefinitionForLinker() const
Returns true if this global&#39;s definition will be the one chosen by the linker.
Definition: GlobalValue.h:546
void copyAttributesFrom(const GlobalVariable *Src)
copyAttributesFrom - copy all additional attributes (those not needed to create a GlobalVariable) fro...
Definition: Globals.cpp:411
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
Compile-time customization of User operands.
Definition: User.h:42
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:231
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
bool hasInitializer() const
Definitions have initializers, declarations don&#39;t.
Attribute getAttribute(StringRef Kind) const
Return the attribute object.
void addDebugInfo(DIGlobalVariableExpression *GV)
Attach a DIGlobalVariableExpression.
Definition: Metadata.cpp:1517
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
Constant * getInitializer()
bool isExternallyInitialized() const
static AttributeList get(LLVMContext &C, ArrayRef< std::pair< unsigned, Attribute >> Attrs)
Create an AttributeList with the specified parameters in it.
Definition: Attributes.cpp:973
bool hasAttribute(StringRef Kind) const
Return true if the attribute exists.
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results...
Definition: Attributes.h:70