Assume lane masks are always precise
[oota-llvm.git] / 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/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> class SymbolTableListTraits;
33
34 class GlobalVariable : public GlobalObject, public ilist_node<GlobalVariable> {
35   friend class SymbolTableListTraits<GlobalVariable>;
36   void *operator new(size_t, unsigned) = delete;
37   void operator=(const GlobalVariable &) = delete;
38   GlobalVariable(const GlobalVariable &) = delete;
39
40   void setParent(Module *parent);
41
42   bool isConstantGlobal : 1;                   // Is this a global constant?
43   bool isExternallyInitializedConstant : 1;    // Is this a global whose value
44                                                // can change from its initial
45                                                // value before global
46                                                // initializers are run?
47 public:
48   // allocate space for exactly one operand
49   void *operator new(size_t s) {
50     return User::operator new(s, 1);
51   }
52
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
67   ~GlobalVariable() override {
68     // FIXME: needed by operator delete
69     setGlobalVariableNumOperands(1);
70   }
71
72   /// Provide fast operand accessors
73   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
74
75   /// Definitions have initializers, declarations don't.
76   ///
77   inline bool hasInitializer() const { return !isDeclaration(); }
78
79   /// hasDefinitiveInitializer - Whether the global variable has an initializer,
80   /// and any other instances of the global (this can happen due to weak
81   /// linkage) are guaranteed to have the same initializer.
82   ///
83   /// Note that if you want to transform a global, you must use
84   /// hasUniqueInitializer() instead, because of the *_odr linkage type.
85   ///
86   /// Example:
87   ///
88   /// @a = global SomeType* null - Initializer is both definitive and unique.
89   ///
90   /// @b = global weak SomeType* null - Initializer is neither definitive nor
91   /// unique.
92   ///
93   /// @c = global weak_odr SomeType* null - Initializer is definitive, but not
94   /// unique.
95   inline bool hasDefinitiveInitializer() const {
96     return hasInitializer() &&
97       // The initializer of a global variable with weak linkage may change at
98       // link time.
99       !mayBeOverridden() &&
100       // The initializer of a global variable with the externally_initialized
101       // marker may change at runtime before C++ initializers are evaluated.
102       !isExternallyInitialized();
103   }
104
105   /// hasUniqueInitializer - Whether the global variable has an initializer, and
106   /// any changes made to the initializer will turn up in the final executable.
107   inline bool hasUniqueInitializer() const {
108     return hasInitializer() &&
109       // It's not safe to modify initializers of global variables with weak
110       // linkage, because the linker might choose to discard the initializer and
111       // use the initializer from another instance of the global variable
112       // instead. It is wrong to modify the initializer of a global variable
113       // with *_odr linkage because then different instances of the global may
114       // have different initializers, breaking the One Definition Rule.
115       !isWeakForLinker() &&
116       // It is not safe to modify initializers of global variables with the
117       // external_initializer marker since the value may be changed at runtime
118       // before C++ initializers are evaluated.
119       !isExternallyInitialized();
120   }
121
122   /// getInitializer - Return the initializer for this global variable.  It is
123   /// illegal to call this method if the global is external, because we cannot
124   /// tell what the value is initialized to!
125   ///
126   inline const Constant *getInitializer() const {
127     assert(hasInitializer() && "GV doesn't have initializer!");
128     return static_cast<Constant*>(Op<0>().get());
129   }
130   inline Constant *getInitializer() {
131     assert(hasInitializer() && "GV doesn't have initializer!");
132     return static_cast<Constant*>(Op<0>().get());
133   }
134   /// setInitializer - Sets the initializer for this global variable, removing
135   /// any existing initializer if InitVal==NULL.  If this GV has type T*, the
136   /// initializer must have type T.
137   void setInitializer(Constant *InitVal);
138
139   /// If the value is a global constant, its value is immutable throughout the
140   /// runtime execution of the program.  Assigning a value into the constant
141   /// leads to undefined behavior.
142   ///
143   bool isConstant() const { return isConstantGlobal; }
144   void setConstant(bool Val) { isConstantGlobal = Val; }
145
146   bool isExternallyInitialized() const {
147     return isExternallyInitializedConstant;
148   }
149   void setExternallyInitialized(bool Val) {
150     isExternallyInitializedConstant = Val;
151   }
152
153   /// copyAttributesFrom - copy all additional attributes (those not needed to
154   /// create a GlobalVariable) from the GlobalVariable Src to this one.
155   void copyAttributesFrom(const GlobalValue *Src) override;
156
157   /// removeFromParent - This method unlinks 'this' from the containing module,
158   /// but does not delete it.
159   ///
160   void removeFromParent() override;
161
162   /// eraseFromParent - This method unlinks 'this' from the containing module
163   /// and deletes it.
164   ///
165   void eraseFromParent() override;
166
167   // Methods for support type inquiry through isa, cast, and dyn_cast:
168   static inline bool classof(const Value *V) {
169     return V->getValueID() == Value::GlobalVariableVal;
170   }
171 };
172
173 template <>
174 struct OperandTraits<GlobalVariable> :
175   public OptionalOperandTraits<GlobalVariable> {
176 };
177
178 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value)
179
180 } // End llvm namespace
181
182 #endif