]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/IR/GlobalVariable.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r304460, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / IR / GlobalVariable.h
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/Attributes.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> {
43   friend class SymbolTableListTraits<GlobalVariable>;
44
45   AttributeSet Attrs;
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.
55   GlobalVariable(Type *Ty, bool isConstant, LinkageTypes Linkage,
56                  Constant *Initializer = nullptr, const Twine &Name = "",
57                  ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0,
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,
64                  ThreadLocalMode = NotThreadLocal, unsigned AddressSpace = 0,
65                  bool isExternallyInitialized = false);
66   GlobalVariable(const GlobalVariable &) = delete;
67   GlobalVariable &operator=(const GlobalVariable &) = delete;
68
69   ~GlobalVariable() {
70     dropAllReferences();
71
72     // FIXME: needed by operator delete
73     setGlobalVariableNumOperands(1);
74   }
75
76   // allocate space for exactly one operand
77   void *operator new(size_t s) {
78     return User::operator new(s, 1);
79   }
80
81   void *operator new(size_t, unsigned) = delete;
82
83   /// Provide fast operand accessors
84   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
85
86   /// Definitions have initializers, declarations don't.
87   ///
88   inline bool hasInitializer() const { return !isDeclaration(); }
89
90   /// hasDefinitiveInitializer - Whether the global variable has an initializer,
91   /// and any other instances of the global (this can happen due to weak
92   /// linkage) are guaranteed to have the same initializer.
93   ///
94   /// Note that if you want to transform a global, you must use
95   /// hasUniqueInitializer() instead, because of the *_odr linkage type.
96   ///
97   /// Example:
98   ///
99   /// @a = global SomeType* null - Initializer is both definitive and unique.
100   ///
101   /// @b = global weak SomeType* null - Initializer is neither definitive nor
102   /// unique.
103   ///
104   /// @c = global weak_odr SomeType* null - Initializer is definitive, but not
105   /// unique.
106   inline bool hasDefinitiveInitializer() const {
107     return hasInitializer() &&
108       // The initializer of a global variable may change to something arbitrary
109       // at link time.
110       !isInterposable() &&
111       // The initializer of a global variable with the externally_initialized
112       // marker may change at runtime before C++ initializers are evaluated.
113       !isExternallyInitialized();
114   }
115
116   /// hasUniqueInitializer - Whether the global variable has an initializer, and
117   /// any changes made to the initializer will turn up in the final executable.
118   inline bool hasUniqueInitializer() const {
119     return
120         // We need to be sure this is the definition that will actually be used
121         isStrongDefinitionForLinker() &&
122         // It is not safe to modify initializers of global variables with the
123         // external_initializer marker since the value may be changed at runtime
124         // before C++ initializers are evaluated.
125         !isExternallyInitialized();
126   }
127
128   /// getInitializer - Return the initializer for this global variable.  It is
129   /// illegal to call this method if the global is external, because we cannot
130   /// tell what the value is initialized to!
131   ///
132   inline const Constant *getInitializer() const {
133     assert(hasInitializer() && "GV doesn't have initializer!");
134     return static_cast<Constant*>(Op<0>().get());
135   }
136   inline Constant *getInitializer() {
137     assert(hasInitializer() && "GV doesn't have initializer!");
138     return static_cast<Constant*>(Op<0>().get());
139   }
140   /// setInitializer - Sets the initializer for this global variable, removing
141   /// any existing initializer if InitVal==NULL.  If this GV has type T*, the
142   /// initializer must have type T.
143   void setInitializer(Constant *InitVal);
144
145   /// If the value is a global constant, its value is immutable throughout the
146   /// runtime execution of the program.  Assigning a value into the constant
147   /// leads to undefined behavior.
148   ///
149   bool isConstant() const { return isConstantGlobal; }
150   void setConstant(bool Val) { isConstantGlobal = Val; }
151
152   bool isExternallyInitialized() const {
153     return isExternallyInitializedConstant;
154   }
155   void setExternallyInitialized(bool Val) {
156     isExternallyInitializedConstant = Val;
157   }
158
159   /// copyAttributesFrom - copy all additional attributes (those not needed to
160   /// create a GlobalVariable) from the GlobalVariable Src to this one.
161   void copyAttributesFrom(const GlobalVariable *Src);
162
163   /// removeFromParent - This method unlinks 'this' from the containing module,
164   /// but does not delete it.
165   ///
166   void removeFromParent();
167
168   /// eraseFromParent - This method unlinks 'this' from the containing module
169   /// and deletes it.
170   ///
171   void eraseFromParent();
172
173   /// Drop all references in preparation to destroy the GlobalVariable. This
174   /// drops not only the reference to the initializer but also to any metadata.
175   void dropAllReferences();
176
177   /// Attach a DIGlobalVariableExpression.
178   void addDebugInfo(DIGlobalVariableExpression *GV);
179
180   /// Fill the vector with all debug info attachements.
181   void getDebugInfo(SmallVectorImpl<DIGlobalVariableExpression *> &GVs) const;
182
183   /// Add attribute to this global.
184   void addAttribute(Attribute::AttrKind Kind) {
185     Attrs = Attrs.addAttribute(getContext(), Kind);
186   }
187
188   /// Add attribute to this global.
189   void addAttribute(StringRef Kind, StringRef Val = StringRef()) {
190     Attrs = Attrs.addAttribute(getContext(), Kind, Val);
191   }
192
193   /// Return true if the attribute exists.
194   bool hasAttribute(Attribute::AttrKind Kind) const {
195     return Attrs.hasAttribute(Kind);
196   }
197
198   /// Return true if the attribute exists.
199   bool hasAttribute(StringRef Kind) const {
200     return Attrs.hasAttribute(Kind);
201   }
202
203   /// Return true if any attributes exist.
204   bool hasAttributes() const {
205     return Attrs.hasAttributes();
206   }
207
208   /// Return the attribute object.
209   Attribute getAttribute(Attribute::AttrKind Kind) const {
210     return Attrs.getAttribute(Kind);
211   }
212
213   /// Return the attribute object.
214   Attribute getAttribute(StringRef Kind) const {
215     return Attrs.getAttribute(Kind);
216   }
217
218   /// Return the attribute set for this global
219   AttributeSet getAttributes() const {
220     return Attrs;
221   }
222
223   /// Return attribute set as list with index.
224   /// FIXME: This may not be required once ValueEnumerators
225   /// in bitcode-writer can enumerate attribute-set.
226   AttributeList getAttributesAsList(unsigned index) const {
227     if (!hasAttributes())
228       return AttributeList();
229     std::pair<unsigned, AttributeSet> AS[1] = {{index, Attrs}};
230     return AttributeList::get(getContext(), AS);
231   }
232
233   /// Set attribute list for this global
234   void setAttributes(AttributeSet A) {
235     Attrs = A;
236   }
237
238   // Methods for support type inquiry through isa, cast, and dyn_cast:
239   static inline bool classof(const Value *V) {
240     return V->getValueID() == Value::GlobalVariableVal;
241   }
242 };
243
244 template <>
245 struct OperandTraits<GlobalVariable> :
246   public OptionalOperandTraits<GlobalVariable> {
247 };
248
249 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value)
250
251 } // end namespace llvm
252
253 #endif // LLVM_IR_GLOBALVARIABLE_H