GlobalVariable.h revision 341825
1231990Smp//===-- llvm/GlobalVariable.h - GlobalVariable class ------------*- C++ -*-===//
259243Sobrien//
359243Sobrien//                     The LLVM Compiler Infrastructure
459243Sobrien//
559243Sobrien// This file is distributed under the University of Illinois Open Source
659243Sobrien// License. See LICENSE.TXT for details.
759243Sobrien//
859243Sobrien//===----------------------------------------------------------------------===//
959243Sobrien//
1059243Sobrien// This file contains the declaration of the GlobalVariable class, which
1159243Sobrien// represents a single global variable (or constant) in the VM.
1259243Sobrien//
1359243Sobrien// Global variables are constant pointers that refer to hunks of space that are
1459243Sobrien// allocated by either the VM, or by the linker in a static compiler.  A global
1559243Sobrien// variable may have an initial value, which is copied into the executables .data
1659243Sobrien// area.  Global Constants are required to have initializers.
17100616Smp//
1859243Sobrien//===----------------------------------------------------------------------===//
1959243Sobrien
2059243Sobrien#ifndef LLVM_IR_GLOBALVARIABLE_H
2159243Sobrien#define LLVM_IR_GLOBALVARIABLE_H
2259243Sobrien
2359243Sobrien#include "llvm/ADT/PointerUnion.h"
2459243Sobrien#include "llvm/ADT/Twine.h"
2559243Sobrien#include "llvm/ADT/ilist_node.h"
2659243Sobrien#include "llvm/IR/Attributes.h"
2759243Sobrien#include "llvm/IR/GlobalObject.h"
2859243Sobrien#include "llvm/IR/OperandTraits.h"
2959243Sobrien#include "llvm/IR/Value.h"
3059243Sobrien#include <cassert>
3159243Sobrien#include <cstddef>
3259243Sobrien
3359243Sobriennamespace llvm {
3459243Sobrien
35231990Smpclass Constant;
3659243Sobrienclass Module;
37231990Smp
3859243Sobrientemplate <typename ValueSubClass> class SymbolTableListTraits;
3959243Sobrienclass DIGlobalVariable;
40145479Smpclass DIGlobalVariableExpression;
41167465Smp
4259243Sobrienclass GlobalVariable : public GlobalObject, public ilist_node<GlobalVariable> {
4359243Sobrien  friend class SymbolTableListTraits<GlobalVariable>;
44167465Smp
45167465Smp  AttributeSet Attrs;
4659243Sobrien  bool isConstantGlobal : 1;                   // Is this a global constant?
4759243Sobrien  bool isExternallyInitializedConstant : 1;    // Is this a global whose value
4859243Sobrien                                               // can change from its initial
4959243Sobrien                                               // value before global
5059243Sobrien                                               // initializers are run?
5159243Sobrien
5259243Sobrienpublic:
5359243Sobrien  /// GlobalVariable ctor - If a parent module is specified, the global is
5459243Sobrien  /// automatically inserted into the end of the specified modules global list.
5559243Sobrien  GlobalVariable(Type *Ty, bool isConstant, LinkageTypes Linkage,
5659243Sobrien                 Constant *Initializer = nullptr, const Twine &Name = "",
5759243Sobrien                 ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0,
5859243Sobrien                 bool isExternallyInitialized = false);
59231990Smp  /// GlobalVariable ctor - This creates a global and inserts it before the
60231990Smp  /// specified other global.
61231990Smp  GlobalVariable(Module &M, Type *Ty, bool isConstant,
62231990Smp                 LinkageTypes Linkage, Constant *Initializer,
63231990Smp                 const Twine &Name = "", GlobalVariable *InsertBefore = nullptr,
64231990Smp                 ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0,
65231990Smp                 bool isExternallyInitialized = false);
66231990Smp  GlobalVariable(const GlobalVariable &) = delete;
67231990Smp  GlobalVariable &operator=(const GlobalVariable &) = delete;
68231990Smp
69231990Smp  ~GlobalVariable() {
70231990Smp    dropAllReferences();
71231990Smp  }
72231990Smp
73231990Smp  // allocate space for exactly one operand
74231990Smp  void *operator new(size_t s) {
75231990Smp    return User::operator new(s, 1);
76231990Smp  }
77231990Smp
78231990Smp  // delete space for exactly one operand as created in the corresponding new operator
79231990Smp  void operator delete(void *ptr){
80231990Smp    assert(ptr != nullptr && "must not be nullptr");
81231990Smp    User *Obj = static_cast<User *>(ptr);
82231990Smp    // Number of operands can be set to 0 after construction and initialization. Make sure
83231990Smp    // that number of operands is reset to 1, as this is needed in User::operator delete
84231990Smp    Obj->setGlobalVariableNumOperands(1);
85231990Smp    User::operator delete(Obj);
86231990Smp  }
87231990Smp
88231990Smp  /// Provide fast operand accessors
89231990Smp  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
90231990Smp
91231990Smp  /// Definitions have initializers, declarations don't.
92231990Smp  ///
93231990Smp  inline bool hasInitializer() const { return !isDeclaration(); }
94231990Smp
95231990Smp  /// hasDefinitiveInitializer - Whether the global variable has an initializer,
96231990Smp  /// and any other instances of the global (this can happen due to weak
97231990Smp  /// linkage) are guaranteed to have the same initializer.
98231990Smp  ///
99231990Smp  /// Note that if you want to transform a global, you must use
100231990Smp  /// hasUniqueInitializer() instead, because of the *_odr linkage type.
101231990Smp  ///
102231990Smp  /// Example:
103231990Smp  ///
104231990Smp  /// @a = global SomeType* null - Initializer is both definitive and unique.
105231990Smp  ///
106231990Smp  /// @b = global weak SomeType* null - Initializer is neither definitive nor
107231990Smp  /// unique.
108231990Smp  ///
109231990Smp  /// @c = global weak_odr SomeType* null - Initializer is definitive, but not
110231990Smp  /// unique.
111231990Smp  inline bool hasDefinitiveInitializer() const {
112231990Smp    return hasInitializer() &&
113231990Smp      // The initializer of a global variable may change to something arbitrary
114231990Smp      // at link time.
115231990Smp      !isInterposable() &&
116231990Smp      // The initializer of a global variable with the externally_initialized
117231990Smp      // marker may change at runtime before C++ initializers are evaluated.
118231990Smp      !isExternallyInitialized();
119231990Smp  }
120231990Smp
121231990Smp  /// hasUniqueInitializer - Whether the global variable has an initializer, and
122231990Smp  /// any changes made to the initializer will turn up in the final executable.
123231990Smp  inline bool hasUniqueInitializer() const {
124231990Smp    return
125231990Smp        // We need to be sure this is the definition that will actually be used
126231990Smp        isStrongDefinitionForLinker() &&
127231990Smp        // It is not safe to modify initializers of global variables with the
128231990Smp        // external_initializer marker since the value may be changed at runtime
129231990Smp        // before C++ initializers are evaluated.
130231990Smp        !isExternallyInitialized();
131231990Smp  }
132231990Smp
133231990Smp  /// getInitializer - Return the initializer for this global variable.  It is
134231990Smp  /// illegal to call this method if the global is external, because we cannot
135231990Smp  /// tell what the value is initialized to!
136231990Smp  ///
137231990Smp  inline const Constant *getInitializer() const {
138231990Smp    assert(hasInitializer() && "GV doesn't have initializer!");
139231990Smp    return static_cast<Constant*>(Op<0>().get());
140231990Smp  }
141231990Smp  inline Constant *getInitializer() {
142231990Smp    assert(hasInitializer() && "GV doesn't have initializer!");
14359243Sobrien    return static_cast<Constant*>(Op<0>().get());
144231990Smp  }
145231990Smp  /// setInitializer - Sets the initializer for this global variable, removing
146231990Smp  /// any existing initializer if InitVal==NULL.  If this GV has type T*, the
14759243Sobrien  /// initializer must have type T.
148145479Smp  void setInitializer(Constant *InitVal);
14959243Sobrien
15059243Sobrien  /// If the value is a global constant, its value is immutable throughout the
15159243Sobrien  /// runtime execution of the program.  Assigning a value into the constant
15259243Sobrien  /// leads to undefined behavior.
15359243Sobrien  ///
15459243Sobrien  bool isConstant() const { return isConstantGlobal; }
155167465Smp  void setConstant(bool Val) { isConstantGlobal = Val; }
156167465Smp
157167465Smp  bool isExternallyInitialized() const {
158167465Smp    return isExternallyInitializedConstant;
15959243Sobrien  }
160167465Smp  void setExternallyInitialized(bool Val) {
16159243Sobrien    isExternallyInitializedConstant = Val;
16259243Sobrien  }
163231990Smp
164231990Smp  /// copyAttributesFrom - copy all additional attributes (those not needed to
16559243Sobrien  /// create a GlobalVariable) from the GlobalVariable Src to this one.
16659243Sobrien  void copyAttributesFrom(const GlobalVariable *Src);
167231990Smp
168231990Smp  /// removeFromParent - This method unlinks 'this' from the containing module,
169231990Smp  /// but does not delete it.
170231990Smp  ///
171231990Smp  void removeFromParent();
172231990Smp
173231990Smp  /// eraseFromParent - This method unlinks 'this' from the containing module
174231990Smp  /// and deletes it.
175231990Smp  ///
176231990Smp  void eraseFromParent();
177231990Smp
178231990Smp  /// Drop all references in preparation to destroy the GlobalVariable. This
179231990Smp  /// drops not only the reference to the initializer but also to any metadata.
180231990Smp  void dropAllReferences();
181231990Smp
182231990Smp  /// Attach a DIGlobalVariableExpression.
183231990Smp  void addDebugInfo(DIGlobalVariableExpression *GV);
184231990Smp
185231990Smp  /// Fill the vector with all debug info attachements.
186231990Smp  void getDebugInfo(SmallVectorImpl<DIGlobalVariableExpression *> &GVs) const;
187231990Smp
188231990Smp  /// Add attribute to this global.
189231990Smp  void addAttribute(Attribute::AttrKind Kind) {
190231990Smp    Attrs = Attrs.addAttribute(getContext(), Kind);
191231990Smp  }
192231990Smp
193231990Smp  /// Add attribute to this global.
194231990Smp  void addAttribute(StringRef Kind, StringRef Val = StringRef()) {
195231990Smp    Attrs = Attrs.addAttribute(getContext(), Kind, Val);
196231990Smp  }
197231990Smp
198231990Smp  /// Return true if the attribute exists.
199231990Smp  bool hasAttribute(Attribute::AttrKind Kind) const {
200231990Smp    return Attrs.hasAttribute(Kind);
201231990Smp  }
202231990Smp
203231990Smp  /// Return true if the attribute exists.
204231990Smp  bool hasAttribute(StringRef Kind) const {
205231990Smp    return Attrs.hasAttribute(Kind);
206231990Smp  }
207231990Smp
208231990Smp  /// Return true if any attributes exist.
209231990Smp  bool hasAttributes() const {
210231990Smp    return Attrs.hasAttributes();
211231990Smp  }
212231990Smp
213231990Smp  /// Return the attribute object.
214231990Smp  Attribute getAttribute(Attribute::AttrKind Kind) const {
215231990Smp    return Attrs.getAttribute(Kind);
216231990Smp  }
217231990Smp
218231990Smp  /// Return the attribute object.
219231990Smp  Attribute getAttribute(StringRef Kind) const {
220231990Smp    return Attrs.getAttribute(Kind);
221231990Smp  }
222231990Smp
223231990Smp  /// Return the attribute set for this global
224231990Smp  AttributeSet getAttributes() const {
225231990Smp    return Attrs;
226231990Smp  }
227231990Smp
228231990Smp  /// Return attribute set as list with index.
229231990Smp  /// FIXME: This may not be required once ValueEnumerators
230231990Smp  /// in bitcode-writer can enumerate attribute-set.
231231990Smp  AttributeList getAttributesAsList(unsigned index) const {
232231990Smp    if (!hasAttributes())
233231990Smp      return AttributeList();
234231990Smp    std::pair<unsigned, AttributeSet> AS[1] = {{index, Attrs}};
235231990Smp    return AttributeList::get(getContext(), AS);
236231990Smp  }
237231990Smp
238231990Smp  /// Set attribute list for this global
239231990Smp  void setAttributes(AttributeSet A) {
240231990Smp    Attrs = A;
241231990Smp  }
242231990Smp
243231990Smp  /// Check if section name is present
244231990Smp  bool hasImplicitSection() const {
245231990Smp    return getAttributes().hasAttribute("bss-section") ||
246231990Smp           getAttributes().hasAttribute("data-section") ||
247231990Smp           getAttributes().hasAttribute("rodata-section");
248231990Smp  }
249231990Smp
250231990Smp  // Methods for support type inquiry through isa, cast, and dyn_cast:
251231990Smp  static bool classof(const Value *V) {
252231990Smp    return V->getValueID() == Value::GlobalVariableVal;
253231990Smp  }
254231990Smp};
255231990Smp
256231990Smptemplate <>
257231990Smpstruct OperandTraits<GlobalVariable> :
258231990Smp  public OptionalOperandTraits<GlobalVariable> {
259231990Smp};
260231990Smp
261231990SmpDEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value)
262231990Smp
263231990Smp} // end namespace llvm
264231990Smp
265231990Smp#endif // LLVM_IR_GLOBALVARIABLE_H
266231990Smp