LLVM  16.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 DIGlobalVariableExpression;
38 
39 class GlobalVariable : public GlobalObject, public ilist_node<GlobalVariable> {
41 
43  bool isConstantGlobal : 1; // Is this a global constant?
44  bool isExternallyInitializedConstant : 1; // Is this a global whose value
45  // can change from its initial
46  // value before global
47  // initializers are run?
48 
49 public:
50  /// GlobalVariable ctor - If a parent module is specified, the global is
51  /// automatically inserted into the end of the specified modules global list.
53  Constant *Initializer = nullptr, const Twine &Name = "",
55  bool isExternallyInitialized = false);
56  /// GlobalVariable ctor - This creates a global and inserts it before the
57  /// specified other global.
59  Constant *Initializer, const Twine &Name = "",
60  GlobalVariable *InsertBefore = nullptr,
63  bool isExternallyInitialized = false);
64  GlobalVariable(const GlobalVariable &) = delete;
65  GlobalVariable &operator=(const GlobalVariable &) = delete;
66 
69  }
70 
71  // allocate space for exactly one operand
72  void *operator new(size_t s) {
73  return User::operator new(s, 1);
74  }
75 
76  // delete space for exactly one operand as created in the corresponding new operator
77  void operator delete(void *ptr){
78  assert(ptr != nullptr && "must not be nullptr");
79  User *Obj = static_cast<User *>(ptr);
80  // Number of operands can be set to 0 after construction and initialization. Make sure
81  // that number of operands is reset to 1, as this is needed in User::operator delete
83  User::operator delete(Obj);
84  }
85 
86  /// Provide fast operand accessors
88 
89  /// Definitions have initializers, declarations don't.
90  ///
91  inline bool hasInitializer() const { return !isDeclaration(); }
92 
93  /// hasDefinitiveInitializer - Whether the global variable has an initializer,
94  /// and any other instances of the global (this can happen due to weak
95  /// linkage) are guaranteed to have the same initializer.
96  ///
97  /// Note that if you want to transform a global, you must use
98  /// hasUniqueInitializer() instead, because of the *_odr linkage type.
99  ///
100  /// Example:
101  ///
102  /// @a = global SomeType* null - Initializer is both definitive and unique.
103  ///
104  /// @b = global weak SomeType* null - Initializer is neither definitive nor
105  /// unique.
106  ///
107  /// @c = global weak_odr SomeType* null - Initializer is definitive, but not
108  /// unique.
109  inline bool hasDefinitiveInitializer() const {
110  return hasInitializer() &&
111  // The initializer of a global variable may change to something arbitrary
112  // at link time.
113  !isInterposable() &&
114  // The initializer of a global variable with the externally_initialized
115  // marker may change at runtime before C++ initializers are evaluated.
117  }
118 
119  /// hasUniqueInitializer - Whether the global variable has an initializer, and
120  /// any changes made to the initializer will turn up in the final executable.
121  inline bool hasUniqueInitializer() const {
122  return
123  // We need to be sure this is the definition that will actually be used
125  // It is not safe to modify initializers of global variables with the
126  // external_initializer marker since the value may be changed at runtime
127  // before C++ initializers are evaluated.
129  }
130 
131  /// getInitializer - Return the initializer for this global variable. It is
132  /// illegal to call this method if the global is external, because we cannot
133  /// tell what the value is initialized to!
134  ///
135  inline const Constant *getInitializer() const {
136  assert(hasInitializer() && "GV doesn't have initializer!");
137  return static_cast<Constant*>(Op<0>().get());
138  }
140  assert(hasInitializer() && "GV doesn't have initializer!");
141  return static_cast<Constant*>(Op<0>().get());
142  }
143  /// setInitializer - Sets the initializer for this global variable, removing
144  /// any existing initializer if InitVal==NULL. If this GV has type T*, the
145  /// initializer must have type T.
146  void setInitializer(Constant *InitVal);
147 
148  /// If the value is a global constant, its value is immutable throughout the
149  /// runtime execution of the program. Assigning a value into the constant
150  /// leads to undefined behavior.
151  ///
152  bool isConstant() const { return isConstantGlobal; }
153  void setConstant(bool Val) { isConstantGlobal = Val; }
154 
155  bool isExternallyInitialized() const {
156  return isExternallyInitializedConstant;
157  }
158  void setExternallyInitialized(bool Val) {
159  isExternallyInitializedConstant = Val;
160  }
161 
162  /// copyAttributesFrom - copy all additional attributes (those not needed to
163  /// create a GlobalVariable) from the GlobalVariable Src to this one.
164  void copyAttributesFrom(const GlobalVariable *Src);
165 
166  /// removeFromParent - This method unlinks 'this' from the containing module,
167  /// but does not delete it.
168  ///
169  void removeFromParent();
170 
171  /// eraseFromParent - This method unlinks 'this' from the containing module
172  /// and deletes it.
173  ///
174  void eraseFromParent();
175 
176  /// Drop all references in preparation to destroy the GlobalVariable. This
177  /// drops not only the reference to the initializer but also to any metadata.
178  void dropAllReferences();
179 
180  /// Attach a DIGlobalVariableExpression.
182 
183  /// Fill the vector with all debug info attachements.
185 
186  /// Add attribute to this global.
188  Attrs = Attrs.addAttribute(getContext(), Kind);
189  }
190 
191  /// Add attribute to this global.
193  Attrs = Attrs.addAttribute(getContext(), Kind, Val);
194  }
195 
196  /// Return true if the attribute exists.
198  return Attrs.hasAttribute(Kind);
199  }
200 
201  /// Return true if the attribute exists.
203  return Attrs.hasAttribute(Kind);
204  }
205 
206  /// Return true if any attributes exist.
207  bool hasAttributes() const {
208  return Attrs.hasAttributes();
209  }
210 
211  /// Return the attribute object.
213  return Attrs.getAttribute(Kind);
214  }
215 
216  /// Return the attribute object.
218  return Attrs.getAttribute(Kind);
219  }
220 
221  /// Return the attribute set for this global
223  return Attrs;
224  }
225 
226  /// Return attribute set as list with index.
227  /// FIXME: This may not be required once ValueEnumerators
228  /// in bitcode-writer can enumerate attribute-set.
230  if (!hasAttributes())
231  return AttributeList();
232  std::pair<unsigned, AttributeSet> AS[1] = {{index, Attrs}};
233  return AttributeList::get(getContext(), AS);
234  }
235 
236  /// Set attribute list for this global
238  Attrs = A;
239  }
240 
241  /// Check if section name is present
242  bool hasImplicitSection() const {
243  return getAttributes().hasAttribute("bss-section") ||
244  getAttributes().hasAttribute("data-section") ||
245  getAttributes().hasAttribute("relro-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
llvm::GlobalVariable::setExternallyInitialized
void setExternallyInitialized(bool Val)
Definition: GlobalVariable.h:158
llvm::GlobalVariable::eraseFromParent
void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
Definition: Globals.cpp:464
Attrs
Function Attrs
Definition: README_ALTIVEC.txt:215
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::GlobalVariable::hasUniqueInitializer
bool hasUniqueInitializer() const
hasUniqueInitializer - Whether the global variable has an initializer, and any changes made to the in...
Definition: GlobalVariable.h:121
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:1600
llvm::GlobalVariable::setConstant
void setConstant(bool Val)
Definition: GlobalVariable.h:153
llvm::GlobalVariable::copyAttributesFrom
void copyAttributesFrom(const GlobalVariable *Src)
copyAttributesFrom - copy all additional attributes (those not needed to create a GlobalVariable) fro...
Definition: Globals.cpp:491
llvm::Attribute
Definition: Attributes.h:65
llvm::GlobalVariable::addAttribute
void addAttribute(StringRef Kind, StringRef Val=StringRef())
Add attribute to this global.
Definition: GlobalVariable.h:192
llvm::GlobalValue::NotThreadLocal
@ NotThreadLocal
Definition: GlobalValue.h:192
llvm::GlobalVariable::isExternallyInitialized
bool isExternallyInitialized() const
Definition: GlobalVariable.h:155
llvm::GlobalVariable
Definition: GlobalVariable.h:39
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:1050
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:425
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:139
llvm::Optional< unsigned >
llvm::GlobalVariable::hasAttribute
bool hasAttribute(Attribute::AttrKind Kind) const
Return true if the attribute exists.
Definition: GlobalVariable.h:197
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:207
llvm::GlobalVariable::hasInitializer
bool hasInitializer() const
Definitions have initializers, declarations don't.
Definition: GlobalVariable.h:91
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:271
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:690
Twine.h
llvm::GlobalObject
Definition: GlobalObject.h:27
llvm::Value::getValueID
unsigned getValueID() const
Return an ID for the concrete type of this object.
Definition: Value.h:532
llvm::GlobalVariable::addAttribute
void addAttribute(Attribute::AttrKind Kind)
Add attribute to this global.
Definition: GlobalVariable.h:187
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:217
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:202
llvm::GlobalVariable::dropAllReferences
void dropAllReferences()
Drop all references in preparation to destroy the GlobalVariable.
Definition: Globals.cpp:497
llvm::GlobalVariable::addDebugInfo
void addDebugInfo(DIGlobalVariableExpression *GV)
Attach a DIGlobalVariableExpression.
Definition: Metadata.cpp:1596
llvm::GlobalVariable::getInitializer
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
Definition: GlobalVariable.h:135
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
llvm::SymbolTableListTraits
Definition: GlobalAlias.h:26
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:109
llvm::GlobalVariable::hasImplicitSection
bool hasImplicitSection() const
Check if section name is present.
Definition: GlobalVariable.h:242
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:84
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:65
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
A
* A
Definition: README_ALTIVEC.txt:89
llvm::Value::getContext
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:994
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:412
llvm::GlobalValue::isStrongDefinitionForLinker
bool isStrongDefinitionForLinker() const
Returns true if this global's definition will be the one chosen by the linker.
Definition: GlobalValue.h:625
llvm::ilist_node
Definition: ilist_node.h:149
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:102
Attributes.h
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
llvm::GlobalVariable::classof
static bool classof(const Value *V)
Definition: GlobalVariable.h:250
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
llvm::GlobalValue::ThreadLocalMode
ThreadLocalMode
Definition: GlobalValue.h:191
llvm::GlobalVariable::getAttributesAsList
AttributeList getAttributesAsList(unsigned index) const
Return attribute set as list with index.
Definition: GlobalVariable.h:229
llvm::GlobalVariable::getAttributes
AttributeSet getAttributes() const
Return the attribute set for this global.
Definition: GlobalVariable.h:222
llvm::DIGlobalVariableExpression
A pair of DIGlobalVariable and DIExpression.
Definition: DebugInfoMetadata.h:3358
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:290
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:152
llvm::GlobalVariable::getAttribute
Attribute getAttribute(Attribute::AttrKind Kind) const
Return the attribute object.
Definition: GlobalVariable.h:212
llvm::GlobalVariable::setAttributes
void setAttributes(AttributeSet A)
Set attribute list for this global.
Definition: GlobalVariable.h:237
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:42
llvm::GlobalVariable::operator=
GlobalVariable & operator=(const GlobalVariable &)=delete
llvm::GlobalVariable::~GlobalVariable
~GlobalVariable()
Definition: GlobalVariable.h:67
llvm::GlobalVariable::removeFromParent
void removeFromParent()
removeFromParent - This method unlinks 'this' from the containing module, but does not delete it.
Definition: Globals.cpp:460
Value.h
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::GlobalVariable::setInitializer
void setInitializer(Constant *InitVal)
setInitializer - Sets the initializer for this global variable, removing any existing initializer if ...
Definition: Globals.cpp:468