1 //===-- llvm/Value.h - Definition of the Value class -------------*- C++ -*--=//
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...
6 // This file also defines the Use<> template for users of value.
8 //===----------------------------------------------------------------------===//
14 #include "llvm/Annotation.h"
15 #include "llvm/AbstractTypeUser.h"
16 #include "Support/Casting.h"
28 template<class ValueSubclass, class ItemParentType, class SymTabType>
31 //===----------------------------------------------------------------------===//
33 //===----------------------------------------------------------------------===//
35 class Value : public Annotable, // Values are annotable
36 public AbstractTypeUser { // Values use potentially abstract types
39 TypeVal, // This is an instance of Type
40 ConstantVal, // This is an instance of Constant
41 ArgumentVal, // This is an instance of Argument
42 InstructionVal, // This is an instance of Instruction
43 BasicBlockVal, // This is an instance of BasicBlock
44 FunctionVal, // This is an instance of Function
45 GlobalVariableVal, // This is an instance of GlobalVariable
49 std::vector<User *> Uses;
51 PATypeHandle<Type> Ty;
54 Value(const Value &); // Do not implement
56 inline void setType(const Type *ty) { Ty = ty; }
58 Value(const Type *Ty, ValueTy vty, const std::string &name = "");
61 // Support for debugging
64 // Implement operator<< on Value...
65 virtual void print(std::ostream &O) const = 0;
67 // All values can potentially be typed
68 inline const Type *getType() const { return Ty; }
70 // All values can potentially be named...
71 inline bool hasName() const { return Name != ""; }
72 inline const std::string &getName() const { return Name; }
74 virtual void setName(const std::string &name, SymbolTable * = 0) {
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.
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.
87 inline ValueTy getValueType() const { return VTy; }
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.
93 void replaceAllUsesWith(Value *D);
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.
99 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
101 //----------------------------------------------------------------------
102 // Methods for handling the vector of uses of this Value.
104 typedef std::vector<User*>::iterator use_iterator;
105 typedef std::vector<User*>::const_iterator use_const_iterator;
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(); }
116 inline void use_push_back(User *I) { Uses.push_back(I); }
117 User *use_remove(use_iterator &I);
119 inline void addUse(User *I) { Uses.push_back(I); }
120 void killUse(User *I);
123 inline std::ostream &operator<<(std::ostream &OS, const Value *V) {
125 OS << "<null> value!\n";
132 //===----------------------------------------------------------------------===//
134 //===----------------------------------------------------------------------===//
136 // UseTy and it's friendly typedefs (Use) are here to make keeping the "use"
137 // list of a definition node up-to-date really easy.
139 template<class ValueSubclass>
144 inline UseTy<ValueSubclass>(ValueSubclass *v, User *user) {
146 if (Val) Val->addUse(U);
149 inline ~UseTy<ValueSubclass>() { if (Val) Val->killUse(U); }
151 inline operator ValueSubclass *() const { return Val; }
153 inline UseTy<ValueSubclass>(const UseTy<ValueSubclass> &user) {
158 inline ValueSubclass *operator=(ValueSubclass *V) {
159 if (Val) Val->killUse(U);
165 inline ValueSubclass *operator->() { return Val; }
166 inline const ValueSubclass *operator->() const { return Val; }
168 inline ValueSubclass *get() { return Val; }
169 inline const ValueSubclass *get() const { return Val; }
171 inline UseTy<ValueSubclass> &operator=(const UseTy<ValueSubclass> &user) {
172 if (Val) Val->killUse(U);
179 typedef UseTy<Value> Use; // Provide Use as a common UseTy type
181 // Provide a specialization of real_type to work with use's... to make them a
182 // bit more transparent.
184 template <class X> class real_type <class UseTy<X> > { typedef X *Type; };
187 // isa - Provide some specializations of isa so that we don't have to include
188 // the subtype header files to test to see if the value is a subclass...
190 template <> inline bool isa<Type, const Value*>(const Value *Val) {
191 return Val->getValueType() == Value::TypeVal;
193 template <> inline bool isa<Type, Value*>(Value *Val) {
194 return Val->getValueType() == Value::TypeVal;
196 template <> inline bool isa<Constant, const Value*>(const Value *Val) {
197 return Val->getValueType() == Value::ConstantVal;
199 template <> inline bool isa<Constant, Value*>(Value *Val) {
200 return Val->getValueType() == Value::ConstantVal;
202 template <> inline bool isa<Argument, const Value*>(const Value *Val) {
203 return Val->getValueType() == Value::ArgumentVal;
205 template <> inline bool isa<Argument, Value*>(Value *Val) {
206 return Val->getValueType() == Value::ArgumentVal;
208 template <> inline bool isa<Instruction, const Value*>(const Value *Val) {
209 return Val->getValueType() == Value::InstructionVal;
211 template <> inline bool isa<Instruction, Value*>(Value *Val) {
212 return Val->getValueType() == Value::InstructionVal;
214 template <> inline bool isa<BasicBlock, const Value*>(const Value *Val) {
215 return Val->getValueType() == Value::BasicBlockVal;
217 template <> inline bool isa<BasicBlock, Value*>(Value *Val) {
218 return Val->getValueType() == Value::BasicBlockVal;
220 template <> inline bool isa<Function, const Value*>(const Value *Val) {
221 return Val->getValueType() == Value::FunctionVal;
223 template <> inline bool isa<Function, Value*>(Value *Val) {
224 return Val->getValueType() == Value::FunctionVal;
226 template <> inline bool isa<GlobalVariable, const Value*>(const Value *Val) {
227 return Val->getValueType() == Value::GlobalVariableVal;
229 template <> inline bool isa<GlobalVariable, Value*>(Value *Val) {
230 return Val->getValueType() == Value::GlobalVariableVal;
232 template <> inline bool isa<GlobalValue, const Value*>(const Value *Val) {
233 return isa<GlobalVariable>(Val) || isa<Function>(Val);
235 template <> inline bool isa<GlobalValue, Value*>(Value *Val) {
236 return isa<GlobalVariable>(Val) || isa<Function>(Val);