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"
15 #include "Support/Annotation.h"
16 #include "Support/Casting.h"
29 //===----------------------------------------------------------------------===//
31 //===----------------------------------------------------------------------===//
33 /// Value - The base class of all values computed by a program that may be used
34 /// as operands to other values.
36 struct Value : public Annotable { // Values are annotable
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
53 void operator=(const Value &); // Do not implement
54 Value(const Value &); // Do not implement
56 Value(const Type *Ty, ValueTy vty, const std::string &name = "");
59 /// dump - Support for debugging, callable in GDB: V->dump()
61 virtual void dump() const;
63 /// print - Implement operator<< on Value...
65 virtual void print(std::ostream &O) const = 0;
67 /// All values are typed, get the type of this value.
69 inline const Type *getType() const { return Ty; }
71 // All values can potentially be named...
72 inline bool hasName() const { return Name != ""; }
73 inline const std::string &getName() const { return Name; }
75 virtual void setName(const std::string &name, SymbolTable * = 0) {
79 /// getValueType - Return the immediate subclass of this Value.
81 inline ValueTy getValueType() const { return VTy; }
83 /// replaceAllUsesWith - Go through the uses list for this definition and make
84 /// each use point to "V" instead of "this". After this completes, 'this's
85 /// use list is guaranteed to be empty.
87 void replaceAllUsesWith(Value *V);
89 // uncheckedReplaceAllUsesWith - Just like replaceAllUsesWith but dangerous.
90 // Only use when in type resolution situations!
91 void uncheckedReplaceAllUsesWith(Value *V);
93 //----------------------------------------------------------------------
94 // Methods for handling the vector of uses of this Value.
96 typedef UseListIteratorWrapper use_iterator;
97 typedef UseListConstIteratorWrapper use_const_iterator;
99 unsigned use_size() const { return Uses.size(); }
100 bool use_empty() const { return Uses.empty(); }
101 use_iterator use_begin() { return Uses.begin(); }
102 use_const_iterator use_begin() const { return Uses.begin(); }
103 use_iterator use_end() { return Uses.end(); }
104 use_const_iterator use_end() const { return Uses.end(); }
105 User *use_back() { return Uses.back().getUser(); }
106 const User *use_back() const { return Uses.back().getUser(); }
108 /// hasOneUse - Return true if there is exactly one user of this value. This
109 /// is specialized because it is a common request and does not require
110 /// traversing the whole use list.
112 bool hasOneUse() const {
113 iplist<Use>::const_iterator I = Uses.begin(), E = Uses.end();
114 if (I == E) return false;
118 /// addUse/killUse - These two methods should only be used by the Use class.
120 void addUse(Use &U) { Uses.push_back(&U); }
121 void killUse(Use &U) { Uses.remove(&U); }
124 inline std::ostream &operator<<(std::ostream &OS, const Value *V) {
126 OS << "<null> value!\n";
132 inline std::ostream &operator<<(std::ostream &OS, const Value &V) {
138 inline User *UseListIteratorWrapper::operator*() const {
139 return Super::operator*().getUser();
142 inline const User *UseListConstIteratorWrapper::operator*() const {
143 return Super::operator*().getUser();
147 Use::Use(Value *v, User *user) : Val(v), U(user) {
148 if (Val) Val->addUse(*this);
151 Use::Use(const Use &u) : Val(u.Val), U(u.U) {
152 if (Val) Val->addUse(*this);
156 if (Val) Val->killUse(*this);
159 void Use::set(Value *V) {
160 if (Val) Val->killUse(*this);
162 if (V) V->addUse(*this);
166 // isa - Provide some specializations of isa so that we don't have to include
167 // the subtype header files to test to see if the value is a subclass...
169 template <> inline bool isa_impl<Type, Value>(const Value &Val) {
170 return Val.getValueType() == Value::TypeVal;
172 template <> inline bool isa_impl<Constant, Value>(const Value &Val) {
173 return Val.getValueType() == Value::ConstantVal;
175 template <> inline bool isa_impl<Argument, Value>(const Value &Val) {
176 return Val.getValueType() == Value::ArgumentVal;
178 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) {
179 return Val.getValueType() == Value::InstructionVal;
181 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) {
182 return Val.getValueType() == Value::BasicBlockVal;
184 template <> inline bool isa_impl<Function, Value>(const Value &Val) {
185 return Val.getValueType() == Value::FunctionVal;
187 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) {
188 return Val.getValueType() == Value::GlobalVariableVal;
190 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) {
191 return isa<GlobalVariable>(Val) || isa<Function>(Val);