LLVM  7.0.0svn
GlobalVariable.h
Go to the documentation of this file.
1 //===-- llvm/GlobalVariable.h - GlobalVariable class ------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the declaration of the GlobalVariable class, which
11 // represents a single global variable (or constant) in the VM.
12 //
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.
17 //
18 //===----------------------------------------------------------------------===//
19 
20 #ifndef LLVM_IR_GLOBALVARIABLE_H
21 #define LLVM_IR_GLOBALVARIABLE_H
22 
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"
30 #include <cassert>
31 #include <cstddef>
32 
33 namespace llvm {
34 
35 class Constant;
36 class Module;
37 
38 template <typename ValueSubClass> class SymbolTableListTraits;
39 class DIGlobalVariable;
40 class DIGlobalVariableExpression;
41 
42 class GlobalVariable : public GlobalObject, public ilist_node<GlobalVariable> {
44 
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?
51 
52 public:
53  /// GlobalVariable ctor - If a parent module is specified, the global is
54  /// automatically inserted into the end of the specified modules global list.
56  Constant *Initializer = nullptr, const Twine &Name = "",
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,
65  bool isExternallyInitialized = false);
66  GlobalVariable(const GlobalVariable &) = delete;
67  GlobalVariable &operator=(const GlobalVariable &) = delete;
68 
71  }
72 
73  // allocate space for exactly one operand
74  void *operator new(size_t s) {
75  return User::operator new(s, 1);
76  }
77 
78  // delete space for exactly one operand as created in the corresponding new operator
79  void operator delete(void *ptr){
80  assert(ptr != nullptr && "must not be nullptr");
81  User *Obj = static_cast<User *>(ptr);
82  // Number of operands can be set to 0 after construction and initialization. Make sure
83  // that number of operands is reset to 1, as this is needed in User::operator delete
85  User::operator delete(Obj);
86  }
87 
88  /// Provide fast operand accessors
90 
91  /// Definitions have initializers, declarations don't.
92  ///
93  inline bool hasInitializer() const { return !isDeclaration(); }
94 
95  /// hasDefinitiveInitializer - Whether the global variable has an initializer,
96  /// and any other instances of the global (this can happen due to weak
97  /// linkage) are guaranteed to have the same initializer.
98  ///
99  /// Note that if you want to transform a global, you must use
100  /// hasUniqueInitializer() instead, because of the *_odr linkage type.
101  ///
102  /// Example:
103  ///
104  /// @a = global SomeType* null - Initializer is both definitive and unique.
105  ///
106  /// @b = global weak SomeType* null - Initializer is neither definitive nor
107  /// unique.
108  ///
109  /// @c = global weak_odr SomeType* null - Initializer is definitive, but not
110  /// unique.
111  inline bool hasDefinitiveInitializer() const {
112  return hasInitializer() &&
113  // The initializer of a global variable may change to something arbitrary
114  // at link time.
115  !isInterposable() &&
116  // The initializer of a global variable with the externally_initialized
117  // marker may change at runtime before C++ initializers are evaluated.
119  }
120 
121  /// hasUniqueInitializer - Whether the global variable has an initializer, and
122  /// any changes made to the initializer will turn up in the final executable.
123  inline bool hasUniqueInitializer() const {
124  return
125  // We need to be sure this is the definition that will actually be used
127  // It is not safe to modify initializers of global variables with the
128  // external_initializer marker since the value may be changed at runtime
129  // before C++ initializers are evaluated.
131  }
132 
133  /// getInitializer - Return the initializer for this global variable. It is
134  /// illegal to call this method if the global is external, because we cannot
135  /// tell what the value is initialized to!
136  ///
137  inline const Constant *getInitializer() const {
138  assert(hasInitializer() && "GV doesn't have initializer!");
139  return static_cast<Constant*>(Op<0>().get());
140  }
142  assert(hasInitializer() && "GV doesn't have initializer!");
143  return static_cast<Constant*>(Op<0>().get());
144  }
145  /// setInitializer - Sets the initializer for this global variable, removing
146  /// any existing initializer if InitVal==NULL. If this GV has type T*, the
147  /// initializer must have type T.
148  void setInitializer(Constant *InitVal);
149 
150  /// If the value is a global constant, its value is immutable throughout the
151  /// runtime execution of the program. Assigning a value into the constant
152  /// leads to undefined behavior.
153  ///
154  bool isConstant() const { return isConstantGlobal; }
155  void setConstant(bool Val) { isConstantGlobal = Val; }
156 
157  bool isExternallyInitialized() const {
158  return isExternallyInitializedConstant;
159  }
160  void setExternallyInitialized(bool Val) {
161  isExternallyInitializedConstant = Val;
162  }
163 
164  /// copyAttributesFrom - copy all additional attributes (those not needed to
165  /// create a GlobalVariable) from the GlobalVariable Src to this one.
166  void copyAttributesFrom(const GlobalVariable *Src);
167 
168  /// removeFromParent - This method unlinks 'this' from the containing module,
169  /// but does not delete it.
170  ///
171  void removeFromParent();
172 
173  /// eraseFromParent - This method unlinks 'this' from the containing module
174  /// and deletes it.
175  ///
176  void eraseFromParent();
177 
178  /// Drop all references in preparation to destroy the GlobalVariable. This
179  /// drops not only the reference to the initializer but also to any metadata.
180  void dropAllReferences();
181 
182  /// Attach a DIGlobalVariableExpression.
184 
185  /// Fill the vector with all debug info attachements.
187 
188  /// Add attribute to this global.
190  Attrs = Attrs.addAttribute(getContext(), Kind);
191  }
192 
193  /// Add attribute to this global.
195  Attrs = Attrs.addAttribute(getContext(), Kind, Val);
196  }
197 
198  /// Return true if the attribute exists.
200  return Attrs.hasAttribute(Kind);
201  }
202 
203  /// Return true if the attribute exists.
204  bool hasAttribute(StringRef Kind) const {
205  return Attrs.hasAttribute(Kind);
206  }
207 
208  /// Return true if any attributes exist.
209  bool hasAttributes() const {
210  return Attrs.hasAttributes();
211  }
212 
213  /// Return the attribute object.
215  return Attrs.getAttribute(Kind);
216  }
217 
218  /// Return the attribute object.
220  return Attrs.getAttribute(Kind);
221  }
222 
223  /// Return the attribute set for this global
225  return Attrs;
226  }
227 
228  /// Return attribute set as list with index.
229  /// FIXME: This may not be required once ValueEnumerators
230  /// in bitcode-writer can enumerate attribute-set.
231  AttributeList getAttributesAsList(unsigned index) const {
232  if (!hasAttributes())
233  return AttributeList();
234  std::pair<unsigned, AttributeSet> AS[1] = {{index, Attrs}};
235  return AttributeList::get(getContext(), AS);
236  }
237 
238  /// Set attribute list for this global
240  Attrs = A;
241  }
242 
243  /// Check if section name is present
244  bool hasImplicitSection() const {
245  return getAttributes().hasAttribute("bss-section") ||
246  getAttributes().hasAttribute("data-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
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:469
static bool classof(const Value *V)
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
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:738
bool isInterposable() const
Return true if this global&#39;s definition can be substituted with an arbitrary definition at link time...
Definition: GlobalValue.h:419
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
void setInitializer(Constant *InitVal)
setInitializer - Sets the initializer for this global variable, removing any existing initializer if ...
Definition: Globals.cpp:358
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:42
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:306
bool hasAttribute(Attribute::AttrKind Kind) const
Return true if the attribute exists in this set.
Definition: Attributes.cpp:575
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:46
This is an important base class in LLVM.
Definition: Constant.h:42
void setExternallyInitialized(bool Val)
void getDebugInfo(SmallVectorImpl< DIGlobalVariableExpression *> &GVs) const
Fill the vector with all debug info attachements.
Definition: Metadata.cpp:1522
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:354
A pair of DIGlobalVariable and DIExpression.
OptionalOperandTraits - when the number of operands may change at runtime.
Definition: OperandTraits.h:54
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:208
void setConstant(bool Val)
const AMDGPUAS & AS
void removeFromParent()
removeFromParent - This method unlinks &#39;this&#39; from the containing module, but does not delete it...
Definition: Globals.cpp:350
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)
Provide fast operand accessors.
AddressSpace
Definition: NVPTXBaseInfo.h:22
unsigned Linkage
Definition: GlobalValue.h:95
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:48
void setAttributes(AttributeSet A)
Set attribute list for this global.
void dropAllReferences()
Drop all references in preparation to destroy the GlobalVariable.
Definition: Globals.cpp:388
bool isStrongDefinitionForLinker() const
Returns true if this global&#39;s definition will be the one chosen by the linker.
Definition: GlobalValue.h:536
void copyAttributesFrom(const GlobalVariable *Src)
copyAttributesFrom - copy all additional attributes (those not needed to create a GlobalVariable) fro...
Definition: Globals.cpp:381
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
Compile-time customization of User operands.
Definition: User.h:43
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:201
const unsigned Kind
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:1518
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
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:870
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:69