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.
44 unsigned SubclassID; // Subclass identifier (for isa/dyn_cast)
49 void operator=(const Value &); // Do not implement
50 Value(const Value &); // Do not implement
53 Value(const Type *Ty, unsigned scid, const std::string &name = "");
56 /// dump - Support for debugging, callable in GDB: V->dump()
58 virtual void dump() const;
60 /// print - Implement operator<< on Value...
62 virtual void print(std::ostream &O) const = 0;
64 /// All values are typed, get the type of this value.
66 inline const Type *getType() const { return Ty; }
68 // All values can potentially be named...
69 inline bool hasName() const { return !Name.empty(); }
70 inline const std::string &getName() const { return Name; }
72 virtual void setName(const std::string &name, SymbolTable * = 0) {
76 /// replaceAllUsesWith - Go through the uses list for this definition and make
77 /// each use point to "V" instead of "this". After this completes, 'this's
78 /// use list is guaranteed to be empty.
80 void replaceAllUsesWith(Value *V);
82 // uncheckedReplaceAllUsesWith - Just like replaceAllUsesWith but dangerous.
83 // Only use when in type resolution situations!
84 void uncheckedReplaceAllUsesWith(Value *V);
86 //----------------------------------------------------------------------
87 // Methods for handling the vector of uses of this Value.
89 typedef value_use_iterator<User> use_iterator;
90 typedef value_use_iterator<const User> use_const_iterator;
92 bool use_empty() const { return UseList == 0; }
93 use_iterator use_begin() { return use_iterator(UseList); }
94 use_const_iterator use_begin() const { return use_const_iterator(UseList); }
95 use_iterator use_end() { return use_iterator(0); }
96 use_const_iterator use_end() const { return use_const_iterator(0); }
97 User *use_back() { return *use_begin(); }
98 const User *use_back() const { return *use_begin(); }
100 /// hasOneUse - Return true if there is exactly one user of this value. This
101 /// is specialized because it is a common request and does not require
102 /// traversing the whole use list.
104 bool hasOneUse() const {
105 use_const_iterator I = use_begin(), E = use_end();
106 if (I == E) return false;
110 /// hasNUses - Return true if this Value has exactly N users.
112 bool hasNUses(unsigned N) const;
114 /// getNumUses - This method computes the number of uses of this Value. This
115 /// is a linear time operation. Use hasOneUse or hasNUses to check for
117 unsigned getNumUses() const;
119 /// addUse/killUse - These two methods should only be used by the Use class.
121 void addUse(Use &U) { U.addToList(&UseList); }
123 /// getValueType - Return an ID for the concrete type of this object. This is
124 /// used to implement the classof checks. This should not be used for any
125 /// other purpose, as the values may change as LLVM evolves. Also, note that
126 /// starting with the InstructionVal value, the value stored is actually the
127 /// Instruction opcode, so there are more than just these values possible here
128 /// (and Instruction must be last).
131 ArgumentVal, // This is an instance of Argument
132 BasicBlockVal, // This is an instance of BasicBlock
133 FunctionVal, // This is an instance of Function
134 GlobalVariableVal, // This is an instance of GlobalVariable
135 UndefValueVal, // This is an instance of UndefValue
136 ConstantExprVal, // This is an instance of ConstantExpr
137 ConstantAggregateZeroVal, // This is an instance of ConstantAggregateNull
138 SimpleConstantVal, // This is some other type of Constant
139 InstructionVal, // This is an instance of Instruction
140 ValueListVal // This is for bcreader, a special ValTy
142 unsigned getValueType() const {
146 // Methods for support type inquiry through isa, cast, and dyn_cast:
147 static inline bool classof(const Value *V) {
148 return true; // Values are always values.
151 /// getRawType - This should only be used to implement the vmcore library.
153 const Type *getRawType() const { return Ty.getRawType(); }
156 /// FIXME: this is a gross hack, needed by another gross hack. Eliminate!
157 void setValueType(unsigned VT) { SubclassID = VT; }
158 friend class Instruction;
161 inline std::ostream &operator<<(std::ostream &OS, const Value &V) {
166 void Use::init(Value *v, User *user) {
169 if (Val) Val->addUse(*this);
173 if (Val) removeFromList();
176 void Use::set(Value *V) {
177 if (Val) removeFromList();
179 if (V) V->addUse(*this);
183 // isa - Provide some specializations of isa so that we don't have to include
184 // the subtype header files to test to see if the value is a subclass...
186 template <> inline bool isa_impl<Constant, Value>(const Value &Val) {
187 return Val.getValueType() == Value::SimpleConstantVal ||
188 Val.getValueType() == Value::FunctionVal ||
189 Val.getValueType() == Value::GlobalVariableVal ||
190 Val.getValueType() == Value::ConstantExprVal ||
191 Val.getValueType() == Value::ConstantAggregateZeroVal ||
192 Val.getValueType() == Value::UndefValueVal;
194 template <> inline bool isa_impl<Argument, Value>(const Value &Val) {
195 return Val.getValueType() == Value::ArgumentVal;
197 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) {
198 return Val.getValueType() >= Value::InstructionVal;
200 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) {
201 return Val.getValueType() == Value::BasicBlockVal;
203 template <> inline bool isa_impl<Function, Value>(const Value &Val) {
204 return Val.getValueType() == Value::FunctionVal;
206 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) {
207 return Val.getValueType() == Value::GlobalVariableVal;
209 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) {
210 return isa<GlobalVariable>(Val) || isa<Function>(Val);
213 } // End llvm namespace