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"
35 class ValueSymbolTable;
36 class TypeSymbolTable;
37 template<typename ValueTy> class StringMapEntry;
38 typedef StringMapEntry<Value*> ValueName;
40 //===----------------------------------------------------------------------===//
42 //===----------------------------------------------------------------------===//
44 /// This is a very important LLVM class. It is the base class of all values
45 /// computed by a program that may be used as operands to other values. Value is
46 /// the super class of other important classes such as Instruction and Function.
47 /// All Values have a Type. Type is not a subclass of Value. All types can have
48 /// a name and they should belong to some Module. Setting the name on the Value
49 /// automatically update's the module's symbol table.
51 /// Every value has a "use list" that keeps track of which other Values are
53 /// @brief LLVM Value Representation
55 const unsigned short SubclassID; // Subclass identifier (for isa/dyn_cast)
57 /// SubclassData - This member is defined by this class, but is not used for
58 /// anything. Subclasses can use it to hold whatever state they find useful.
59 /// This field is initialized to zero by the ctor.
60 unsigned short SubclassData;
65 friend class ValueSymbolTable; // Allow ValueSymbolTable to directly mod Name.
66 friend class SymbolTable; // Allow SymbolTable to directly poke Name.
69 void operator=(const Value &); // Do not implement
70 Value(const Value &); // Do not implement
73 Value(const Type *Ty, unsigned scid);
76 /// dump - Support for debugging, callable in GDB: V->dump()
78 virtual void dump() const;
80 /// print - Implement operator<< on Value...
82 virtual void print(std::ostream &O) const = 0;
83 void print(std::ostream *O) const { if (O) print(*O); }
85 /// All values are typed, get the type of this value.
87 inline const Type *getType() const { return Ty; }
89 // All values can potentially be named...
90 inline bool hasName() const { return Name != 0; }
91 std::string getName() const { return getNameStr(); }
92 std::string getNameStr() const;
93 ValueName *getValueName() const { return Name; }
95 void setName(const std::string &name);
96 void setName(const char *Name, unsigned NameLen);
97 void setName(const char *Name); // Takes a null-terminated string.
100 /// takeName - transfer the name from V to this value, setting V's name to
101 /// empty. It is an error to call V->takeName(V).
102 void takeName(Value *V);
104 /// replaceAllUsesWith - Go through the uses list for this definition and make
105 /// each use point to "V" instead of "this". After this completes, 'this's
106 /// use list is guaranteed to be empty.
108 void replaceAllUsesWith(Value *V);
110 // uncheckedReplaceAllUsesWith - Just like replaceAllUsesWith but dangerous.
111 // Only use when in type resolution situations!
112 void uncheckedReplaceAllUsesWith(Value *V);
114 //----------------------------------------------------------------------
115 // Methods for handling the vector of uses of this Value.
117 typedef value_use_iterator<User> use_iterator;
118 typedef value_use_iterator<const User> use_const_iterator;
120 bool use_empty() const { return UseList == 0; }
121 use_iterator use_begin() { return use_iterator(UseList); }
122 use_const_iterator use_begin() const { return use_const_iterator(UseList); }
123 use_iterator use_end() { return use_iterator(0); }
124 use_const_iterator use_end() const { return use_const_iterator(0); }
125 User *use_back() { return *use_begin(); }
126 const User *use_back() const { return *use_begin(); }
128 /// hasOneUse - Return true if there is exactly one user of this value. This
129 /// is specialized because it is a common request and does not require
130 /// traversing the whole use list.
132 bool hasOneUse() const {
133 use_const_iterator I = use_begin(), E = use_end();
134 if (I == E) return false;
138 /// hasNUses - Return true if this Value has exactly N users.
140 bool hasNUses(unsigned N) const;
142 /// hasNUsesOrMore - Return true if this value has N users or more. This is
143 /// logically equivalent to getNumUses() >= N.
145 bool hasNUsesOrMore(unsigned N) const;
147 /// getNumUses - This method computes the number of uses of this Value. This
148 /// is a linear time operation. Use hasOneUse, hasNUses, or hasMoreThanNUses
149 /// to check for specific values.
150 unsigned getNumUses() const;
152 /// addUse/killUse - These two methods should only be used by the Use class.
154 void addUse(Use &U) { U.addToList(&UseList); }
156 /// An enumeration for keeping track of the concrete subclass of Value that
157 /// is actually instantiated. Values of this enumeration are kept in the
158 /// Value classes SubclassID field. They are used for concrete type
161 ArgumentVal, // This is an instance of Argument
162 BasicBlockVal, // This is an instance of BasicBlock
163 FunctionVal, // This is an instance of Function
164 GlobalAliasVal, // This is an instance of GlobalAlias
165 GlobalVariableVal, // This is an instance of GlobalVariable
166 UndefValueVal, // This is an instance of UndefValue
167 ConstantExprVal, // This is an instance of ConstantExpr
168 ConstantAggregateZeroVal, // This is an instance of ConstantAggregateNull
169 ConstantIntVal, // This is an instance of ConstantInt
170 ConstantFPVal, // This is an instance of ConstantFP
171 ConstantArrayVal, // This is an instance of ConstantArray
172 ConstantStructVal, // This is an instance of ConstantStruct
173 ConstantVectorVal, // This is an instance of ConstantVector
174 ConstantPointerNullVal, // This is an instance of ConstantPointerNull
175 InlineAsmVal, // This is an instance of InlineAsm
176 InstructionVal, // This is an instance of Instruction
179 ConstantFirstVal = FunctionVal,
180 ConstantLastVal = ConstantPointerNullVal
183 /// getValueID - Return an ID for the concrete type of this object. This is
184 /// used to implement the classof checks. This should not be used for any
185 /// other purpose, as the values may change as LLVM evolves. Also, note that
186 /// for instructions, the Instruction's opcode is added to InstructionVal. So
187 /// this means three things:
188 /// # there is no value with code InstructionVal (no opcode==0).
189 /// # there are more possible values for the value type than in ValueTy enum.
190 /// # the InstructionVal enumerator must be the highest valued enumerator in
191 /// the ValueTy enum.
192 unsigned getValueID() const {
196 // Methods for support type inquiry through isa, cast, and dyn_cast:
197 static inline bool classof(const Value *) {
198 return true; // Values are always values.
201 /// getRawType - This should only be used to implement the vmcore library.
203 const Type *getRawType() const { return Ty.getRawType(); }
206 inline std::ostream &operator<<(std::ostream &OS, const Value &V) {
211 void Use::init(Value *v, User *user) {
214 if (Val) Val->addUse(*this);
218 if (Val) removeFromList();
221 void Use::set(Value *V) {
222 if (Val) removeFromList();
224 if (V) V->addUse(*this);
228 // isa - Provide some specializations of isa so that we don't have to include
229 // the subtype header files to test to see if the value is a subclass...
231 template <> inline bool isa_impl<Constant, Value>(const Value &Val) {
232 return Val.getValueID() >= Value::ConstantFirstVal &&
233 Val.getValueID() <= Value::ConstantLastVal;
235 template <> inline bool isa_impl<Argument, Value>(const Value &Val) {
236 return Val.getValueID() == Value::ArgumentVal;
238 template <> inline bool isa_impl<InlineAsm, Value>(const Value &Val) {
239 return Val.getValueID() == Value::InlineAsmVal;
241 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) {
242 return Val.getValueID() >= Value::InstructionVal;
244 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) {
245 return Val.getValueID() == Value::BasicBlockVal;
247 template <> inline bool isa_impl<Function, Value>(const Value &Val) {
248 return Val.getValueID() == Value::FunctionVal;
250 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) {
251 return Val.getValueID() == Value::GlobalVariableVal;
253 template <> inline bool isa_impl<GlobalAlias, Value>(const Value &Val) {
254 return Val.getValueID() == Value::GlobalAliasVal;
256 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) {
257 return isa<GlobalVariable>(Val) || isa<Function>(Val) || isa<GlobalAlias>(Val);
260 } // End llvm namespace