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"
30 //===----------------------------------------------------------------------===//
32 //===----------------------------------------------------------------------===//
34 /// Value - The base class of all values computed by a program that may be used
35 /// as operands to other values.
37 class Value : public Annotable, // Values are annotable
38 public AbstractTypeUser { // Values use potentially abstract types
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
51 std::vector<User *> Uses;
53 PATypeHandle<Type> Ty;
56 void operator=(const Value &); // Do not implement
57 Value(const Value &); // Do not implement
59 inline void setType(const Type *ty) { Ty = ty; }
61 Value(const Type *Ty, ValueTy vty, const std::string &name = "");
64 /// dump - Support for debugging, callable in GDB: V->dump()
68 /// print - Implement operator<< on Value...
70 virtual void print(std::ostream &O) const = 0;
72 /// All values are typed, get the type of this value.
74 inline const Type *getType() const { return Ty; }
76 // All values can potentially be named...
77 inline bool hasName() const { return Name != ""; }
78 inline const std::string &getName() const { return Name; }
80 virtual void setName(const std::string &name, SymbolTable * = 0) {
84 /// getValueType - Return the immediate subclass of this Value.
86 inline ValueTy getValueType() const { return VTy; }
88 /// replaceAllUsesWith - Go through the uses list for this definition and make
89 /// each use point to "V" instead of "this". After this completes, 'this's
90 /// use list is guaranteed to be empty.
92 void replaceAllUsesWith(Value *V);
94 /// refineAbstractType - This function is implemented because we use
95 /// potentially abstract types, and these types may be resolved to more
96 /// concrete types after we are constructed.
98 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
100 //----------------------------------------------------------------------
101 // Methods for handling the vector of uses of this Value.
103 typedef std::vector<User*>::iterator use_iterator;
104 typedef std::vector<User*>::const_iterator use_const_iterator;
106 inline unsigned use_size() const { return Uses.size(); }
107 inline bool use_empty() const { return Uses.empty(); }
108 inline use_iterator use_begin() { return Uses.begin(); }
109 inline use_const_iterator use_begin() const { return Uses.begin(); }
110 inline use_iterator use_end() { return Uses.end(); }
111 inline use_const_iterator use_end() const { return Uses.end(); }
112 inline User *use_back() { return Uses.back(); }
113 inline const User *use_back() const { return Uses.back(); }
115 inline void use_push_back(User *I) { Uses.push_back(I); }
116 User *use_remove(use_iterator &I);
118 inline void addUse(User *I) { Uses.push_back(I); }
119 void killUse(User *I);
122 inline std::ostream &operator<<(std::ostream &OS, const Value *V) {
124 OS << "<null> value!\n";
130 inline std::ostream &operator<<(std::ostream &OS, const Value &V) {
136 //===----------------------------------------------------------------------===//
138 //===----------------------------------------------------------------------===//
140 // UseTy and it's friendly typedefs (Use) are here to make keeping the "use"
141 // list of a definition node up-to-date really easy.
143 template<class ValueSubclass>
148 inline UseTy<ValueSubclass>(ValueSubclass *v, User *user) {
150 if (Val) Val->addUse(U);
153 inline ~UseTy<ValueSubclass>() { if (Val) Val->killUse(U); }
155 inline operator ValueSubclass *() const { return Val; }
157 inline UseTy<ValueSubclass>(const UseTy<ValueSubclass> &user) {
162 inline ValueSubclass *operator=(ValueSubclass *V) {
163 if (Val) Val->killUse(U);
169 inline ValueSubclass *operator->() { return Val; }
170 inline const ValueSubclass *operator->() const { return Val; }
172 inline ValueSubclass *get() { return Val; }
173 inline const ValueSubclass *get() const { return Val; }
175 inline UseTy<ValueSubclass> &operator=(const UseTy<ValueSubclass> &user) {
176 if (Val) Val->killUse(U);
183 typedef UseTy<Value> Use; // Provide Use as a common UseTy type
185 template<typename From> struct simplify_type<UseTy<From> > {
186 typedef typename simplify_type<From*>::SimpleType SimpleType;
188 static SimpleType getSimplifiedValue(const UseTy<From> &Val) {
189 return (SimpleType)Val.get();
192 template<typename From> struct simplify_type<const UseTy<From> > {
193 typedef typename simplify_type<From*>::SimpleType SimpleType;
195 static SimpleType getSimplifiedValue(const UseTy<From> &Val) {
196 return (SimpleType)Val.get();
200 // isa - Provide some specializations of isa so that we don't have to include
201 // the subtype header files to test to see if the value is a subclass...
203 template <> inline bool isa_impl<Type, Value>(const Value &Val) {
204 return Val.getValueType() == Value::TypeVal;
206 template <> inline bool isa_impl<Constant, Value>(const Value &Val) {
207 return Val.getValueType() == Value::ConstantVal;
209 template <> inline bool isa_impl<Argument, Value>(const Value &Val) {
210 return Val.getValueType() == Value::ArgumentVal;
212 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) {
213 return Val.getValueType() == Value::InstructionVal;
215 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) {
216 return Val.getValueType() == Value::BasicBlockVal;
218 template <> inline bool isa_impl<Function, Value>(const Value &Val) {
219 return Val.getValueType() == Value::FunctionVal;
221 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) {
222 return Val.getValueType() == Value::GlobalVariableVal;
224 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) {
225 return isa<GlobalVariable>(Val) || isa<Function>(Val);