fdde5aed96e8c088f76770f6befa39142844b8ce
[oota-llvm.git] / include / llvm / Value.h
1 //===-- llvm/Value.h - Definition of the Value class -------------*- C++ -*--=//
2 //
3 // This file defines the very important Value class.  This is subclassed by a
4 // bunch of other important classes, like Instruction, Function, Type, etc...
5 //
6 // This file also defines the Use<> template for users of value.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_VALUE_H
11 #define LLVM_VALUE_H
12
13 #include <vector>
14 #include "llvm/Annotation.h"
15 #include "llvm/AbstractTypeUser.h"
16 #include "Support/Casting.h"
17 #include <iostream>
18
19 class User;
20 class Type;
21 class Constant;
22 class Argument;
23 class Instruction;
24 class BasicBlock;
25 class GlobalValue;
26 class Function;
27 class GlobalVariable;
28 class SymbolTable;
29
30 //===----------------------------------------------------------------------===//
31 //                                 Value Class
32 //===----------------------------------------------------------------------===//
33
34 class Value : public Annotable,         // Values are annotable
35               public AbstractTypeUser { // Values use potentially abstract types
36 public:
37   enum ValueTy {
38     TypeVal,                // This is an instance of Type
39     ConstantVal,            // This is an instance of Constant
40     ArgumentVal,            // This is an instance of Argument
41     InstructionVal,         // This is an instance of Instruction
42     BasicBlockVal,          // This is an instance of BasicBlock
43     FunctionVal,            // This is an instance of Function
44     GlobalVariableVal,      // This is an instance of GlobalVariable
45   };
46
47 private:
48   std::vector<User *> Uses;
49   std::string Name;
50   PATypeHandle<Type> Ty;
51   ValueTy VTy;
52
53   void operator=(const Value &);     // Do not implement
54   Value(const Value &);              // Do not implement
55 protected:
56   inline void setType(const Type *ty) { Ty = ty; }
57 public:
58   Value(const Type *Ty, ValueTy vty, const std::string &name = "");
59   virtual ~Value();
60   
61   // Support for debugging 
62   void dump() const;
63
64   // Implement operator<< on Value...
65   virtual void print(std::ostream &O) const = 0;
66   
67   // All values can potentially be typed
68   inline const Type *getType() const { return Ty; }
69   
70   // All values can potentially be named...
71   inline bool               hasName() const { return Name != ""; }
72   inline const std::string &getName() const { return Name; }
73
74   virtual void setName(const std::string &name, SymbolTable * = 0) {
75     Name = name;
76   }
77   
78   // Methods for determining the subtype of this Value.  The getValueType()
79   // method returns the type of the value directly.  The cast*() methods are
80   // equivalent to using dynamic_cast<>... if the cast is successful, this is
81   // returned, otherwise you get a null pointer.
82   //
83   // The family of functions Val->cast<type>Asserting() is used in the same
84   // way as the Val->cast<type>() instructions, but they assert the expected
85   // type instead of checking it at runtime.
86   //
87   inline ValueTy getValueType() const { return VTy; }
88   
89   // replaceAllUsesWith - Go through the uses list for this definition and make
90   // each use point to "D" instead of "this".  After this completes, 'this's 
91   // use list should be empty.
92   //
93   void replaceAllUsesWith(Value *D);
94
95   // refineAbstractType - This function is implemented because we use
96   // potentially abstract types, and these types may be resolved to more
97   // concrete types after we are constructed.
98   //
99   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
100   
101   //----------------------------------------------------------------------
102   // Methods for handling the vector of uses of this Value.
103   //
104   typedef std::vector<User*>::iterator       use_iterator;
105   typedef std::vector<User*>::const_iterator use_const_iterator;
106
107   inline unsigned           use_size()  const { return Uses.size();  }
108   inline bool               use_empty() const { return Uses.empty(); }
109   inline use_iterator       use_begin()       { return Uses.begin(); }
110   inline use_const_iterator use_begin() const { return Uses.begin(); }
111   inline use_iterator       use_end()         { return Uses.end();   }
112   inline use_const_iterator use_end()   const { return Uses.end();   }
113   inline User              *use_back()        { return Uses.back();  }
114   inline const User        *use_back()  const { return Uses.back();  }
115
116   inline void use_push_back(User *I)   { Uses.push_back(I); }
117   User *use_remove(use_iterator &I);
118
119   inline void addUse(User *I)      { Uses.push_back(I); }
120   void killUse(User *I);
121 };
122
123 inline std::ostream &operator<<(std::ostream &OS, const Value *V) {
124   if (V == 0)
125     OS << "<null> value!\n";
126   else
127     V->print(OS);
128   return OS;
129 }
130
131 inline std::ostream &operator<<(std::ostream &OS, const Value &V) {
132   V.print(OS);
133   return OS;
134 }
135
136
137 //===----------------------------------------------------------------------===//
138 //                                 UseTy Class
139 //===----------------------------------------------------------------------===//
140
141 // UseTy and it's friendly typedefs (Use) are here to make keeping the "use" 
142 // list of a definition node up-to-date really easy.
143 //
144 template<class ValueSubclass>
145 class UseTy {
146   ValueSubclass *Val;
147   User *U;
148 public:
149   inline UseTy<ValueSubclass>(ValueSubclass *v, User *user) {
150     Val = v; U = user;
151     if (Val) Val->addUse(U);
152   }
153
154   inline ~UseTy<ValueSubclass>() { if (Val) Val->killUse(U); }
155
156   inline operator ValueSubclass *() const { return Val; }
157
158   inline UseTy<ValueSubclass>(const UseTy<ValueSubclass> &user) {
159     Val = 0;
160     U = user.U;
161     operator=(user.Val);
162   }
163   inline ValueSubclass *operator=(ValueSubclass *V) { 
164     if (Val) Val->killUse(U);
165     Val = V;
166     if (V) V->addUse(U);
167     return V;
168   }
169
170   inline       ValueSubclass *operator->()       { return Val; }
171   inline const ValueSubclass *operator->() const { return Val; }
172
173   inline       ValueSubclass *get()       { return Val; }
174   inline const ValueSubclass *get() const { return Val; }
175
176   inline UseTy<ValueSubclass> &operator=(const UseTy<ValueSubclass> &user) {
177     if (Val) Val->killUse(U);
178     Val = user.Val;
179     Val->addUse(U);
180     return *this;
181   }
182 };
183
184 typedef UseTy<Value> Use;    // Provide Use as a common UseTy type
185
186 template<typename From> struct simplify_type<UseTy<From> > {
187   typedef typename simplify_type<From*>::SimpleType SimpleType;
188   
189   static SimpleType getSimplifiedValue(const UseTy<From> &Val) {
190     return (SimpleType)Val.get();
191   }
192 };
193 template<typename From> struct simplify_type<const UseTy<From> > {
194   typedef typename simplify_type<From*>::SimpleType SimpleType;
195   
196   static SimpleType getSimplifiedValue(const UseTy<From> &Val) {
197     return (SimpleType)Val.get();
198   }
199 };
200
201 // isa - Provide some specializations of isa so that we don't have to include
202 // the subtype header files to test to see if the value is a subclass...
203 //
204 template <> inline bool isa_impl<Type, Value>(const Value &Val) { 
205   return Val.getValueType() == Value::TypeVal;
206 }
207 template <> inline bool isa_impl<Constant, Value>(const Value &Val) { 
208   return Val.getValueType() == Value::ConstantVal; 
209 }
210 template <> inline bool isa_impl<Argument, Value>(const Value &Val) { 
211   return Val.getValueType() == Value::ArgumentVal;
212 }
213 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) { 
214   return Val.getValueType() == Value::InstructionVal;
215 }
216 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) { 
217   return Val.getValueType() == Value::BasicBlockVal;
218 }
219 template <> inline bool isa_impl<Function, Value>(const Value &Val) { 
220   return Val.getValueType() == Value::FunctionVal;
221 }
222 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) { 
223   return Val.getValueType() == Value::GlobalVariableVal;
224 }
225 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) { 
226   return isa<GlobalVariable>(Val) || isa<Function>(Val);
227 }
228
229 #endif