1 //===-- llvm/Value.h - Definition of the Value class ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the very important Value class. This is subclassed by a
11 // bunch of other important classes, like Instruction, Function, Type, etc...
13 // This file also defines the Use<> template for users of value.
15 //===----------------------------------------------------------------------===//
20 #include "llvm/AbstractTypeUser.h"
22 #include "llvm/Support/Casting.h"
36 //===----------------------------------------------------------------------===//
38 //===----------------------------------------------------------------------===//
40 /// Value - The base class of all values computed by a program that may be used
41 /// as operands to other values.
45 unsigned SubclassID; // Subclass identifier (for isa/dyn_cast)
50 void operator=(const Value &); // Do not implement
51 Value(const Value &); // Do not implement
54 Value(const Type *Ty, unsigned scid, const std::string &name = "");
57 /// dump - Support for debugging, callable in GDB: V->dump()
59 virtual void dump() const;
61 /// print - Implement operator<< on Value...
63 virtual void print(std::ostream &O) const = 0;
65 /// All values are typed, get the type of this value.
67 inline const Type *getType() const { return Ty; }
69 // All values can potentially be named...
70 inline bool hasName() const { return !Name.empty(); }
71 inline const std::string &getName() const { return Name; }
73 virtual void setName(const std::string &name, SymbolTable * = 0) {
77 /// replaceAllUsesWith - Go through the uses list for this definition and make
78 /// each use point to "V" instead of "this". After this completes, 'this's
79 /// use list is guaranteed to be empty.
81 void replaceAllUsesWith(Value *V);
83 // uncheckedReplaceAllUsesWith - Just like replaceAllUsesWith but dangerous.
84 // Only use when in type resolution situations!
85 void uncheckedReplaceAllUsesWith(Value *V);
87 //----------------------------------------------------------------------
88 // Methods for handling the vector of uses of this Value.
90 typedef UseListIteratorWrapper use_iterator;
91 typedef UseListConstIteratorWrapper use_const_iterator;
92 typedef iplist<Use>::size_type size_type;
94 size_type use_size() const { return Uses.size(); }
95 bool use_empty() const { return Uses.empty(); }
96 use_iterator use_begin() { return Uses.begin(); }
97 use_const_iterator use_begin() const { return Uses.begin(); }
98 use_iterator use_end() { return Uses.end(); }
99 use_const_iterator use_end() const { return Uses.end(); }
100 User *use_back() { return Uses.back().getUser(); }
101 const User *use_back() const { return Uses.back().getUser(); }
103 /// hasOneUse - Return true if there is exactly one user of this value. This
104 /// is specialized because it is a common request and does not require
105 /// traversing the whole use list.
107 bool hasOneUse() const {
108 iplist<Use>::const_iterator I = Uses.begin(), E = Uses.end();
109 if (I == E) return false;
113 /// addUse/killUse - These two methods should only be used by the Use class.
115 void addUse(Use &U) { Uses.push_back(&U); }
116 void killUse(Use &U) { Uses.remove(&U); }
118 /// getValueType - Return an ID for the concrete type of this object. This is
119 /// used to implement the classof checks. This should not be used for any
120 /// other purpose, as the values may change as LLVM evolves. Also, note that
121 /// starting with the InstructionVal value, the value stored is actually the
122 /// Instruction opcode, so there are more than just these values possible here
123 /// (and Instruction must be last).
126 ArgumentVal, // This is an instance of Argument
127 BasicBlockVal, // This is an instance of BasicBlock
128 FunctionVal, // This is an instance of Function
129 GlobalVariableVal, // This is an instance of GlobalVariable
130 UndefValueVal, // This is an instance of UndefValue
131 ConstantExprVal, // This is an instance of ConstantExpr
132 ConstantAggregateZeroVal, // This is an instance of ConstantAggregateNull
133 SimpleConstantVal, // This is some other type of Constant
134 InstructionVal, // This is an instance of Instruction
135 ValueListVal // This is for bcreader, a special ValTy
137 unsigned getValueType() const {
141 // Methods for support type inquiry through isa, cast, and dyn_cast:
142 static inline bool classof(const Value *V) {
143 return true; // Values are always values.
146 /// getRawType - This should only be used to implement the vmcore library.
148 const Type *getRawType() const { return Ty.getRawType(); }
151 /// FIXME: this is a gross hack, needed by another gross hack. Eliminate!
152 void setValueType(unsigned VT) { SubclassID = VT; }
153 friend class Instruction;
156 inline std::ostream &operator<<(std::ostream &OS, const Value &V) {
162 inline User *UseListIteratorWrapper::operator*() const {
163 return Super::operator*().getUser();
166 inline const User *UseListConstIteratorWrapper::operator*() const {
167 return Super::operator*().getUser();
171 Use::Use(Value *v, User *user) : Val(v), U(user) {
172 if (Val) Val->addUse(*this);
175 Use::Use(const Use &u) : Val(u.Val), U(u.U) {
176 if (Val) Val->addUse(*this);
180 if (Val) Val->killUse(*this);
183 void Use::set(Value *V) {
184 if (Val) Val->killUse(*this);
186 if (V) V->addUse(*this);
190 // isa - Provide some specializations of isa so that we don't have to include
191 // the subtype header files to test to see if the value is a subclass...
193 template <> inline bool isa_impl<Constant, Value>(const Value &Val) {
194 return Val.getValueType() == Value::SimpleConstantVal ||
195 Val.getValueType() == Value::FunctionVal ||
196 Val.getValueType() == Value::GlobalVariableVal ||
197 Val.getValueType() == Value::ConstantExprVal ||
198 Val.getValueType() == Value::ConstantAggregateZeroVal ||
199 Val.getValueType() == Value::UndefValueVal;
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);
220 } // End llvm namespace