LLVM  14.0.0git
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/Twine.h"
23 #include "llvm/ADT/ilist_node.h"
24 #include "llvm/IR/Attributes.h"
25 #include "llvm/IR/GlobalObject.h"
26 #include "llvm/IR/OperandTraits.h"
27 #include "llvm/IR/Value.h"
28 #include <cassert>
29 #include <cstddef>
30 
31 namespace llvm {
32 
33 class Constant;
34 class Module;
35 
36 template <typename ValueSubClass> class SymbolTableListTraits;
37 class DIGlobalVariable;
38 class DIGlobalVariableExpression;
39 
40 class GlobalVariable : public GlobalObject, public ilist_node<GlobalVariable> {
42 
44  bool isConstantGlobal : 1; // Is this a global constant?
45  bool isExternallyInitializedConstant : 1; // Is this a global whose value
46  // can change from its initial
47  // value before global
48  // initializers are run?
49 
50 public:
51  /// GlobalVariable ctor - If a parent module is specified, the global is
52  /// automatically inserted into the end of the specified modules global list.
54  Constant *Initializer = nullptr, const Twine &Name = "",
56  bool isExternallyInitialized = false);
57  /// GlobalVariable ctor - This creates a global and inserts it before the
58  /// specified other global.
60  Constant *Initializer, const Twine &Name = "",
61  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.
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.
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("relro-section") ||
247  getAttributes().hasAttribute("rodata-section");
248  }
249 
250  // Methods for support type inquiry through isa, cast, and dyn_cast:
251  static bool classof(const Value *V) {
252  return V->getValueID() == Value::GlobalVariableVal;
253  }
254 };
255 
256 template <>
258  public OptionalOperandTraits<GlobalVariable> {
259 };
260 
262 
263 } // end namespace llvm
264 
265 #endif // LLVM_IR_GLOBALVARIABLE_H
llvm::GlobalVariable::setExternallyInitialized
void setExternallyInitialized(bool Val)
Definition: GlobalVariable.h:159
llvm::GlobalVariable::eraseFromParent
void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
Definition: Globals.cpp:385
Attrs
Function Attrs
Definition: README_ALTIVEC.txt:215
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::GlobalVariable::hasUniqueInitializer
bool hasUniqueInitializer() const
hasUniqueInitializer - Whether the global variable has an initializer, and any changes made to the in...
Definition: GlobalVariable.h:122
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::GlobalVariable::getDebugInfo
void getDebugInfo(SmallVectorImpl< DIGlobalVariableExpression * > &GVs) const
Fill the vector with all debug info attachements.
Definition: Metadata.cpp:1558
llvm::GlobalVariable::setConstant
void setConstant(bool Val)
Definition: GlobalVariable.h:154
llvm::GlobalVariable::copyAttributesFrom
void copyAttributesFrom(const GlobalVariable *Src)
copyAttributesFrom - copy all additional attributes (those not needed to create a GlobalVariable) fro...
Definition: Globals.cpp:412
llvm::Attribute
Definition: Attributes.h:52
llvm::GlobalVariable::addAttribute
void addAttribute(StringRef Kind, StringRef Val=StringRef())
Add attribute to this global.
Definition: GlobalVariable.h:193
llvm::GlobalValue::NotThreadLocal
@ NotThreadLocal
Definition: GlobalValue.h:179
llvm::GlobalVariable::isExternallyInitialized
bool isExternallyInitialized() const
Definition: GlobalVariable.h:156
llvm::GlobalVariable
Definition: GlobalVariable.h:40
llvm::AttributeList::get
static AttributeList get(LLVMContext &C, ArrayRef< std::pair< unsigned, Attribute >> Attrs)
Create an AttributeList with the specified parameters in it.
Definition: Attributes.cpp:1011
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::AttributeList
Definition: Attributes.h:399
DEFINE_TRANSPARENT_OPERAND_ACCESSORS
#define DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CLASS, VALUECLASS)
Macro for generating out-of-class operand accessor definitions.
Definition: OperandTraits.h:125
llvm::GlobalVariable::getInitializer
Constant * getInitializer()
Definition: GlobalVariable.h:140
llvm::Optional< unsigned >
llvm::GlobalVariable::hasAttribute
bool hasAttribute(Attribute::AttrKind Kind) const
Return true if the attribute exists.
Definition: GlobalVariable.h:198
GlobalObject.h
llvm::GlobalValue::LinkageTypes
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:47
llvm::GlobalVariable::hasAttributes
bool hasAttributes() const
Return true if any attributes exist.
Definition: GlobalVariable.h:208
llvm::GlobalVariable::hasInitializer
bool hasInitializer() const
Definitions have initializers, declarations don't.
Definition: GlobalVariable.h:92
llvm::User::setGlobalVariableNumOperands
void setGlobalVariableNumOperands(unsigned NumOps)
Set the number of operands on a GlobalVariable.
Definition: User.h:207
llvm::OperandTraits
Compile-time customization of User operands.
Definition: User.h:42
llvm::GlobalValue::isDeclaration
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:228
llvm::ISD::Constant
@ Constant
Definition: ISDOpcodes.h:76
llvm::User
Definition: User.h:44
llvm::AttributeSet::hasAttribute
bool hasAttribute(Attribute::AttrKind Kind) const
Return true if the attribute exists in this set.
Definition: Attributes.cpp:659
Twine.h
llvm::GlobalObject
Definition: GlobalObject.h:28
llvm::Value::getValueID
unsigned getValueID() const
Return an ID for the concrete type of this object.
Definition: Value.h:533
llvm::GlobalVariable::addAttribute
void addAttribute(Attribute::AttrKind Kind)
Add attribute to this global.
Definition: GlobalVariable.h:188
OperandTraits.h
DebugLocVerifyLevel::None
@ None
llvm::AddressSpace
AddressSpace
Definition: NVPTXBaseInfo.h:21
llvm::GlobalVariable::getAttribute
Attribute getAttribute(StringRef Kind) const
Return the attribute object.
Definition: GlobalVariable.h:218
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::GlobalVariable::hasAttribute
bool hasAttribute(StringRef Kind) const
Return true if the attribute exists.
Definition: GlobalVariable.h:203
llvm::GlobalVariable::dropAllReferences
void dropAllReferences()
Drop all references in preparation to destroy the GlobalVariable.
Definition: Globals.cpp:418
llvm::GlobalVariable::addDebugInfo
void addDebugInfo(DIGlobalVariableExpression *GV)
Attach a DIGlobalVariableExpression.
Definition: Metadata.cpp:1554
llvm::GlobalVariable::getInitializer
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
Definition: GlobalVariable.h:136
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
llvm::SymbolTableListTraits
Definition: GlobalAlias.h:25
index
splat index
Definition: README_ALTIVEC.txt:181
llvm::GlobalVariable::hasDefinitiveInitializer
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
Definition: GlobalVariable.h:110
llvm::GlobalVariable::hasImplicitSection
bool hasImplicitSection() const
Check if section name is present.
Definition: GlobalVariable.h:243
s
multiplies can be turned into SHL s
Definition: README.txt:370
llvm::Attribute::AttrKind
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results,...
Definition: Attributes.h:71
llvm::GlobalValue::Linkage
unsigned Linkage
Definition: GlobalValue.h:94
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
A
* A
Definition: README_ALTIVEC.txt:89
llvm::Value::getContext
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:990
Module
Machine Check Debug Module
Definition: MachineCheckDebugify.cpp:122
llvm::GlobalVariable::GlobalVariable
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:333
llvm::GlobalValue::isStrongDefinitionForLinker
bool isStrongDefinitionForLinker() const
Returns true if this global's definition will be the one chosen by the linker.
Definition: GlobalValue.h:553
llvm::ilist_node
Definition: ilist_node.h:148
llvm::GlobalValue::isInterposable
bool isInterposable() const
Return true if this global's definition can be substituted with an arbitrary definition at link time ...
Definition: Globals.cpp:98
Attributes.h
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::GlobalVariable::classof
static bool classof(const Value *V)
Definition: GlobalVariable.h:251
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::GlobalValue::ThreadLocalMode
ThreadLocalMode
Definition: GlobalValue.h:178
llvm::GlobalVariable::getAttributesAsList
AttributeList getAttributesAsList(unsigned index) const
Return attribute set as list with index.
Definition: GlobalVariable.h:230
llvm::GlobalVariable::getAttributes
AttributeSet getAttributes() const
Return the attribute set for this global.
Definition: GlobalVariable.h:223
llvm::DIGlobalVariableExpression
A pair of DIGlobalVariable and DIExpression.
Definition: DebugInfoMetadata.h:3401
llvm::OptionalOperandTraits
OptionalOperandTraits - when the number of operands may change at runtime.
Definition: OperandTraits.h:53
llvm::GlobalVariable::DECLARE_TRANSPARENT_OPERAND_ACCESSORS
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)
Provide fast operand accessors.
llvm::AttributeSet
Definition: Attributes.h:267
llvm::GlobalVariable::isConstant
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
Definition: GlobalVariable.h:153
llvm::GlobalVariable::getAttribute
Attribute getAttribute(Attribute::AttrKind Kind) const
Return the attribute object.
Definition: GlobalVariable.h:213
llvm::GlobalVariable::setAttributes
void setAttributes(AttributeSet A)
Set attribute list for this global.
Definition: GlobalVariable.h:238
ilist_node.h
llvm::SmallVectorImpl
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:43
llvm::GlobalVariable::operator=
GlobalVariable & operator=(const GlobalVariable &)=delete
llvm::GlobalVariable::~GlobalVariable
~GlobalVariable()
Definition: GlobalVariable.h:68
llvm::GlobalVariable::removeFromParent
void removeFromParent()
removeFromParent - This method unlinks 'this' from the containing module, but does not delete it.
Definition: Globals.cpp:381
Value.h
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
llvm::GlobalVariable::setInitializer
void setInitializer(Constant *InitVal)
setInitializer - Sets the initializer for this global variable, removing any existing initializer if ...
Definition: Globals.cpp:389