GlobalVariable.h revision 314564
1249259Sdim//===-- llvm/GlobalVariable.h - GlobalVariable class ------------*- C++ -*-===//
2249259Sdim//
3249259Sdim//                     The LLVM Compiler Infrastructure
4249259Sdim//
5249259Sdim// This file is distributed under the University of Illinois Open Source
6249259Sdim// License. See LICENSE.TXT for details.
7249259Sdim//
8249259Sdim//===----------------------------------------------------------------------===//
9249259Sdim//
10249259Sdim// This file contains the declaration of the GlobalVariable class, which
11249259Sdim// represents a single global variable (or constant) in the VM.
12249259Sdim//
13249259Sdim// Global variables are constant pointers that refer to hunks of space that are
14249259Sdim// allocated by either the VM, or by the linker in a static compiler.  A global
15249259Sdim// variable may have an initial value, which is copied into the executables .data
16249259Sdim// area.  Global Constants are required to have initializers.
17249259Sdim//
18249259Sdim//===----------------------------------------------------------------------===//
19249259Sdim
20249259Sdim#ifndef LLVM_IR_GLOBALVARIABLE_H
21249259Sdim#define LLVM_IR_GLOBALVARIABLE_H
22249259Sdim
23314564Sdim#include "llvm/ADT/PointerUnion.h"
24249259Sdim#include "llvm/ADT/Twine.h"
25249259Sdim#include "llvm/ADT/ilist_node.h"
26276479Sdim#include "llvm/IR/GlobalObject.h"
27249259Sdim#include "llvm/IR/OperandTraits.h"
28314564Sdim#include "llvm/IR/Value.h"
29314564Sdim#include <cassert>
30314564Sdim#include <cstddef>
31249259Sdim
32249259Sdimnamespace llvm {
33249259Sdim
34314564Sdimclass Constant;
35249259Sdimclass Module;
36314564Sdim
37296417Sdimtemplate <typename ValueSubClass> class SymbolTableListTraits;
38314564Sdimclass DIGlobalVariable;
39314564Sdimclass DIGlobalVariableExpression;
40249259Sdim
41276479Sdimclass GlobalVariable : public GlobalObject, public ilist_node<GlobalVariable> {
42296417Sdim  friend class SymbolTableListTraits<GlobalVariable>;
43249259Sdim
44249259Sdim  bool isConstantGlobal : 1;                   // Is this a global constant?
45249259Sdim  bool isExternallyInitializedConstant : 1;    // Is this a global whose value
46249259Sdim                                               // can change from its initial
47249259Sdim                                               // value before global
48249259Sdim                                               // initializers are run?
49314564Sdim
50249259Sdimpublic:
51249259Sdim  /// GlobalVariable ctor - If a parent module is specified, the global is
52249259Sdim  /// automatically inserted into the end of the specified modules global list.
53249259Sdim  GlobalVariable(Type *Ty, bool isConstant, LinkageTypes Linkage,
54276479Sdim                 Constant *Initializer = nullptr, const Twine &Name = "",
55249259Sdim                 ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0,
56249259Sdim                 bool isExternallyInitialized = false);
57249259Sdim  /// GlobalVariable ctor - This creates a global and inserts it before the
58249259Sdim  /// specified other global.
59249259Sdim  GlobalVariable(Module &M, Type *Ty, bool isConstant,
60249259Sdim                 LinkageTypes Linkage, Constant *Initializer,
61276479Sdim                 const Twine &Name = "", GlobalVariable *InsertBefore = nullptr,
62249259Sdim                 ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0,
63249259Sdim                 bool isExternallyInitialized = false);
64314564Sdim  GlobalVariable(const GlobalVariable &) = delete;
65314564Sdim  GlobalVariable &operator=(const GlobalVariable &) = delete;
66249259Sdim
67288943Sdim  ~GlobalVariable() override {
68309124Sdim    dropAllReferences();
69309124Sdim
70288943Sdim    // FIXME: needed by operator delete
71288943Sdim    setGlobalVariableNumOperands(1);
72249259Sdim  }
73249259Sdim
74314564Sdim  // allocate space for exactly one operand
75314564Sdim  void *operator new(size_t s) {
76314564Sdim    return User::operator new(s, 1);
77314564Sdim  }
78314564Sdim
79314564Sdim  void *operator new(size_t, unsigned) = delete;
80314564Sdim
81249259Sdim  /// Provide fast operand accessors
82249259Sdim  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
83249259Sdim
84261991Sdim  /// Definitions have initializers, declarations don't.
85249259Sdim  ///
86249259Sdim  inline bool hasInitializer() const { return !isDeclaration(); }
87249259Sdim
88249259Sdim  /// hasDefinitiveInitializer - Whether the global variable has an initializer,
89249259Sdim  /// and any other instances of the global (this can happen due to weak
90249259Sdim  /// linkage) are guaranteed to have the same initializer.
91249259Sdim  ///
92249259Sdim  /// Note that if you want to transform a global, you must use
93249259Sdim  /// hasUniqueInitializer() instead, because of the *_odr linkage type.
94249259Sdim  ///
95249259Sdim  /// Example:
96249259Sdim  ///
97249259Sdim  /// @a = global SomeType* null - Initializer is both definitive and unique.
98249259Sdim  ///
99249259Sdim  /// @b = global weak SomeType* null - Initializer is neither definitive nor
100249259Sdim  /// unique.
101249259Sdim  ///
102249259Sdim  /// @c = global weak_odr SomeType* null - Initializer is definitive, but not
103249259Sdim  /// unique.
104249259Sdim  inline bool hasDefinitiveInitializer() const {
105249259Sdim    return hasInitializer() &&
106309124Sdim      // The initializer of a global variable may change to something arbitrary
107309124Sdim      // at link time.
108309124Sdim      !isInterposable() &&
109249259Sdim      // The initializer of a global variable with the externally_initialized
110249259Sdim      // marker may change at runtime before C++ initializers are evaluated.
111249259Sdim      !isExternallyInitialized();
112249259Sdim  }
113249259Sdim
114249259Sdim  /// hasUniqueInitializer - Whether the global variable has an initializer, and
115249259Sdim  /// any changes made to the initializer will turn up in the final executable.
116249259Sdim  inline bool hasUniqueInitializer() const {
117296417Sdim    return
118296417Sdim        // We need to be sure this is the definition that will actually be used
119296417Sdim        isStrongDefinitionForLinker() &&
120296417Sdim        // It is not safe to modify initializers of global variables with the
121296417Sdim        // external_initializer marker since the value may be changed at runtime
122296417Sdim        // before C++ initializers are evaluated.
123296417Sdim        !isExternallyInitialized();
124249259Sdim  }
125249259Sdim
126249259Sdim  /// getInitializer - Return the initializer for this global variable.  It is
127249259Sdim  /// illegal to call this method if the global is external, because we cannot
128249259Sdim  /// tell what the value is initialized to!
129249259Sdim  ///
130249259Sdim  inline const Constant *getInitializer() const {
131249259Sdim    assert(hasInitializer() && "GV doesn't have initializer!");
132249259Sdim    return static_cast<Constant*>(Op<0>().get());
133249259Sdim  }
134249259Sdim  inline Constant *getInitializer() {
135249259Sdim    assert(hasInitializer() && "GV doesn't have initializer!");
136249259Sdim    return static_cast<Constant*>(Op<0>().get());
137249259Sdim  }
138249259Sdim  /// setInitializer - Sets the initializer for this global variable, removing
139249259Sdim  /// any existing initializer if InitVal==NULL.  If this GV has type T*, the
140249259Sdim  /// initializer must have type T.
141249259Sdim  void setInitializer(Constant *InitVal);
142249259Sdim
143249259Sdim  /// If the value is a global constant, its value is immutable throughout the
144249259Sdim  /// runtime execution of the program.  Assigning a value into the constant
145249259Sdim  /// leads to undefined behavior.
146249259Sdim  ///
147249259Sdim  bool isConstant() const { return isConstantGlobal; }
148249259Sdim  void setConstant(bool Val) { isConstantGlobal = Val; }
149249259Sdim
150249259Sdim  bool isExternallyInitialized() const {
151249259Sdim    return isExternallyInitializedConstant;
152249259Sdim  }
153249259Sdim  void setExternallyInitialized(bool Val) {
154249259Sdim    isExternallyInitializedConstant = Val;
155249259Sdim  }
156249259Sdim
157249259Sdim  /// copyAttributesFrom - copy all additional attributes (those not needed to
158249259Sdim  /// create a GlobalVariable) from the GlobalVariable Src to this one.
159276479Sdim  void copyAttributesFrom(const GlobalValue *Src) override;
160249259Sdim
161249259Sdim  /// removeFromParent - This method unlinks 'this' from the containing module,
162249259Sdim  /// but does not delete it.
163249259Sdim  ///
164276479Sdim  void removeFromParent() override;
165249259Sdim
166249259Sdim  /// eraseFromParent - This method unlinks 'this' from the containing module
167249259Sdim  /// and deletes it.
168249259Sdim  ///
169276479Sdim  void eraseFromParent() override;
170249259Sdim
171309124Sdim  /// Drop all references in preparation to destroy the GlobalVariable. This
172309124Sdim  /// drops not only the reference to the initializer but also to any metadata.
173309124Sdim  void dropAllReferences();
174309124Sdim
175314564Sdim  /// Attach a DIGlobalVariableExpression.
176314564Sdim  void addDebugInfo(DIGlobalVariableExpression *GV);
177314564Sdim
178314564Sdim  /// Fill the vector with all debug info attachements.
179314564Sdim  void getDebugInfo(SmallVectorImpl<DIGlobalVariableExpression *> &GVs) const;
180314564Sdim
181249259Sdim  // Methods for support type inquiry through isa, cast, and dyn_cast:
182249259Sdim  static inline bool classof(const Value *V) {
183249259Sdim    return V->getValueID() == Value::GlobalVariableVal;
184249259Sdim  }
185249259Sdim};
186249259Sdim
187249259Sdimtemplate <>
188249259Sdimstruct OperandTraits<GlobalVariable> :
189249259Sdim  public OptionalOperandTraits<GlobalVariable> {
190249259Sdim};
191249259Sdim
192249259SdimDEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value)
193249259Sdim
194314564Sdim} // end namespace llvm
195249259Sdim
196314564Sdim#endif // LLVM_IR_GLOBALVARIABLE_H
197