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 Value(const Type *Ty, ValueTy vty, const std::string &name = "");
62 /// dump - Support for debugging, callable in GDB: V->dump()
66 /// print - Implement operator<< on Value...
68 virtual void print(std::ostream &O) const = 0;
70 /// All values are typed, get the type of this value.
72 inline const Type *getType() const { return Ty; }
74 // All values can potentially be named...
75 inline bool hasName() const { return Name != ""; }
76 inline const std::string &getName() const { return Name; }
78 virtual void setName(const std::string &name, SymbolTable * = 0) {
82 /// getValueType - Return the immediate subclass of this Value.
84 inline ValueTy getValueType() const { return VTy; }
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.
90 void replaceAllUsesWith(Value *V);
92 /// refineAbstractType - This function is implemented because we use
93 /// potentially abstract types, and these types may be resolved to more
94 /// concrete types after we are constructed.
96 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
98 //----------------------------------------------------------------------
99 // Methods for handling the vector of uses of this Value.
101 typedef std::vector<User*>::iterator use_iterator;
102 typedef std::vector<User*>::const_iterator use_const_iterator;
104 inline unsigned use_size() const { return Uses.size(); }
105 inline bool use_empty() const { return Uses.empty(); }
106 inline use_iterator use_begin() { return Uses.begin(); }
107 inline use_const_iterator use_begin() const { return Uses.begin(); }
108 inline use_iterator use_end() { return Uses.end(); }
109 inline use_const_iterator use_end() const { return Uses.end(); }
110 inline User *use_back() { return Uses.back(); }
111 inline const User *use_back() const { return Uses.back(); }
113 inline void use_push_back(User *I) { Uses.push_back(I); }
114 User *use_remove(use_iterator &I);
116 inline void addUse(User *I) { Uses.push_back(I); }
117 void killUse(User *I);
120 inline std::ostream &operator<<(std::ostream &OS, const Value *V) {
122 OS << "<null> value!\n";
128 inline std::ostream &operator<<(std::ostream &OS, const Value &V) {
134 //===----------------------------------------------------------------------===//
136 //===----------------------------------------------------------------------===//
138 // UseTy and it's friendly typedefs (Use) are here to make keeping the "use"
139 // list of a definition node up-to-date really easy.
141 template<class ValueSubclass>
146 inline UseTy<ValueSubclass>(ValueSubclass *v, User *user) {
148 if (Val) Val->addUse(U);
151 inline ~UseTy<ValueSubclass>() { if (Val) Val->killUse(U); }
153 inline operator ValueSubclass *() const { return Val; }
155 inline UseTy<ValueSubclass>(const UseTy<ValueSubclass> &user) {
160 inline ValueSubclass *operator=(ValueSubclass *V) {
161 if (Val) Val->killUse(U);
167 inline ValueSubclass *operator->() { return Val; }
168 inline const ValueSubclass *operator->() const { return Val; }
170 inline ValueSubclass *get() { return Val; }
171 inline const ValueSubclass *get() const { return Val; }
173 inline UseTy<ValueSubclass> &operator=(const UseTy<ValueSubclass> &user) {
174 if (Val) Val->killUse(U);
181 typedef UseTy<Value> Use; // Provide Use as a common UseTy type
183 template<typename From> struct simplify_type<UseTy<From> > {
184 typedef typename simplify_type<From*>::SimpleType SimpleType;
186 static SimpleType getSimplifiedValue(const UseTy<From> &Val) {
187 return (SimpleType)Val.get();
190 template<typename From> struct simplify_type<const UseTy<From> > {
191 typedef typename simplify_type<From*>::SimpleType SimpleType;
193 static SimpleType getSimplifiedValue(const UseTy<From> &Val) {
194 return (SimpleType)Val.get();
198 // isa - Provide some specializations of isa so that we don't have to include
199 // the subtype header files to test to see if the value is a subclass...
201 template <> inline bool isa_impl<Type, Value>(const Value &Val) {
202 return Val.getValueType() == Value::TypeVal;
204 template <> inline bool isa_impl<Constant, Value>(const Value &Val) {
205 return Val.getValueType() == Value::ConstantVal;
207 template <> inline bool isa_impl<Argument, Value>(const Value &Val) {
208 return Val.getValueType() == Value::ArgumentVal;
210 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) {
211 return Val.getValueType() == Value::InstructionVal;
213 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) {
214 return Val.getValueType() == Value::BasicBlockVal;
216 template <> inline bool isa_impl<Function, Value>(const Value &Val) {
217 return Val.getValueType() == Value::FunctionVal;
219 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) {
220 return Val.getValueType() == Value::GlobalVariableVal;
222 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) {
223 return isa<GlobalVariable>(Val) || isa<Function>(Val);