Change the Opcode enum for PHI nodes from "Instruction::PHINode" to "Instruction...
[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 "llvm/Use.h"
15 #include "Support/Annotation.h"
16 #include "Support/Casting.h"
17 #include <iostream>
18
19 class Type;
20 class Constant;
21 class Argument;
22 class Instruction;
23 class BasicBlock;
24 class GlobalValue;
25 class Function;
26 class GlobalVariable;
27 class SymbolTable;
28
29 //===----------------------------------------------------------------------===//
30 //                                 Value Class
31 //===----------------------------------------------------------------------===//
32
33 /// Value - The base class of all values computed by a program that may be used
34 /// as operands to other values.
35 ///
36 struct Value : public Annotable {         // Values are annotable
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   iplist<Use> Uses;
49   std::string Name;
50   PATypeHolder Ty;
51   ValueTy VTy;
52
53   void operator=(const Value &);     // Do not implement
54   Value(const Value &);              // Do not implement
55 public:
56   Value(const Type *Ty, ValueTy vty, const std::string &name = "");
57   virtual ~Value();
58   
59   /// dump - Support for debugging, callable in GDB: V->dump()
60   //
61   virtual void dump() const;
62
63   /// print - Implement operator<< on Value...
64   ///
65   virtual void print(std::ostream &O) const = 0;
66   
67   /// All values are typed, get the type of this value.
68   ///
69   inline const Type *getType() const { return Ty; }
70   
71   // All values can potentially be named...
72   inline bool               hasName() const { return Name != ""; }
73   inline const std::string &getName() const { return Name; }
74
75   virtual void setName(const std::string &name, SymbolTable * = 0) {
76     Name = name;
77   }
78   
79   /// getValueType - Return the immediate subclass of this Value.
80   ///
81   inline ValueTy getValueType() const { return VTy; }
82   
83   /// replaceAllUsesWith - Go through the uses list for this definition and make
84   /// each use point to "V" instead of "this".  After this completes, 'this's 
85   /// use list is guaranteed to be empty.
86   ///
87   void replaceAllUsesWith(Value *V);
88
89   // uncheckedReplaceAllUsesWith - Just like replaceAllUsesWith but dangerous.
90   // Only use when in type resolution situations!
91   void uncheckedReplaceAllUsesWith(Value *V);
92
93   //----------------------------------------------------------------------
94   // Methods for handling the vector of uses of this Value.
95   //
96   typedef UseListIteratorWrapper      use_iterator;
97   typedef UseListConstIteratorWrapper use_const_iterator;
98
99   unsigned           use_size()  const { return Uses.size();  }
100   bool               use_empty() const { return Uses.empty(); }
101   use_iterator       use_begin()       { return Uses.begin(); }
102   use_const_iterator use_begin() const { return Uses.begin(); }
103   use_iterator       use_end()         { return Uses.end();   }
104   use_const_iterator use_end()   const { return Uses.end();   }
105   User             *use_back()         { return Uses.back().getUser(); }
106   const User       *use_back()  const  { return Uses.back().getUser(); }
107
108   /// hasOneUse - Return true if there is exactly one user of this value.  This
109   /// is specialized because it is a common request and does not require
110   /// traversing the whole use list.
111   ///
112   bool hasOneUse() const {
113     iplist<Use>::const_iterator I = Uses.begin(), E = Uses.end();
114     if (I == E) return false;
115     return ++I == E;
116   }
117
118   /// addUse/killUse - These two methods should only be used by the Use class.
119   ///
120   void addUse(Use &U)  { Uses.push_back(&U); }
121   void killUse(Use &U) { Uses.remove(&U); }
122 };
123
124 inline std::ostream &operator<<(std::ostream &OS, const Value *V) {
125   if (V == 0)
126     OS << "<null> value!\n";
127   else
128     V->print(OS);
129   return OS;
130 }
131
132 inline std::ostream &operator<<(std::ostream &OS, const Value &V) {
133   V.print(OS);
134   return OS;
135 }
136
137
138 inline User *UseListIteratorWrapper::operator*() const {
139   return Super::operator*().getUser();
140 }
141
142 inline const User *UseListConstIteratorWrapper::operator*() const {
143   return Super::operator*().getUser();
144 }
145
146
147 Use::Use(Value *v, User *user) : Val(v), U(user) {
148   if (Val) Val->addUse(*this);
149 }
150
151 Use::Use(const Use &u) : Val(u.Val), U(u.U) {
152   if (Val) Val->addUse(*this);
153 }
154
155 Use::~Use() {
156   if (Val) Val->killUse(*this);
157 }
158
159 void Use::set(Value *V) { 
160   if (Val) Val->killUse(*this);
161   Val = V;
162   if (V) V->addUse(*this);
163 }
164
165
166 // isa - Provide some specializations of isa so that we don't have to include
167 // the subtype header files to test to see if the value is a subclass...
168 //
169 template <> inline bool isa_impl<Type, Value>(const Value &Val) { 
170   return Val.getValueType() == Value::TypeVal;
171 }
172 template <> inline bool isa_impl<Constant, Value>(const Value &Val) { 
173   return Val.getValueType() == Value::ConstantVal; 
174 }
175 template <> inline bool isa_impl<Argument, Value>(const Value &Val) { 
176   return Val.getValueType() == Value::ArgumentVal;
177 }
178 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) { 
179   return Val.getValueType() == Value::InstructionVal;
180 }
181 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) { 
182   return Val.getValueType() == Value::BasicBlockVal;
183 }
184 template <> inline bool isa_impl<Function, Value>(const Value &Val) { 
185   return Val.getValueType() == Value::FunctionVal;
186 }
187 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) { 
188   return Val.getValueType() == Value::GlobalVariableVal;
189 }
190 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) { 
191   return isa<GlobalVariable>(Val) || isa<Function>(Val);
192 }
193
194 #endif