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 //===----------------------------------------------------------------------===//
13 #include "llvm/AbstractTypeUser.h"
14 #include "Support/Annotation.h"
15 #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 struct Value : public Annotable { // Values are annotable
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;
54 void operator=(const Value &); // Do not implement
55 Value(const Value &); // Do not implement
57 Value(const Type *Ty, ValueTy vty, const std::string &name = "");
60 /// dump - Support for debugging, callable in GDB: V->dump()
62 virtual void dump() const;
64 /// print - Implement operator<< on Value...
66 virtual void print(std::ostream &O) const = 0;
68 /// All values are typed, get the type of this value.
70 inline const Type *getType() const { return Ty; }
72 // All values can potentially be named...
73 inline bool hasName() const { return Name != ""; }
74 inline const std::string &getName() const { return Name; }
76 virtual void setName(const std::string &name, SymbolTable * = 0) {
80 /// getValueType - Return the immediate subclass of this Value.
82 inline ValueTy getValueType() const { return VTy; }
84 /// replaceAllUsesWith - Go through the uses list for this definition and make
85 /// each use point to "V" instead of "this". After this completes, 'this's
86 /// use list is guaranteed to be empty.
88 void replaceAllUsesWith(Value *V);
90 // uncheckedReplaceAllUsesWith - Just like replaceAllUsesWith but dangerous.
91 // Only use when in type resolution situations!
92 void uncheckedReplaceAllUsesWith(Value *V);
94 //----------------------------------------------------------------------
95 // Methods for handling the vector of uses of this Value.
97 typedef std::vector<User*>::iterator use_iterator;
98 typedef std::vector<User*>::const_iterator use_const_iterator;
100 unsigned use_size() const { return Uses.size(); }
101 bool use_empty() const { return Uses.empty(); }
102 use_iterator use_begin() { return Uses.begin(); }
103 use_const_iterator use_begin() const { return Uses.begin(); }
104 use_iterator use_end() { return Uses.end(); }
105 use_const_iterator use_end() const { return Uses.end(); }
106 User *use_back() { return Uses.back(); }
107 const User *use_back() const { return Uses.back(); }
109 /// hasOneUse - Return true if there is exactly one user of this value.
111 bool hasOneUse() const { return use_size() == 1; }
113 /// addUse/killUse - These two methods should only be used by the Use class
115 void addUse(User *I) { Uses.push_back(I); }
116 void killUse(User *I);
119 inline std::ostream &operator<<(std::ostream &OS, const Value *V) {
121 OS << "<null> value!\n";
127 inline std::ostream &operator<<(std::ostream &OS, const Value &V) {
133 //===----------------------------------------------------------------------===//
135 //===----------------------------------------------------------------------===//
137 // Use is here to make keeping the "use" list of a Value up-to-date really easy.
143 inline Use(Value *v, User *user) {
145 if (Val) Val->addUse(U);
148 inline Use(const Use &user) {
153 inline ~Use() { if (Val) Val->killUse(U); }
154 inline operator Value*() const { return Val; }
156 inline Value *operator=(Value *V) {
157 if (Val) Val->killUse(U);
163 inline Value *operator->() { return Val; }
164 inline const Value *operator->() const { return Val; }
166 inline Value *get() { return Val; }
167 inline const Value *get() const { return Val; }
169 inline const Use &operator=(const Use &user) {
170 if (Val) Val->killUse(U);
177 template<> struct simplify_type<Use> {
178 typedef Value* SimpleType;
180 static SimpleType getSimplifiedValue(const Use &Val) {
181 return (SimpleType)Val.get();
184 template<> struct simplify_type<const Use> {
185 typedef Value* SimpleType;
187 static SimpleType getSimplifiedValue(const Use &Val) {
188 return (SimpleType)Val.get();
192 // isa - Provide some specializations of isa so that we don't have to include
193 // the subtype header files to test to see if the value is a subclass...
195 template <> inline bool isa_impl<Type, Value>(const Value &Val) {
196 return Val.getValueType() == Value::TypeVal;
198 template <> inline bool isa_impl<Constant, Value>(const Value &Val) {
199 return Val.getValueType() == Value::ConstantVal;
201 template <> inline bool isa_impl<Argument, Value>(const Value &Val) {
202 return Val.getValueType() == Value::ArgumentVal;
204 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) {
205 return Val.getValueType() == Value::InstructionVal;
207 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) {
208 return Val.getValueType() == Value::BasicBlockVal;
210 template <> inline bool isa_impl<Function, Value>(const Value &Val) {
211 return Val.getValueType() == Value::FunctionVal;
213 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) {
214 return Val.getValueType() == Value::GlobalVariableVal;
216 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) {
217 return isa<GlobalVariable>(Val) || isa<Function>(Val);