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/ADT/StringRef.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/Support/Casting.h"
35 class ValueSymbolTable;
36 class TypeSymbolTable;
37 template<typename ValueTy> class StringMapEntry;
38 template <typename ValueTy = Value>
40 typedef StringMapEntry<Value*> ValueName;
42 class AssemblyAnnotationWriter;
43 class ValueHandleBase;
46 //===----------------------------------------------------------------------===//
48 //===----------------------------------------------------------------------===//
50 /// This is a very important LLVM class. It is the base class of all values
51 /// computed by a program that may be used as operands to other values. Value is
52 /// the super class of other important classes such as Instruction and Function.
53 /// All Values have a Type. Type is not a subclass of Value. All types can have
54 /// a name and they should belong to some Module. Setting the name on the Value
55 /// automatically updates the module's symbol table.
57 /// Every value has a "use list" that keeps track of which other Values are
58 /// using this Value. A Value can also have an arbitrary number of ValueHandle
59 /// objects that watch it and listen to RAUW and Destroy events see
60 /// llvm/Support/ValueHandle.h for details.
62 /// @brief LLVM Value Representation
64 const unsigned char SubclassID; // Subclass identifier (for isa/dyn_cast)
65 unsigned char HasValueHandle : 1; // Has a ValueHandle pointing to this?
67 /// SubclassOptionalData - This member is similar to SubclassData, however it
68 /// is for holding information which may be used to aid optimization, but
69 /// which may be cleared to zero without affecting conservative
71 unsigned char SubclassOptionalData : 7;
73 /// SubclassData - This member is defined by this class, but is not used for
74 /// anything. Subclasses can use it to hold whatever state they find useful.
75 /// This field is initialized to zero by the ctor.
76 unsigned short SubclassData;
81 friend class ValueSymbolTable; // Allow ValueSymbolTable to directly mod Name.
82 friend class SymbolTable; // Allow SymbolTable to directly poke Name.
83 friend class ValueHandleBase;
86 void operator=(const Value &); // Do not implement
87 Value(const Value &); // Do not implement
90 Value(const Type *Ty, unsigned scid);
93 /// dump - Support for debugging, callable in GDB: V->dump()
95 virtual void dump() const;
97 /// print - Implement operator<< on Value.
99 void print(raw_ostream &O, AssemblyAnnotationWriter *AAW = 0) const;
101 /// All values are typed, get the type of this value.
103 inline const Type *getType() const { return VTy; }
105 /// All values hold a context through their type.
106 LLVMContext &getContext() const;
108 // All values can potentially be named...
109 inline bool hasName() const { return Name != 0; }
110 ValueName *getValueName() const { return Name; }
112 /// getName() - Return a constant reference to the value's name. This is cheap
113 /// and guaranteed to return the same reference as long as the value is not
116 /// This is currently guaranteed to return a StringRef for which data() points
117 /// to a valid null terminated string. The use of StringRef.data() is
118 /// deprecated here, however, and clients should not rely on it. If such
119 /// behavior is needed, clients should use expensive getNameStr(), or switch
120 /// to an interface that does not depend on null termination.
121 StringRef getName() const;
123 /// getNameStr() - Return the name of the specified value, *constructing a
124 /// string* to hold it. This is guaranteed to construct a string and is very
125 /// expensive, clients should use getName() unless necessary.
126 std::string getNameStr() const;
128 /// setName() - Change the name of the value, choosing a new unique name if
129 /// the provided name is taken.
131 /// \arg Name - The new name; or "" if the value's name should be removed.
132 void setName(const Twine &Name);
135 /// takeName - transfer the name from V to this value, setting V's name to
136 /// empty. It is an error to call V->takeName(V).
137 void takeName(Value *V);
139 /// replaceAllUsesWith - Go through the uses list for this definition and make
140 /// each use point to "V" instead of "this". After this completes, 'this's
141 /// use list is guaranteed to be empty.
143 void replaceAllUsesWith(Value *V);
145 // uncheckedReplaceAllUsesWith - Just like replaceAllUsesWith but dangerous.
146 // Only use when in type resolution situations!
147 void uncheckedReplaceAllUsesWith(Value *V);
149 //----------------------------------------------------------------------
150 // Methods for handling the chain of uses of this Value.
152 typedef value_use_iterator<User> use_iterator;
153 typedef value_use_iterator<const User> use_const_iterator;
155 bool use_empty() const { return UseList == 0; }
156 use_iterator use_begin() { return use_iterator(UseList); }
157 use_const_iterator use_begin() const { return use_const_iterator(UseList); }
158 use_iterator use_end() { return use_iterator(0); }
159 use_const_iterator use_end() const { return use_const_iterator(0); }
160 User *use_back() { return *use_begin(); }
161 const User *use_back() const { return *use_begin(); }
163 /// hasOneUse - Return true if there is exactly one user of this value. This
164 /// is specialized because it is a common request and does not require
165 /// traversing the whole use list.
167 bool hasOneUse() const {
168 use_const_iterator I = use_begin(), E = use_end();
169 if (I == E) return false;
173 /// hasNUses - Return true if this Value has exactly N users.
175 bool hasNUses(unsigned N) const;
177 /// hasNUsesOrMore - Return true if this value has N users or more. This is
178 /// logically equivalent to getNumUses() >= N.
180 bool hasNUsesOrMore(unsigned N) const;
182 bool isUsedInBasicBlock(const BasicBlock *BB) const;
184 /// getNumUses - This method computes the number of uses of this Value. This
185 /// is a linear time operation. Use hasOneUse, hasNUses, or hasMoreThanNUses
186 /// to check for specific values.
187 unsigned getNumUses() const;
189 /// addUse - This method should only be used by the Use class.
191 void addUse(Use &U) { U.addToList(&UseList); }
193 /// An enumeration for keeping track of the concrete subclass of Value that
194 /// is actually instantiated. Values of this enumeration are kept in the
195 /// Value classes SubclassID field. They are used for concrete type
198 ArgumentVal, // This is an instance of Argument
199 BasicBlockVal, // This is an instance of BasicBlock
200 FunctionVal, // This is an instance of Function
201 GlobalAliasVal, // This is an instance of GlobalAlias
202 GlobalVariableVal, // This is an instance of GlobalVariable
203 UndefValueVal, // This is an instance of UndefValue
204 ConstantExprVal, // This is an instance of ConstantExpr
205 ConstantAggregateZeroVal, // This is an instance of ConstantAggregateNull
206 ConstantIntVal, // This is an instance of ConstantInt
207 ConstantFPVal, // This is an instance of ConstantFP
208 ConstantArrayVal, // This is an instance of ConstantArray
209 ConstantStructVal, // This is an instance of ConstantStruct
210 ConstantVectorVal, // This is an instance of ConstantVector
211 ConstantPointerNullVal, // This is an instance of ConstantPointerNull
212 MDNodeVal, // This is an instance of MDNode
213 MDStringVal, // This is an instance of MDString
214 NamedMDNodeVal, // This is an instance of NamedMDNode
215 InlineAsmVal, // This is an instance of InlineAsm
216 PseudoSourceValueVal, // This is an instance of PseudoSourceValue
217 InstructionVal, // This is an instance of Instruction
220 ConstantFirstVal = FunctionVal,
221 ConstantLastVal = ConstantPointerNullVal
224 /// getValueID - Return an ID for the concrete type of this object. This is
225 /// used to implement the classof checks. This should not be used for any
226 /// other purpose, as the values may change as LLVM evolves. Also, note that
227 /// for instructions, the Instruction's opcode is added to InstructionVal. So
228 /// this means three things:
229 /// # there is no value with code InstructionVal (no opcode==0).
230 /// # there are more possible values for the value type than in ValueTy enum.
231 /// # the InstructionVal enumerator must be the highest valued enumerator in
232 /// the ValueTy enum.
233 unsigned getValueID() const {
237 /// getRawSubclassOptionalData - Return the raw optional flags value
238 /// contained in this value. This should only be used when testing two
239 /// Values for equivalence.
240 unsigned getRawSubclassOptionalData() const {
241 return SubclassOptionalData;
244 /// hasSameSubclassOptionalData - Test whether the optional flags contained
245 /// in this value are equal to the optional flags in the given value.
246 bool hasSameSubclassOptionalData(const Value *V) const {
247 return SubclassOptionalData == V->SubclassOptionalData;
250 /// intersectOptionalDataWith - Clear any optional flags in this value
251 /// that are not also set in the given value.
252 void intersectOptionalDataWith(const Value *V) {
253 SubclassOptionalData &= V->SubclassOptionalData;
256 // Methods for support type inquiry through isa, cast, and dyn_cast:
257 static inline bool classof(const Value *) {
258 return true; // Values are always values.
261 /// getRawType - This should only be used to implement the vmcore library.
263 const Type *getRawType() const { return VTy.getRawType(); }
265 /// stripPointerCasts - This method strips off any unneeded pointer
266 /// casts from the specified value, returning the original uncasted value.
267 /// Note that the returned value has pointer type if the specified value does.
268 Value *stripPointerCasts();
269 const Value *stripPointerCasts() const {
270 return const_cast<Value*>(this)->stripPointerCasts();
273 /// getUnderlyingObject - This method strips off any GEP address adjustments
274 /// and pointer casts from the specified value, returning the original object
275 /// being addressed. Note that the returned value has pointer type if the
276 /// specified value does.
277 Value *getUnderlyingObject();
278 const Value *getUnderlyingObject() const {
279 return const_cast<Value*>(this)->getUnderlyingObject();
282 /// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
283 /// return the value in the PHI node corresponding to PredBB. If not, return
284 /// ourself. This is useful if you want to know the value something has in a
285 /// predecessor block.
286 Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB);
288 const Value *DoPHITranslation(const BasicBlock *CurBB,
289 const BasicBlock *PredBB) const{
290 return const_cast<Value*>(this)->DoPHITranslation(CurBB, PredBB);
294 inline raw_ostream &operator<<(raw_ostream &OS, const Value &V) {
299 void Use::set(Value *V) {
300 if (Val) removeFromList();
302 if (V) V->addUse(*this);
306 // isa - Provide some specializations of isa so that we don't have to include
307 // the subtype header files to test to see if the value is a subclass...
309 template <> inline bool isa_impl<Constant, Value>(const Value &Val) {
310 return Val.getValueID() >= Value::ConstantFirstVal &&
311 Val.getValueID() <= Value::ConstantLastVal;
313 template <> inline bool isa_impl<Argument, Value>(const Value &Val) {
314 return Val.getValueID() == Value::ArgumentVal;
316 template <> inline bool isa_impl<InlineAsm, Value>(const Value &Val) {
317 return Val.getValueID() == Value::InlineAsmVal;
319 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) {
320 return Val.getValueID() >= Value::InstructionVal;
322 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) {
323 return Val.getValueID() == Value::BasicBlockVal;
325 template <> inline bool isa_impl<Function, Value>(const Value &Val) {
326 return Val.getValueID() == Value::FunctionVal;
328 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) {
329 return Val.getValueID() == Value::GlobalVariableVal;
331 template <> inline bool isa_impl<GlobalAlias, Value>(const Value &Val) {
332 return Val.getValueID() == Value::GlobalAliasVal;
334 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) {
335 return isa<GlobalVariable>(Val) || isa<Function>(Val) ||
336 isa<GlobalAlias>(Val);
340 // Value* is only 4-byte aligned.
342 class PointerLikeTypeTraits<Value*> {
345 static inline void *getAsVoidPointer(PT P) { return P; }
346 static inline PT getFromVoidPointer(void *P) {
347 return static_cast<PT>(P);
349 enum { NumLowBitsAvailable = 2 };
352 } // End llvm namespace