LLVM  4.0.0
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/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 
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.
59  GlobalVariable(Module &M, Type *Ty, bool isConstant,
60  LinkageTypes Linkage, Constant *Initializer,
61  const Twine &Name = "", GlobalVariable *InsertBefore = nullptr,
63  bool isExternallyInitialized = false);
64  GlobalVariable(const GlobalVariable &) = delete;
65  GlobalVariable &operator=(const GlobalVariable &) = delete;
66 
67  ~GlobalVariable() override {
69 
70  // FIXME: needed by operator delete
72  }
73 
74  // allocate space for exactly one operand
75  void *operator new(size_t s) {
76  return User::operator new(s, 1);
77  }
78 
79  void *operator new(size_t, unsigned) = delete;
80 
81  /// Provide fast operand accessors
83 
84  /// Definitions have initializers, declarations don't.
85  ///
86  inline bool hasInitializer() const { return !isDeclaration(); }
87 
88  /// hasDefinitiveInitializer - Whether the global variable has an initializer,
89  /// and any other instances of the global (this can happen due to weak
90  /// linkage) are guaranteed to have the same initializer.
91  ///
92  /// Note that if you want to transform a global, you must use
93  /// hasUniqueInitializer() instead, because of the *_odr linkage type.
94  ///
95  /// Example:
96  ///
97  /// @a = global SomeType* null - Initializer is both definitive and unique.
98  ///
99  /// @b = global weak SomeType* null - Initializer is neither definitive nor
100  /// unique.
101  ///
102  /// @c = global weak_odr SomeType* null - Initializer is definitive, but not
103  /// unique.
104  inline bool hasDefinitiveInitializer() const {
105  return hasInitializer() &&
106  // The initializer of a global variable may change to something arbitrary
107  // at link time.
108  !isInterposable() &&
109  // The initializer of a global variable with the externally_initialized
110  // marker may change at runtime before C++ initializers are evaluated.
112  }
113 
114  /// hasUniqueInitializer - Whether the global variable has an initializer, and
115  /// any changes made to the initializer will turn up in the final executable.
116  inline bool hasUniqueInitializer() const {
117  return
118  // We need to be sure this is the definition that will actually be used
120  // It is not safe to modify initializers of global variables with the
121  // external_initializer marker since the value may be changed at runtime
122  // before C++ initializers are evaluated.
124  }
125 
126  /// getInitializer - Return the initializer for this global variable. It is
127  /// illegal to call this method if the global is external, because we cannot
128  /// tell what the value is initialized to!
129  ///
130  inline const Constant *getInitializer() const {
131  assert(hasInitializer() && "GV doesn't have initializer!");
132  return static_cast<Constant*>(Op<0>().get());
133  }
135  assert(hasInitializer() && "GV doesn't have initializer!");
136  return static_cast<Constant*>(Op<0>().get());
137  }
138  /// setInitializer - Sets the initializer for this global variable, removing
139  /// any existing initializer if InitVal==NULL. If this GV has type T*, the
140  /// initializer must have type T.
141  void setInitializer(Constant *InitVal);
142 
143  /// If the value is a global constant, its value is immutable throughout the
144  /// runtime execution of the program. Assigning a value into the constant
145  /// leads to undefined behavior.
146  ///
147  bool isConstant() const { return isConstantGlobal; }
148  void setConstant(bool Val) { isConstantGlobal = Val; }
149 
150  bool isExternallyInitialized() const {
151  return isExternallyInitializedConstant;
152  }
153  void setExternallyInitialized(bool Val) {
154  isExternallyInitializedConstant = Val;
155  }
156 
157  /// copyAttributesFrom - copy all additional attributes (those not needed to
158  /// create a GlobalVariable) from the GlobalVariable Src to this one.
159  void copyAttributesFrom(const GlobalValue *Src) override;
160 
161  /// removeFromParent - This method unlinks 'this' from the containing module,
162  /// but does not delete it.
163  ///
164  void removeFromParent() override;
165 
166  /// eraseFromParent - This method unlinks 'this' from the containing module
167  /// and deletes it.
168  ///
169  void eraseFromParent() override;
170 
171  /// Drop all references in preparation to destroy the GlobalVariable. This
172  /// drops not only the reference to the initializer but also to any metadata.
173  void dropAllReferences();
174 
175  /// Attach a DIGlobalVariableExpression.
177 
178  /// Fill the vector with all debug info attachements.
180 
181  // Methods for support type inquiry through isa, cast, and dyn_cast:
182  static inline bool classof(const Value *V) {
183  return V->getValueID() == Value::GlobalVariableVal;
184  }
185 };
186 
187 template <>
189  public OptionalOperandTraits<GlobalVariable> {
190 };
191 
193 
194 } // end namespace llvm
195 
196 #endif // LLVM_IR_GLOBALVARIABLE_H
static bool classof(const Value *V)
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:52
void getDebugInfo(SmallVectorImpl< DIGlobalVariableExpression * > &GVs) const
Fill the vector with all debug info attachements.
Definition: Metadata.cpp:1466
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
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:323
GlobalVariable & operator=(const GlobalVariable &)=delete
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:32
bool isStrongDefinitionForLinker() const
Returns true if this global's definition will be the one chosen by the linker.
Definition: GlobalValue.h:509
bool hasUniqueInitializer() const
hasUniqueInitializer - Whether the global variable has an initializer, and any changes made to the in...
void eraseFromParent() override
eraseFromParent - This method unlinks 'this' from the containing module and deletes it...
Definition: Globals.cpp:319
#define DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CLASS, VALUECLASS)
Macro for generating out-of-class operand accessor definitions.
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
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:275
bool isExternallyInitialized() const
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:42
void setExternallyInitialized(bool Val)
A pair of DIGlobalVariable and DIExpression.
unsigned getValueID() const
Return an ID for the concrete type of this object.
Definition: Value.h:434
OptionalOperandTraits - when the number of operands may change at runtime.
Definition: OperandTraits.h:51
void setGlobalVariableNumOperands(unsigned NumOps)
Set the number of operands on a GlobalVariable.
Definition: User.h:183
~GlobalVariable() override
void setConstant(bool Val)
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)
Provide fast operand accessors.
AddressSpace
Definition: NVPTXBaseInfo.h:22
unsigned Linkage
Definition: GlobalValue.h:93
bool hasInitializer() const
Definitions have initializers, declarations don't.
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:48
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
void dropAllReferences()
Drop all references in preparation to destroy the GlobalVariable.
Definition: Globals.cpp:354
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:188
Compile-time customization of User operands.
Definition: User.h:43
bool isInterposable() const
Return true if this global's definition can be substituted with an arbitrary definition at link time...
Definition: GlobalValue.h:399
void copyAttributesFrom(const GlobalValue *Src) override
copyAttributesFrom - copy all additional attributes (those not needed to create a GlobalVariable) fro...
Definition: Globals.cpp:346
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:71
void addDebugInfo(DIGlobalVariableExpression *GV)
Attach a DIGlobalVariableExpression.
Definition: Metadata.cpp:1462
Constant * getInitializer()
void removeFromParent() override
removeFromParent - This method unlinks 'this' from the containing module, but does not delete it...
Definition: Globals.cpp:315