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 short SubclassID; // Subclass identifier (for isa/dyn_cast)
46 /// SubclassData - This member is defined by this class, but is not used for
47 /// anything. Subclasses can use it to hold whatever state they find useful.
48 /// This field is initialized to zero by the ctor.
49 unsigned short SubclassData;
54 friend class SymbolTable; // Allow SymbolTable to directly poke Name.
57 void operator=(const Value &); // Do not implement
58 Value(const Value &); // Do not implement
61 Value(const Type *Ty, unsigned scid, const std::string &name = "");
64 /// dump - Support for debugging, callable in GDB: V->dump()
66 virtual void dump() const;
68 /// print - Implement operator<< on Value...
70 virtual void print(std::ostream &O) const = 0;
72 /// All values are typed, get the type of this value.
74 inline const Type *getType() const { return Ty; }
76 // All values can potentially be named...
77 inline bool hasName() const { return !Name.empty(); }
78 inline const std::string &getName() const { return Name; }
80 void setName(const std::string &name);
82 /// replaceAllUsesWith - Go through the uses list for this definition and make
83 /// each use point to "V" instead of "this". After this completes, 'this's
84 /// use list is guaranteed to be empty.
86 void replaceAllUsesWith(Value *V);
88 // uncheckedReplaceAllUsesWith - Just like replaceAllUsesWith but dangerous.
89 // Only use when in type resolution situations!
90 void uncheckedReplaceAllUsesWith(Value *V);
92 //----------------------------------------------------------------------
93 // Methods for handling the vector of uses of this Value.
95 typedef value_use_iterator<User> use_iterator;
96 typedef value_use_iterator<const User> use_const_iterator;
98 bool use_empty() const { return UseList == 0; }
99 use_iterator use_begin() { return use_iterator(UseList); }
100 use_const_iterator use_begin() const { return use_const_iterator(UseList); }
101 use_iterator use_end() { return use_iterator(0); }
102 use_const_iterator use_end() const { return use_const_iterator(0); }
103 User *use_back() { return *use_begin(); }
104 const User *use_back() const { return *use_begin(); }
106 /// hasOneUse - Return true if there is exactly one user of this value. This
107 /// is specialized because it is a common request and does not require
108 /// traversing the whole use list.
110 bool hasOneUse() const {
111 use_const_iterator I = use_begin(), E = use_end();
112 if (I == E) return false;
116 /// hasNUses - Return true if this Value has exactly N users.
118 bool hasNUses(unsigned N) const;
120 /// hasNUsesOrMore - Return true if this value has N users or more. This is
121 /// logically equivalent to getNumUses() >= N.
123 bool hasNUsesOrMore(unsigned N) const;
125 /// getNumUses - This method computes the number of uses of this Value. This
126 /// is a linear time operation. Use hasOneUse, hasNUses, or hasMoreThanNUses
127 /// to check for specific values.
128 unsigned getNumUses() const;
130 /// addUse/killUse - These two methods should only be used by the Use class.
132 void addUse(Use &U) { U.addToList(&UseList); }
134 /// getValueType - Return an ID for the concrete type of this object. This is
135 /// used to implement the classof checks. This should not be used for any
136 /// other purpose, as the values may change as LLVM evolves. Also, note that
137 /// starting with the InstructionVal value, the value stored is actually the
138 /// Instruction opcode, so there are more than just these values possible here
139 /// (and Instruction must be last).
142 ArgumentVal, // This is an instance of Argument
143 BasicBlockVal, // This is an instance of BasicBlock
144 FunctionVal, // This is an instance of Function
145 GlobalVariableVal, // This is an instance of GlobalVariable
146 UndefValueVal, // This is an instance of UndefValue
147 ConstantExprVal, // This is an instance of ConstantExpr
148 ConstantAggregateZeroVal, // This is an instance of ConstantAggregateNull
149 ConstantBoolVal, // This is an instance of ConstantBool
150 ConstantSIntVal, // This is an instance of ConstantSInt
151 ConstantUIntVal, // This is an instance of ConstantUInt
152 ConstantFPVal, // This is an instance of ConstantFP
153 ConstantArrayVal, // This is an instance of ConstantArray
154 ConstantStructVal, // This is an instance of ConstantStruct
155 ConstantPackedVal, // This is an instance of ConstantPacked
156 ConstantPointerNullVal, // This is an instance of ConstantPointerNull
157 InstructionVal, // This is an instance of Instruction
160 ConstantFirstVal = FunctionVal,
161 ConstantLastVal = ConstantPointerNullVal
163 unsigned getValueType() const {
167 // Methods for support type inquiry through isa, cast, and dyn_cast:
168 static inline bool classof(const Value * /*V*/) {
169 return true; // Values are always values.
172 /// getRawType - This should only be used to implement the vmcore library.
174 const Type *getRawType() const { return Ty.getRawType(); }
177 /// FIXME: this is a gross hack, needed by another gross hack. Eliminate!
178 void setValueType(unsigned short VT) { SubclassID = VT; }
179 friend class Instruction;
182 inline std::ostream &operator<<(std::ostream &OS, const Value &V) {
187 void Use::init(Value *v, User *user) {
190 if (Val) Val->addUse(*this);
194 if (Val) removeFromList();
197 void Use::set(Value *V) {
198 if (Val) removeFromList();
200 if (V) V->addUse(*this);
204 // isa - Provide some specializations of isa so that we don't have to include
205 // the subtype header files to test to see if the value is a subclass...
207 template <> inline bool isa_impl<Constant, Value>(const Value &Val) {
208 return Val.getValueType() >= Value::ConstantFirstVal &&
209 Val.getValueType() <= Value::ConstantLastVal;
211 template <> inline bool isa_impl<Argument, Value>(const Value &Val) {
212 return Val.getValueType() == Value::ArgumentVal;
214 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) {
215 return Val.getValueType() >= Value::InstructionVal;
217 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) {
218 return Val.getValueType() == Value::BasicBlockVal;
220 template <> inline bool isa_impl<Function, Value>(const Value &Val) {
221 return Val.getValueType() == Value::FunctionVal;
223 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) {
224 return Val.getValueType() == Value::GlobalVariableVal;
226 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) {
227 return isa<GlobalVariable>(Val) || isa<Function>(Val);
230 } // End llvm namespace