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 class Value : public Annotable, // Values are annotable
35 public AbstractTypeUser { // Values use potentially abstract types
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
48 std::vector<User *> Uses;
50 PATypeHandle<Type> Ty;
53 void operator=(const Value &); // Do not implement
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";
131 inline std::ostream &operator<<(std::ostream &OS, const Value &V) {
137 //===----------------------------------------------------------------------===//
139 //===----------------------------------------------------------------------===//
141 // UseTy and it's friendly typedefs (Use) are here to make keeping the "use"
142 // list of a definition node up-to-date really easy.
144 template<class ValueSubclass>
149 inline UseTy<ValueSubclass>(ValueSubclass *v, User *user) {
151 if (Val) Val->addUse(U);
154 inline ~UseTy<ValueSubclass>() { if (Val) Val->killUse(U); }
156 inline operator ValueSubclass *() const { return Val; }
158 inline UseTy<ValueSubclass>(const UseTy<ValueSubclass> &user) {
163 inline ValueSubclass *operator=(ValueSubclass *V) {
164 if (Val) Val->killUse(U);
170 inline ValueSubclass *operator->() { return Val; }
171 inline const ValueSubclass *operator->() const { return Val; }
173 inline ValueSubclass *get() { return Val; }
174 inline const ValueSubclass *get() const { return Val; }
176 inline UseTy<ValueSubclass> &operator=(const UseTy<ValueSubclass> &user) {
177 if (Val) Val->killUse(U);
184 typedef UseTy<Value> Use; // Provide Use as a common UseTy type
186 template<typename From> struct simplify_type<UseTy<From> > {
187 typedef typename simplify_type<From*>::SimpleType SimpleType;
189 static SimpleType getSimplifiedValue(const UseTy<From> &Val) {
190 return (SimpleType)Val.get();
193 template<typename From> struct simplify_type<const UseTy<From> > {
194 typedef typename simplify_type<From*>::SimpleType SimpleType;
196 static SimpleType getSimplifiedValue(const UseTy<From> &Val) {
197 return (SimpleType)Val.get();
201 // isa - Provide some specializations of isa so that we don't have to include
202 // the subtype header files to test to see if the value is a subclass...
204 template <> inline bool isa_impl<Type, Value>(const Value &Val) {
205 return Val.getValueType() == Value::TypeVal;
207 template <> inline bool isa_impl<Constant, Value>(const Value &Val) {
208 return Val.getValueType() == Value::ConstantVal;
210 template <> inline bool isa_impl<Argument, Value>(const Value &Val) {
211 return Val.getValueType() == Value::ArgumentVal;
213 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) {
214 return Val.getValueType() == Value::InstructionVal;
216 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) {
217 return Val.getValueType() == Value::BasicBlockVal;
219 template <> inline bool isa_impl<Function, Value>(const Value &Val) {
220 return Val.getValueType() == Value::FunctionVal;
222 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) {
223 return Val.getValueType() == Value::GlobalVariableVal;
225 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) {
226 return isa<GlobalVariable>(Val) || isa<Function>(Val);