LLVM  3.7.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/Twine.h"
24 #include "llvm/ADT/ilist_node.h"
25 #include "llvm/IR/GlobalObject.h"
26 #include "llvm/IR/OperandTraits.h"
27 
28 namespace llvm {
29 
30 class Module;
31 class Constant;
32 template<typename ValueSubClass, typename ItemParentClass>
33  class SymbolTableListTraits;
34 
35 class GlobalVariable : public GlobalObject, public ilist_node<GlobalVariable> {
37  void *operator new(size_t, unsigned) = delete;
38  void operator=(const GlobalVariable &) = delete;
39  GlobalVariable(const GlobalVariable &) = delete;
40 
41  void setParent(Module *parent);
42 
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 public:
49  // allocate space for exactly one operand
50  void *operator new(size_t s) {
51  return User::operator new(s, 1);
52  }
53 
54  /// GlobalVariable ctor - If a parent module is specified, the global is
55  /// automatically inserted into the end of the specified modules global list.
57  Constant *Initializer = nullptr, const Twine &Name = "",
59  bool isExternallyInitialized = false);
60  /// GlobalVariable ctor - This creates a global and inserts it before the
61  /// specified other global.
62  GlobalVariable(Module &M, Type *Ty, bool isConstant,
63  LinkageTypes Linkage, Constant *Initializer,
64  const Twine &Name = "", GlobalVariable *InsertBefore = nullptr,
66  bool isExternallyInitialized = false);
67 
68  ~GlobalVariable() override {
69  // FIXME: needed by operator delete
71  }
72 
73  /// Provide fast operand accessors
75 
76  /// Definitions have initializers, declarations don't.
77  ///
78  inline bool hasInitializer() const { return !isDeclaration(); }
79 
80  /// hasDefinitiveInitializer - Whether the global variable has an initializer,
81  /// and any other instances of the global (this can happen due to weak
82  /// linkage) are guaranteed to have the same initializer.
83  ///
84  /// Note that if you want to transform a global, you must use
85  /// hasUniqueInitializer() instead, because of the *_odr linkage type.
86  ///
87  /// Example:
88  ///
89  /// @a = global SomeType* null - Initializer is both definitive and unique.
90  ///
91  /// @b = global weak SomeType* null - Initializer is neither definitive nor
92  /// unique.
93  ///
94  /// @c = global weak_odr SomeType* null - Initializer is definitive, but not
95  /// unique.
96  inline bool hasDefinitiveInitializer() const {
97  return hasInitializer() &&
98  // The initializer of a global variable with weak linkage may change at
99  // link time.
100  !mayBeOverridden() &&
101  // The initializer of a global variable with the externally_initialized
102  // marker may change at runtime before C++ initializers are evaluated.
104  }
105 
106  /// hasUniqueInitializer - Whether the global variable has an initializer, and
107  /// any changes made to the initializer will turn up in the final executable.
108  inline bool hasUniqueInitializer() const {
109  return hasInitializer() &&
110  // It's not safe to modify initializers of global variables with weak
111  // linkage, because the linker might choose to discard the initializer and
112  // use the initializer from another instance of the global variable
113  // instead. It is wrong to modify the initializer of a global variable
114  // with *_odr linkage because then different instances of the global may
115  // have different initializers, breaking the One Definition Rule.
116  !isWeakForLinker() &&
117  // It is not safe to modify initializers of global variables with the
118  // external_initializer marker since the value may be changed at runtime
119  // before C++ initializers are evaluated.
121  }
122 
123  /// getInitializer - Return the initializer for this global variable. It is
124  /// illegal to call this method if the global is external, because we cannot
125  /// tell what the value is initialized to!
126  ///
127  inline const Constant *getInitializer() const {
128  assert(hasInitializer() && "GV doesn't have initializer!");
129  return static_cast<Constant*>(Op<0>().get());
130  }
132  assert(hasInitializer() && "GV doesn't have initializer!");
133  return static_cast<Constant*>(Op<0>().get());
134  }
135  /// setInitializer - Sets the initializer for this global variable, removing
136  /// any existing initializer if InitVal==NULL. If this GV has type T*, the
137  /// initializer must have type T.
138  void setInitializer(Constant *InitVal);
139 
140  /// If the value is a global constant, its value is immutable throughout the
141  /// runtime execution of the program. Assigning a value into the constant
142  /// leads to undefined behavior.
143  ///
144  bool isConstant() const { return isConstantGlobal; }
145  void setConstant(bool Val) { isConstantGlobal = Val; }
146 
147  bool isExternallyInitialized() const {
148  return isExternallyInitializedConstant;
149  }
150  void setExternallyInitialized(bool Val) {
151  isExternallyInitializedConstant = Val;
152  }
153 
154  /// copyAttributesFrom - copy all additional attributes (those not needed to
155  /// create a GlobalVariable) from the GlobalVariable Src to this one.
156  void copyAttributesFrom(const GlobalValue *Src) override;
157 
158  /// removeFromParent - This method unlinks 'this' from the containing module,
159  /// but does not delete it.
160  ///
161  void removeFromParent() override;
162 
163  /// eraseFromParent - This method unlinks 'this' from the containing module
164  /// and deletes it.
165  ///
166  void eraseFromParent() override;
167 
168  // Methods for support type inquiry through isa, cast, and dyn_cast:
169  static inline bool classof(const Value *V) {
170  return V->getValueID() == Value::GlobalVariableVal;
171  }
172 };
173 
174 template <>
176  public OptionalOperandTraits<GlobalVariable> {
177 };
178 
180 
181 } // End llvm namespace
182 
183 #endif
static bool classof(const Value *V)
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
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:79
void setInitializer(Constant *InitVal)
setInitializer - Sets the initializer for this global variable, removing any existing initializer if ...
Definition: Globals.cpp:198
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:194
#define DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CLASS, VALUECLASS)
Macro for generating out-of-class operand accessor definitions.
LinkageTypes Linkage
Definition: GlobalValue.h:79
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
bool isWeakForLinker() const
Definition: GlobalValue.h:297
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:41
void setExternallyInitialized(bool Val)
unsigned getValueID() const
Return an ID for the concrete type of this object.
Definition: Value.h:362
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:148
~GlobalVariable() override
void setConstant(bool Val)
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)
Provide fast operand accessors.
AddressSpace
Definition: NVPTXBaseInfo.h:22
bool hasInitializer() const
Definitions have initializers, declarations don't.
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:39
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
ilist_node - Base class that provides next/prev services for nodes that use ilist_nextprev_traits or ...
Definition: ilist_node.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:128
Compile-time customization of User operands.
Definition: User.h:34
void copyAttributesFrom(const GlobalValue *Src) override
copyAttributesFrom - copy all additional attributes (those not needed to create a GlobalVariable) fro...
Definition: Globals.cpp:221
LLVM Value Representation.
Definition: Value.h:69
bool mayBeOverridden() const
Definition: GlobalValue.h:295
Constant * getInitializer()
void removeFromParent() override
removeFromParent - This method unlinks 'this' from the containing module, but does not delete it...
Definition: Globals.cpp:190