1 //===-- llvm/Value.h - Definition of the Value class ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the Value class.
12 //===----------------------------------------------------------------------===//
17 #include "llvm/AbstractTypeUser.h"
19 #include "llvm/Support/Casting.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 updates 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 ValueName *getValueName() const { return Name; }
92 /// getNameStart - Return a pointer to a null terminated string for this name.
93 /// Note that names can have null characters within the string as well as at
94 /// their end. This always returns a non-null pointer.
95 const char *getNameStart() const;
97 /// isName - Return true if this value has the name specified by the provided
98 /// nul terminated string.
99 bool isName(const char *N) const;
101 /// getNameLen - Return the length of the string, correctly handling nul
102 /// characters embedded into them.
103 unsigned getNameLen() const;
105 /// getName()/getNameStr() - Return the name of the specified value,
106 /// *constructing a string* to hold it. Because these are guaranteed to
107 /// construct a string, they are very expensive and should be avoided.
108 std::string getName() const { return getNameStr(); }
109 std::string getNameStr() const;
112 void setName(const std::string &name);
113 void setName(const char *Name, unsigned NameLen);
114 void setName(const char *Name); // Takes a null-terminated string.
117 /// takeName - transfer the name from V to this value, setting V's name to
118 /// empty. It is an error to call V->takeName(V).
119 void takeName(Value *V);
121 /// replaceAllUsesWith - Go through the uses list for this definition and make
122 /// each use point to "V" instead of "this". After this completes, 'this's
123 /// use list is guaranteed to be empty.
125 void replaceAllUsesWith(Value *V);
127 // uncheckedReplaceAllUsesWith - Just like replaceAllUsesWith but dangerous.
128 // Only use when in type resolution situations!
129 void uncheckedReplaceAllUsesWith(Value *V);
131 //----------------------------------------------------------------------
132 // Methods for handling the vector of uses of this Value.
134 typedef value_use_iterator<User> use_iterator;
135 typedef value_use_iterator<const User> use_const_iterator;
137 bool use_empty() const { return UseList == 0; }
138 use_iterator use_begin() { return use_iterator(UseList); }
139 use_const_iterator use_begin() const { return use_const_iterator(UseList); }
140 use_iterator use_end() { return use_iterator(0); }
141 use_const_iterator use_end() const { return use_const_iterator(0); }
142 User *use_back() { return *use_begin(); }
143 const User *use_back() const { return *use_begin(); }
145 /// hasOneUse - Return true if there is exactly one user of this value. This
146 /// is specialized because it is a common request and does not require
147 /// traversing the whole use list.
149 bool hasOneUse() const {
150 use_const_iterator I = use_begin(), E = use_end();
151 if (I == E) return false;
155 /// hasNUses - Return true if this Value has exactly N users.
157 bool hasNUses(unsigned N) const;
159 /// hasNUsesOrMore - Return true if this value has N users or more. This is
160 /// logically equivalent to getNumUses() >= N.
162 bool hasNUsesOrMore(unsigned N) const;
164 bool isUsedInBasicBlock(BasicBlock *BB) const;
166 /// getNumUses - This method computes the number of uses of this Value. This
167 /// is a linear time operation. Use hasOneUse, hasNUses, or hasMoreThanNUses
168 /// to check for specific values.
169 unsigned getNumUses() const;
171 /// addUse - This method should only be used by the Use class.
173 void addUse(Use &U) { U.addToList(&UseList); }
175 /// An enumeration for keeping track of the concrete subclass of Value that
176 /// is actually instantiated. Values of this enumeration are kept in the
177 /// Value classes SubclassID field. They are used for concrete type
180 ArgumentVal, // This is an instance of Argument
181 BasicBlockVal, // This is an instance of BasicBlock
182 FunctionVal, // This is an instance of Function
183 GlobalAliasVal, // This is an instance of GlobalAlias
184 GlobalVariableVal, // This is an instance of GlobalVariable
185 UndefValueVal, // This is an instance of UndefValue
186 ConstantExprVal, // This is an instance of ConstantExpr
187 ConstantAggregateZeroVal, // This is an instance of ConstantAggregateNull
188 ConstantIntVal, // This is an instance of ConstantInt
189 ConstantFPVal, // This is an instance of ConstantFP
190 ConstantArrayVal, // This is an instance of ConstantArray
191 ConstantStructVal, // This is an instance of ConstantStruct
192 ConstantVectorVal, // This is an instance of ConstantVector
193 ConstantPointerNullVal, // This is an instance of ConstantPointerNull
194 InlineAsmVal, // This is an instance of InlineAsm
195 PseudoSourceValueVal, // This is an instance of PseudoSourceValue
196 InstructionVal, // This is an instance of Instruction
199 ConstantFirstVal = FunctionVal,
200 ConstantLastVal = ConstantPointerNullVal
203 /// getValueID - Return an ID for the concrete type of this object. This is
204 /// used to implement the classof checks. This should not be used for any
205 /// other purpose, as the values may change as LLVM evolves. Also, note that
206 /// for instructions, the Instruction's opcode is added to InstructionVal. So
207 /// this means three things:
208 /// # there is no value with code InstructionVal (no opcode==0).
209 /// # there are more possible values for the value type than in ValueTy enum.
210 /// # the InstructionVal enumerator must be the highest valued enumerator in
211 /// the ValueTy enum.
212 unsigned getValueID() const {
216 // Methods for support type inquiry through isa, cast, and dyn_cast:
217 static inline bool classof(const Value *) {
218 return true; // Values are always values.
221 /// getRawType - This should only be used to implement the vmcore library.
223 const Type *getRawType() const { return Ty.getRawType(); }
225 /// stripPointerCasts - This method strips off any unneeded pointer
226 /// casts from the specified value, returning the original uncasted value.
227 /// Note that the returned value is guaranteed to have pointer type.
228 Value *stripPointerCasts();
229 const Value *stripPointerCasts() const {
230 return const_cast<Value*>(this)->stripPointerCasts();
234 inline std::ostream &operator<<(std::ostream &OS, const Value &V) {
239 void Use::init(Value *V, User *) {
241 if (V) V->addUse(*this);
244 void Use::set(Value *V) {
245 if (Val) removeFromList();
247 if (V) V->addUse(*this);
251 // isa - Provide some specializations of isa so that we don't have to include
252 // the subtype header files to test to see if the value is a subclass...
254 template <> inline bool isa_impl<Constant, Value>(const Value &Val) {
255 return Val.getValueID() >= Value::ConstantFirstVal &&
256 Val.getValueID() <= Value::ConstantLastVal;
258 template <> inline bool isa_impl<Argument, Value>(const Value &Val) {
259 return Val.getValueID() == Value::ArgumentVal;
261 template <> inline bool isa_impl<InlineAsm, Value>(const Value &Val) {
262 return Val.getValueID() == Value::InlineAsmVal;
264 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) {
265 return Val.getValueID() >= Value::InstructionVal;
267 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) {
268 return Val.getValueID() == Value::BasicBlockVal;
270 template <> inline bool isa_impl<Function, Value>(const Value &Val) {
271 return Val.getValueID() == Value::FunctionVal;
273 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) {
274 return Val.getValueID() == Value::GlobalVariableVal;
276 template <> inline bool isa_impl<GlobalAlias, Value>(const Value &Val) {
277 return Val.getValueID() == Value::GlobalAliasVal;
279 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) {
280 return isa<GlobalVariable>(Val) || isa<Function>(Val) || isa<GlobalAlias>(Val);
283 } // End llvm namespace