Fix BasicAA's recursion detection so that it doesn't pessimize
[oota-llvm.git] / include / llvm / 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_GLOBAL_VARIABLE_H
21 #define LLVM_GLOBAL_VARIABLE_H
22
23 #include "llvm/GlobalValue.h"
24 #include "llvm/OperandTraits.h"
25 #include "llvm/ADT/ilist_node.h"
26 #include "llvm/ADT/Twine.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 GlobalValue, public ilist_node<GlobalVariable> {
36   friend class SymbolTableListTraits<GlobalVariable, Module>;
37   void *operator new(size_t, unsigned);       // Do not implement
38   void operator=(const GlobalVariable &);     // Do not implement
39   GlobalVariable(const GlobalVariable &);     // Do not implement
40
41   void setParent(Module *parent);
42
43   bool isConstantGlobal : 1;           // Is this a global constant?
44   bool isThreadLocalSymbol : 1;        // Is this symbol "Thread Local"?
45
46 public:
47   // allocate space for exactly one operand
48   void *operator new(size_t s) {
49     return User::operator new(s, 1);
50   }
51   /// GlobalVariable ctor - If a parent module is specified, the global is
52   /// automatically inserted into the end of the specified modules global list.
53   GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
54                  Constant *Initializer = 0, const Twine &Name = "",
55                  bool ThreadLocal = false, unsigned AddressSpace = 0);
56   /// GlobalVariable ctor - This creates a global and inserts it before the
57   /// specified other global.
58   GlobalVariable(Module &M, const Type *Ty, bool isConstant,
59                  LinkageTypes Linkage, Constant *Initializer,
60                  const Twine &Name,
61                  GlobalVariable *InsertBefore = 0, bool ThreadLocal = false,
62                  unsigned AddressSpace = 0);
63
64   ~GlobalVariable() {
65     NumOperands = 1; // FIXME: needed by operator delete
66   }
67
68   /// Provide fast operand accessors
69   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
70
71   /// isDeclaration - Is this global variable lacking an initializer?  If so,
72   /// the global variable is defined in some other translation unit, and is thus
73   /// only a declaration here.
74   virtual bool isDeclaration() const { return getNumOperands() == 0; }
75
76   /// hasInitializer - Unless a global variable isExternal(), it has an
77   /// initializer.  The initializer for the global variable/constant is held by
78   /// Initializer if an initializer is specified.
79   ///
80   inline bool hasInitializer() const { return !isDeclaration(); }
81
82   /// hasDefinitiveInitializer - Whether the global variable has an initializer,
83   /// and any other instances of the global (this can happen due to weak
84   /// linkage) are guaranteed to have the same initializer.
85   ///
86   /// Note that if you want to transform a global, you must use
87   /// hasUniqueInitializer() instead, because of the *_odr linkage type.
88   ///
89   /// Example:
90   ///
91   /// @a = global SomeType* null - Initializer is both definitive and unique.
92   ///
93   /// @b = global weak SomeType* null - Initializer is neither definitive nor
94   /// unique.
95   ///
96   /// @c = global weak_odr SomeType* null - Initializer is definitive, but not
97   /// unique.
98   inline bool hasDefinitiveInitializer() const {
99     return hasInitializer() &&
100       // The initializer of a global variable with weak linkage may change at
101       // link time.
102       !mayBeOverridden();
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   }
117
118   /// getInitializer - Return the initializer for this global variable.  It is
119   /// illegal to call this method if the global is external, because we cannot
120   /// tell what the value is initialized to!
121   ///
122   inline /*const FIXME*/ Constant *getInitializer() const {
123     assert(hasInitializer() && "GV doesn't have initializer!");
124     return static_cast<Constant*>(Op<0>().get());
125   }
126   inline Constant *getInitializer() {
127     assert(hasInitializer() && "GV doesn't have initializer!");
128     return static_cast<Constant*>(Op<0>().get());
129   }
130   /// setInitializer - Sets the initializer for this global variable, removing
131   /// any existing initializer if InitVal==NULL.  If this GV has type T*, the
132   /// initializer must have type T.
133   void setInitializer(Constant *InitVal);
134
135   /// If the value is a global constant, its value is immutable throughout the
136   /// runtime execution of the program.  Assigning a value into the constant
137   /// leads to undefined behavior.
138   ///
139   bool isConstant() const { return isConstantGlobal; }
140   void setConstant(bool Val) { isConstantGlobal = Val; }
141
142   /// If the value is "Thread Local", its value isn't shared by the threads.
143   bool isThreadLocal() const { return isThreadLocalSymbol; }
144   void setThreadLocal(bool Val) { isThreadLocalSymbol = Val; }
145
146   /// copyAttributesFrom - copy all additional attributes (those not needed to
147   /// create a GlobalVariable) from the GlobalVariable Src to this one.
148   void copyAttributesFrom(const GlobalValue *Src);
149
150   /// removeFromParent - This method unlinks 'this' from the containing module,
151   /// but does not delete it.
152   ///
153   virtual void removeFromParent();
154
155   /// eraseFromParent - This method unlinks 'this' from the containing module
156   /// and deletes it.
157   ///
158   virtual void eraseFromParent();
159
160   /// Override Constant's implementation of this method so we can
161   /// replace constant initializers.
162   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
163
164   // Methods for support type inquiry through isa, cast, and dyn_cast:
165   static inline bool classof(const GlobalVariable *) { return true; }
166   static inline bool classof(const Value *V) {
167     return V->getValueID() == Value::GlobalVariableVal;
168   }
169 };
170
171 template <>
172 struct OperandTraits<GlobalVariable> :
173   public OptionalOperandTraits<GlobalVariable> {
174 };
175
176 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value)
177
178 } // End llvm namespace
179
180 #endif