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 declares the Value class.
11 // This file also defines the Use<> template for users of value.
13 //===----------------------------------------------------------------------===//
18 #include "llvm/AbstractTypeUser.h"
20 #include "llvm/Support/Casting.h"
21 #include "llvm/Support/Streams.h"
34 class ValueSymbolTable;
35 class TypeSymbolTable;
36 template<typename ValueTy> class StringMapEntry;
37 typedef StringMapEntry<Value*> ValueName;
39 //===----------------------------------------------------------------------===//
41 //===----------------------------------------------------------------------===//
43 /// This is a very important LLVM class. It is the base class of all values
44 /// computed by a program that may be used as operands to other values. Value is
45 /// the super class of other important classes such as Instruction and Function.
46 /// All Values have a Type. Type is not a subclass of Value. All types can have
47 /// a name and they should belong to some Module. Setting the name on the Value
48 /// automatically update's the module's symbol table.
50 /// Every value has a "use list" that keeps track of which other Values are
52 /// @brief LLVM Value Representation
54 const unsigned short SubclassID; // Subclass identifier (for isa/dyn_cast)
56 /// SubclassData - This member is defined by this class, but is not used for
57 /// anything. Subclasses can use it to hold whatever state they find useful.
58 /// This field is initialized to zero by the ctor.
59 unsigned short SubclassData;
64 friend class ValueSymbolTable; // Allow ValueSymbolTable to directly mod Name.
65 friend class SymbolTable; // Allow SymbolTable to directly poke Name.
68 void operator=(const Value &); // Do not implement
69 Value(const Value &); // Do not implement
72 Value(const Type *Ty, unsigned scid);
75 /// dump - Support for debugging, callable in GDB: V->dump()
77 virtual void dump() const;
79 /// print - Implement operator<< on Value...
81 virtual void print(std::ostream &O) const = 0;
82 void print(std::ostream *O) const { if (O) print(*O); }
84 /// All values are typed, get the type of this value.
86 inline const Type *getType() const { return Ty; }
88 // All values can potentially be named...
89 inline bool hasName() const { return Name != 0; }
90 std::string getName() const;
91 ValueName *getValueName() const { return Name; }
93 void setName(const std::string &name);
94 void setName(const char *Name, unsigned NameLen);
96 /// takeName - transfer the name from V to this value, setting V's name to
97 /// empty. It is an error to call V->takeName(V).
98 void takeName(Value *V);
100 /// replaceAllUsesWith - Go through the uses list for this definition and make
101 /// each use point to "V" instead of "this". After this completes, 'this's
102 /// use list is guaranteed to be empty.
104 void replaceAllUsesWith(Value *V);
106 // uncheckedReplaceAllUsesWith - Just like replaceAllUsesWith but dangerous.
107 // Only use when in type resolution situations!
108 void uncheckedReplaceAllUsesWith(Value *V);
110 //----------------------------------------------------------------------
111 // Methods for handling the vector of uses of this Value.
113 typedef value_use_iterator<User> use_iterator;
114 typedef value_use_iterator<const User> use_const_iterator;
116 bool use_empty() const { return UseList == 0; }
117 use_iterator use_begin() { return use_iterator(UseList); }
118 use_const_iterator use_begin() const { return use_const_iterator(UseList); }
119 use_iterator use_end() { return use_iterator(0); }
120 use_const_iterator use_end() const { return use_const_iterator(0); }
121 User *use_back() { return *use_begin(); }
122 const User *use_back() const { return *use_begin(); }
124 /// hasOneUse - Return true if there is exactly one user of this value. This
125 /// is specialized because it is a common request and does not require
126 /// traversing the whole use list.
128 bool hasOneUse() const {
129 use_const_iterator I = use_begin(), E = use_end();
130 if (I == E) return false;
134 /// hasNUses - Return true if this Value has exactly N users.
136 bool hasNUses(unsigned N) const;
138 /// hasNUsesOrMore - Return true if this value has N users or more. This is
139 /// logically equivalent to getNumUses() >= N.
141 bool hasNUsesOrMore(unsigned N) const;
143 /// getNumUses - This method computes the number of uses of this Value. This
144 /// is a linear time operation. Use hasOneUse, hasNUses, or hasMoreThanNUses
145 /// to check for specific values.
146 unsigned getNumUses() const;
148 /// addUse/killUse - These two methods should only be used by the Use class.
150 void addUse(Use &U) { U.addToList(&UseList); }
152 /// An enumeration for keeping track of the concrete subclass of Value that
153 /// is actually instantiated. Values of this enumeration are kept in the
154 /// Value classes SubclassID field. They are used for concrete type
157 ArgumentVal, // This is an instance of Argument
158 BasicBlockVal, // This is an instance of BasicBlock
159 FunctionVal, // This is an instance of Function
160 GlobalVariableVal, // This is an instance of GlobalVariable
161 UndefValueVal, // This is an instance of UndefValue
162 ConstantExprVal, // This is an instance of ConstantExpr
163 ConstantAggregateZeroVal, // This is an instance of ConstantAggregateNull
164 ConstantIntVal, // This is an instance of ConstantInt
165 ConstantFPVal, // This is an instance of ConstantFP
166 ConstantArrayVal, // This is an instance of ConstantArray
167 ConstantStructVal, // This is an instance of ConstantStruct
168 ConstantPackedVal, // This is an instance of ConstantPacked
169 ConstantPointerNullVal, // This is an instance of ConstantPointerNull
170 InlineAsmVal, // This is an instance of InlineAsm
171 InstructionVal, // This is an instance of Instruction
174 ConstantFirstVal = FunctionVal,
175 ConstantLastVal = ConstantPointerNullVal
178 /// getValueType - Return an ID for the concrete type of this object. This is
179 /// used to implement the classof checks. This should not be used for any
180 /// other purpose, as the values may change as LLVM evolves. Also, note that
181 /// starting with the InstructionVal value, the value stored is actually the
182 /// Instruction opcode, so there are more than just these values possible here
183 /// (and Instruction must be last).
185 unsigned getValueType() const {
189 // Methods for support type inquiry through isa, cast, and dyn_cast:
190 static inline bool classof(const Value *) {
191 return true; // Values are always values.
194 /// getRawType - This should only be used to implement the vmcore library.
196 const Type *getRawType() const { return Ty.getRawType(); }
199 inline std::ostream &operator<<(std::ostream &OS, const Value &V) {
204 void Use::init(Value *v, User *user) {
207 if (Val) Val->addUse(*this);
211 if (Val) removeFromList();
214 void Use::set(Value *V) {
215 if (Val) removeFromList();
217 if (V) V->addUse(*this);
221 // isa - Provide some specializations of isa so that we don't have to include
222 // the subtype header files to test to see if the value is a subclass...
224 template <> inline bool isa_impl<Constant, Value>(const Value &Val) {
225 return Val.getValueType() >= Value::ConstantFirstVal &&
226 Val.getValueType() <= Value::ConstantLastVal;
228 template <> inline bool isa_impl<Argument, Value>(const Value &Val) {
229 return Val.getValueType() == Value::ArgumentVal;
231 template <> inline bool isa_impl<InlineAsm, Value>(const Value &Val) {
232 return Val.getValueType() == Value::InlineAsmVal;
234 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) {
235 return Val.getValueType() >= Value::InstructionVal;
237 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) {
238 return Val.getValueType() == Value::BasicBlockVal;
240 template <> inline bool isa_impl<Function, Value>(const Value &Val) {
241 return Val.getValueType() == Value::FunctionVal;
243 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) {
244 return Val.getValueType() == Value::GlobalVariableVal;
246 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) {
247 return isa<GlobalVariable>(Val) || isa<Function>(Val);
250 } // End llvm namespace