437a380801f995a9ea886e2c59e6c55175794b6b
[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 "llvm/AbstractTypeUser.h"
14 #include "Support/Annotation.h"
15 #include "Support/Casting.h"
16 #include <iostream>
17 #include <vector>
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 /// Value - The base class of all values computed by a program that may be used
35 /// as operands to other values.
36 ///
37 class Value : public Annotable,         // Values are annotable
38               public AbstractTypeUser { // Values use potentially abstract types
39 public:
40   enum ValueTy {
41     TypeVal,                // This is an instance of Type
42     ConstantVal,            // This is an instance of Constant
43     ArgumentVal,            // This is an instance of Argument
44     InstructionVal,         // This is an instance of Instruction
45     BasicBlockVal,          // This is an instance of BasicBlock
46     FunctionVal,            // This is an instance of Function
47     GlobalVariableVal,      // This is an instance of GlobalVariable
48   };
49
50 private:
51   std::vector<User *> Uses;
52   std::string Name;
53   PATypeHandle Ty;
54   ValueTy VTy;
55
56   void operator=(const Value &);     // Do not implement
57   Value(const Value &);              // Do not implement
58 public:
59   Value(const Type *Ty, ValueTy vty, const std::string &name = "");
60   virtual ~Value();
61   
62   /// dump - Support for debugging, callable in GDB: V->dump()
63   //
64   void dump() const;
65
66   /// print - Implement operator<< on Value...
67   ///
68   virtual void print(std::ostream &O) const = 0;
69   
70   /// All values are typed, get the type of this value.
71   ///
72   inline const Type *getType() const { return Ty; }
73   
74   // All values can potentially be named...
75   inline bool               hasName() const { return Name != ""; }
76   inline const std::string &getName() const { return Name; }
77
78   virtual void setName(const std::string &name, SymbolTable * = 0) {
79     Name = name;
80   }
81   
82   /// getValueType - Return the immediate subclass of this Value.
83   ///
84   inline ValueTy getValueType() const { return VTy; }
85   
86   /// replaceAllUsesWith - Go through the uses list for this definition and make
87   /// each use point to "V" instead of "this".  After this completes, 'this's 
88   /// use list is guaranteed to be empty.
89   ///
90   void replaceAllUsesWith(Value *V);
91
92   // uncheckedReplaceAllUsesWith - Just like replaceAllUsesWith but dangerous.
93   // Only use when in type resolution situations!
94   void uncheckedReplaceAllUsesWith(Value *V);
95
96   /// refineAbstractType - This function is implemented because we use
97   /// potentially abstract types, and these types may be resolved to more
98   /// concrete types after we are constructed.
99   ///
100   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
101   
102   //----------------------------------------------------------------------
103   // Methods for handling the vector of uses of this Value.
104   //
105   typedef std::vector<User*>::iterator       use_iterator;
106   typedef std::vector<User*>::const_iterator use_const_iterator;
107
108   inline unsigned           use_size()  const { return Uses.size();  }
109   inline bool               use_empty() const { return Uses.empty(); }
110   inline use_iterator       use_begin()       { return Uses.begin(); }
111   inline use_const_iterator use_begin() const { return Uses.begin(); }
112   inline use_iterator       use_end()         { return Uses.end();   }
113   inline use_const_iterator use_end()   const { return Uses.end();   }
114   inline User              *use_back()        { return Uses.back();  }
115   inline const User        *use_back()  const { return Uses.back();  }
116
117   /// addUse/killUse - These two methods should only be used by the Use class
118   /// below.
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 //                                  Use Class
139 //===----------------------------------------------------------------------===//
140
141 // Use is here to make keeping the "use" list of a Value up-to-date really easy.
142 //
143 class Use {
144   Value *Val;
145   User *U;
146 public:
147   inline Use(Value *v, User *user) {
148     Val = v; U = user;
149     if (Val) Val->addUse(U);
150   }
151
152   inline Use(const Use &user) {
153     Val = 0;
154     U = user.U;
155     operator=(user.Val);
156   }
157   inline ~Use() { if (Val) Val->killUse(U); }
158   inline operator Value*() const { return Val; }
159
160   inline Value *operator=(Value *V) { 
161     if (Val) Val->killUse(U);
162     Val = V;
163     if (V) V->addUse(U);
164     return V;
165   }
166
167   inline       Value *operator->()       { return Val; }
168   inline const Value *operator->() const { return Val; }
169
170   inline       Value *get()       { return Val; }
171   inline const Value *get() const { return Val; }
172
173   inline const Use &operator=(const Use &user) {
174     if (Val) Val->killUse(U);
175     Val = user.Val;
176     Val->addUse(U);
177     return *this;
178   }
179 };
180
181 template<> struct simplify_type<Use> {
182   typedef Value* SimpleType;
183   
184   static SimpleType getSimplifiedValue(const Use &Val) {
185     return (SimpleType)Val.get();
186   }
187 };
188 template<> struct simplify_type<const Use> {
189   typedef Value* SimpleType;
190   
191   static SimpleType getSimplifiedValue(const Use &Val) {
192     return (SimpleType)Val.get();
193   }
194 };
195
196 // isa - Provide some specializations of isa so that we don't have to include
197 // the subtype header files to test to see if the value is a subclass...
198 //
199 template <> inline bool isa_impl<Type, Value>(const Value &Val) { 
200   return Val.getValueType() == Value::TypeVal;
201 }
202 template <> inline bool isa_impl<Constant, Value>(const Value &Val) { 
203   return Val.getValueType() == Value::ConstantVal; 
204 }
205 template <> inline bool isa_impl<Argument, Value>(const Value &Val) { 
206   return Val.getValueType() == Value::ArgumentVal;
207 }
208 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) { 
209   return Val.getValueType() == Value::InstructionVal;
210 }
211 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) { 
212   return Val.getValueType() == Value::BasicBlockVal;
213 }
214 template <> inline bool isa_impl<Function, Value>(const Value &Val) { 
215   return Val.getValueType() == Value::FunctionVal;
216 }
217 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) { 
218   return Val.getValueType() == Value::GlobalVariableVal;
219 }
220 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) { 
221   return isa<GlobalVariable>(Val) || isa<Function>(Val);
222 }
223
224 #endif