Devirtualizing Value destructor (PR889). Patch by Pawel Kunio!
[oota-llvm.git] / include / llvm / GlobalVariable.h
1 //===-- llvm/GlobalVariable.h - GlobalVariable class ------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 intial 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
25 namespace llvm {
26
27 class Module;
28 class Constant;
29 class PointerType;
30 template<typename ValueSubClass, typename ItemParentClass>
31   class SymbolTableListTraits;
32
33 class GlobalVariable : public GlobalValue {
34   friend class SymbolTableListTraits<GlobalVariable, Module>;
35   void operator=(const GlobalVariable &);     // Do not implement
36   GlobalVariable(const GlobalVariable &);     // Do not implement
37
38   void setParent(Module *parent);
39
40   GlobalVariable *Prev, *Next;
41   void setNext(GlobalVariable *N) { Next = N; }
42   void setPrev(GlobalVariable *N) { Prev = N; }
43
44   bool isConstantGlobal : 1;           // Is this a global constant?
45   bool isThreadLocalSymbol : 1;        // Is this symbol "Thread Local"?
46   Use Initializer;
47
48 protected:
49   static void destroyThis(GlobalVariable*v) {
50     GlobalValue::destroyThis(v);
51   }
52   friend class Value;
53 public:
54   /// GlobalVariable ctor - If a parent module is specified, the global is
55   /// automatically inserted into the end of the specified modules global list.
56   GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
57                  Constant *Initializer = 0, const std::string &Name = "",
58                  Module *Parent = 0, bool ThreadLocal = false);
59   /// GlobalVariable ctor - This creates a global and inserts it before the
60   /// specified other global.
61   GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
62                  Constant *Initializer, const std::string &Name,
63                  GlobalVariable *InsertBefore, bool ThreadLocal = false);
64   
65   /// isDeclaration - Is this global variable lacking an initializer?  If so, 
66   /// the global variable is defined in some other translation unit, and is thus
67   /// only a declaration here.
68   virtual bool isDeclaration() const { return getNumOperands() == 0; }
69
70   /// hasInitializer - Unless a global variable isExternal(), it has an
71   /// initializer.  The initializer for the global variable/constant is held by
72   /// Initializer if an initializer is specified.
73   ///
74   inline bool hasInitializer() const { return !isDeclaration(); }
75
76   /// getInitializer - Return the initializer for this global variable.  It is
77   /// illegal to call this method if the global is external, because we cannot
78   /// tell what the value is initialized to!
79   ///
80   inline Constant *getInitializer() const {
81     assert(hasInitializer() && "GV doesn't have initializer!");
82     return reinterpret_cast<Constant*>(Initializer.get());
83   }
84   inline Constant *getInitializer() {
85     assert(hasInitializer() && "GV doesn't have initializer!");
86     return reinterpret_cast<Constant*>(Initializer.get());
87   }
88   inline void setInitializer(Constant *CPV) {
89     if (CPV == 0) {
90       if (hasInitializer()) {
91         Initializer.set(0);
92         NumOperands = 0;
93       }
94     } else {
95       if (!hasInitializer())
96         NumOperands = 1;
97       Initializer.set(CPV);
98     }
99   }
100
101   /// If the value is a global constant, its value is immutable throughout the
102   /// runtime execution of the program.  Assigning a value into the constant
103   /// leads to undefined behavior.
104   ///
105   bool isConstant() const { return isConstantGlobal; }
106   void setConstant(bool Value) { isConstantGlobal = Value; }
107
108   /// If the value is "Thread Local", its value isn't shared by the threads.
109   bool isThreadLocal() const { return isThreadLocalSymbol; }
110   void setThreadLocal(bool Value) { isThreadLocalSymbol = Value; }
111
112   /// removeFromParent - This method unlinks 'this' from the containing module,
113   /// but does not delete it.
114   ///
115   void removeFromParent();
116
117   /// eraseFromParent - This method unlinks 'this' from the containing module
118   /// and deletes it.
119   ///
120   void eraseFromParent();
121
122   /// Override Constant's implementation of this method so we can
123   /// replace constant initializers.
124   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
125
126   virtual void print(std::ostream &OS) const;
127   void print(std::ostream *OS) const { if (OS) print(*OS); }
128
129   // Methods for support type inquiry through isa, cast, and dyn_cast:
130   static inline bool classof(const GlobalVariable *) { return true; }
131   static inline bool classof(const Value *V) {
132     return V->getValueID() == Value::GlobalVariableVal;
133   }
134 private:
135   // getNext/Prev - Return the next or previous global variable in the list.
136         GlobalVariable *getNext()       { return Next; }
137   const GlobalVariable *getNext() const { return Next; }
138         GlobalVariable *getPrev()       { return Prev; }
139   const GlobalVariable *getPrev() const { return Prev; }
140 };
141
142 } // End llvm namespace
143
144 #endif