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/Support/Casting.h"
34 class ValueSymbolTable;
35 class TypeSymbolTable;
36 template<typename ValueTy> class StringMapEntry;
37 template <typename ValueTy = Value>
39 typedef StringMapEntry<Value*> ValueName;
41 class AssemblyAnnotationWriter;
42 class ValueHandleBase;
47 //===----------------------------------------------------------------------===//
49 //===----------------------------------------------------------------------===//
51 /// This is a very important LLVM class. It is the base class of all values
52 /// computed by a program that may be used as operands to other values. Value is
53 /// the super class of other important classes such as Instruction and Function.
54 /// All Values have a Type. Type is not a subclass of Value. All types can have
55 /// a name and they should belong to some Module. Setting the name on the Value
56 /// automatically updates the module's symbol table.
58 /// Every value has a "use list" that keeps track of which other Values are
59 /// using this Value. A Value can also have an arbitrary number of ValueHandle
60 /// objects that watch it and listen to RAUW and Destroy events. See
61 /// llvm/Support/ValueHandle.h for details.
63 /// @brief LLVM Value Representation
65 const unsigned char SubclassID; // Subclass identifier (for isa/dyn_cast)
66 unsigned char HasValueHandle : 1; // Has a ValueHandle pointing to this?
68 /// SubclassOptionalData - This member is similar to SubclassData, however it
69 /// is for holding information which may be used to aid optimization, but
70 /// which may be cleared to zero without affecting conservative
72 unsigned char SubclassOptionalData : 7;
75 /// SubclassData - This member is defined by this class, but is not used for
76 /// anything. Subclasses can use it to hold whatever state they find useful.
77 /// This field is initialized to zero by the ctor.
78 unsigned short SubclassData;
83 friend class ValueSymbolTable; // Allow ValueSymbolTable to directly mod Name.
84 friend class ValueHandleBase;
85 friend class AbstractTypeUser;
88 void operator=(const Value &); // Do not implement
89 Value(const Value &); // Do not implement
92 /// printCustom - Value subclasses can override this to implement custom
93 /// printing behavior.
94 virtual void printCustom(raw_ostream &O) const;
96 Value(const Type *Ty, unsigned scid);
100 /// dump - Support for debugging, callable in GDB: V->dump()
104 /// print - Implement operator<< on Value.
106 void print(raw_ostream &O, AssemblyAnnotationWriter *AAW = 0) const;
108 /// All values are typed, get the type of this value.
110 inline const Type *getType() const { return VTy; }
112 /// All values hold a context through their type.
113 LLVMContext &getContext() const;
115 // All values can potentially be named...
116 inline bool hasName() const { return Name != 0; }
117 ValueName *getValueName() const { return Name; }
119 /// getName() - Return a constant reference to the value's name. This is cheap
120 /// and guaranteed to return the same reference as long as the value is not
123 /// This is currently guaranteed to return a StringRef for which data() points
124 /// to a valid null terminated string. The use of StringRef.data() is
125 /// deprecated here, however, and clients should not rely on it. If such
126 /// behavior is needed, clients should use expensive getNameStr(), or switch
127 /// to an interface that does not depend on null termination.
128 StringRef getName() const;
130 /// getNameStr() - Return the name of the specified value, *constructing a
131 /// string* to hold it. This is guaranteed to construct a string and is very
132 /// expensive, clients should use getName() unless necessary.
133 std::string getNameStr() const;
135 /// setName() - Change the name of the value, choosing a new unique name if
136 /// the provided name is taken.
138 /// \arg Name - The new name; or "" if the value's name should be removed.
139 void setName(const Twine &Name);
142 /// takeName - transfer the name from V to this value, setting V's name to
143 /// empty. It is an error to call V->takeName(V).
144 void takeName(Value *V);
146 /// replaceAllUsesWith - Go through the uses list for this definition and make
147 /// each use point to "V" instead of "this". After this completes, 'this's
148 /// use list is guaranteed to be empty.
150 void replaceAllUsesWith(Value *V);
152 // uncheckedReplaceAllUsesWith - Just like replaceAllUsesWith but dangerous.
153 // Only use when in type resolution situations!
154 void uncheckedReplaceAllUsesWith(Value *V);
156 //----------------------------------------------------------------------
157 // Methods for handling the chain of uses of this Value.
159 typedef value_use_iterator<User> use_iterator;
160 typedef value_use_iterator<const User> const_use_iterator;
162 bool use_empty() const { return UseList == 0; }
163 use_iterator use_begin() { return use_iterator(UseList); }
164 const_use_iterator use_begin() const { return const_use_iterator(UseList); }
165 use_iterator use_end() { return use_iterator(0); }
166 const_use_iterator use_end() const { return const_use_iterator(0); }
167 User *use_back() { return *use_begin(); }
168 const User *use_back() const { return *use_begin(); }
170 /// hasOneUse - Return true if there is exactly one user of this value. This
171 /// is specialized because it is a common request and does not require
172 /// traversing the whole use list.
174 bool hasOneUse() const {
175 const_use_iterator I = use_begin(), E = use_end();
176 if (I == E) return false;
180 /// hasNUses - Return true if this Value has exactly N users.
182 bool hasNUses(unsigned N) const;
184 /// hasNUsesOrMore - Return true if this value has N users or more. This is
185 /// logically equivalent to getNumUses() >= N.
187 bool hasNUsesOrMore(unsigned N) const;
189 bool isUsedInBasicBlock(const BasicBlock *BB) const;
191 /// getNumUses - This method computes the number of uses of this Value. This
192 /// is a linear time operation. Use hasOneUse, hasNUses, or hasMoreThanNUses
193 /// to check for specific values.
194 unsigned getNumUses() const;
196 /// addUse - This method should only be used by the Use class.
198 void addUse(Use &U) { U.addToList(&UseList); }
200 /// An enumeration for keeping track of the concrete subclass of Value that
201 /// is actually instantiated. Values of this enumeration are kept in the
202 /// Value classes SubclassID field. They are used for concrete type
205 ArgumentVal, // This is an instance of Argument
206 BasicBlockVal, // This is an instance of BasicBlock
207 FunctionVal, // This is an instance of Function
208 GlobalAliasVal, // This is an instance of GlobalAlias
209 GlobalVariableVal, // This is an instance of GlobalVariable
210 UndefValueVal, // This is an instance of UndefValue
211 BlockAddressVal, // This is an instance of BlockAddress
212 ConstantExprVal, // This is an instance of ConstantExpr
213 ConstantAggregateZeroVal, // This is an instance of ConstantAggregateZero
214 ConstantIntVal, // This is an instance of ConstantInt
215 ConstantFPVal, // This is an instance of ConstantFP
216 ConstantArrayVal, // This is an instance of ConstantArray
217 ConstantStructVal, // This is an instance of ConstantStruct
218 ConstantVectorVal, // This is an instance of ConstantVector
219 ConstantPointerNullVal, // This is an instance of ConstantPointerNull
220 MDNodeVal, // This is an instance of MDNode
221 MDStringVal, // This is an instance of MDString
222 InlineAsmVal, // This is an instance of InlineAsm
223 PseudoSourceValueVal, // This is an instance of PseudoSourceValue
224 FixedStackPseudoSourceValueVal, // This is an instance of
225 // FixedStackPseudoSourceValue
226 InstructionVal, // This is an instance of Instruction
227 // Enum values starting at InstructionVal are used for Instructions;
228 // don't add new values here!
231 ConstantFirstVal = FunctionVal,
232 ConstantLastVal = ConstantPointerNullVal
235 /// getValueID - Return an ID for the concrete type of this object. This is
236 /// used to implement the classof checks. This should not be used for any
237 /// other purpose, as the values may change as LLVM evolves. Also, note that
238 /// for instructions, the Instruction's opcode is added to InstructionVal. So
239 /// this means three things:
240 /// # there is no value with code InstructionVal (no opcode==0).
241 /// # there are more possible values for the value type than in ValueTy enum.
242 /// # the InstructionVal enumerator must be the highest valued enumerator in
243 /// the ValueTy enum.
244 unsigned getValueID() const {
248 /// getRawSubclassOptionalData - Return the raw optional flags value
249 /// contained in this value. This should only be used when testing two
250 /// Values for equivalence.
251 unsigned getRawSubclassOptionalData() const {
252 return SubclassOptionalData;
255 /// hasSameSubclassOptionalData - Test whether the optional flags contained
256 /// in this value are equal to the optional flags in the given value.
257 bool hasSameSubclassOptionalData(const Value *V) const {
258 return SubclassOptionalData == V->SubclassOptionalData;
261 /// intersectOptionalDataWith - Clear any optional flags in this value
262 /// that are not also set in the given value.
263 void intersectOptionalDataWith(const Value *V) {
264 SubclassOptionalData &= V->SubclassOptionalData;
267 /// hasValueHandle - Return true if there is a value handle associated with
269 bool hasValueHandle() const { return HasValueHandle; }
271 // Methods for support type inquiry through isa, cast, and dyn_cast:
272 static inline bool classof(const Value *) {
273 return true; // Values are always values.
276 /// getRawType - This should only be used to implement the vmcore library.
278 const Type *getRawType() const { return VTy.getRawType(); }
280 /// stripPointerCasts - This method strips off any unneeded pointer
281 /// casts from the specified value, returning the original uncasted value.
282 /// Note that the returned value has pointer type if the specified value does.
283 Value *stripPointerCasts();
284 const Value *stripPointerCasts() const {
285 return const_cast<Value*>(this)->stripPointerCasts();
288 /// getUnderlyingObject - This method strips off any GEP address adjustments
289 /// and pointer casts from the specified value, returning the original object
290 /// being addressed. Note that the returned value has pointer type if the
291 /// specified value does. If the MaxLookup value is non-zero, it limits the
292 /// number of instructions to be stripped off.
293 Value *getUnderlyingObject(unsigned MaxLookup = 6);
294 const Value *getUnderlyingObject(unsigned MaxLookup = 6) const {
295 return const_cast<Value*>(this)->getUnderlyingObject(MaxLookup);
298 /// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
299 /// return the value in the PHI node corresponding to PredBB. If not, return
300 /// ourself. This is useful if you want to know the value something has in a
301 /// predecessor block.
302 Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB);
304 const Value *DoPHITranslation(const BasicBlock *CurBB,
305 const BasicBlock *PredBB) const{
306 return const_cast<Value*>(this)->DoPHITranslation(CurBB, PredBB);
309 /// MaximumAlignment - This is the greatest alignment value supported by
310 /// load, store, and alloca instructions, and global values.
311 static const unsigned MaximumAlignment = 1u << 29;
314 unsigned short getSubclassDataFromValue() const { return SubclassData; }
315 void setValueSubclassData(unsigned short D) { SubclassData = D; }
318 inline raw_ostream &operator<<(raw_ostream &OS, const Value &V) {
323 void Use::set(Value *V) {
324 if (Val) removeFromList();
326 if (V) V->addUse(*this);
330 // isa - Provide some specializations of isa so that we don't have to include
331 // the subtype header files to test to see if the value is a subclass...
333 template <> struct isa_impl<Constant, Value> {
334 static inline bool doit(const Value &Val) {
335 return Val.getValueID() >= Value::ConstantFirstVal &&
336 Val.getValueID() <= Value::ConstantLastVal;
340 template <> struct isa_impl<Argument, Value> {
341 static inline bool doit (const Value &Val) {
342 return Val.getValueID() == Value::ArgumentVal;
346 template <> struct isa_impl<InlineAsm, Value> {
347 static inline bool doit(const Value &Val) {
348 return Val.getValueID() == Value::InlineAsmVal;
352 template <> struct isa_impl<Instruction, Value> {
353 static inline bool doit(const Value &Val) {
354 return Val.getValueID() >= Value::InstructionVal;
358 template <> struct isa_impl<BasicBlock, Value> {
359 static inline bool doit(const Value &Val) {
360 return Val.getValueID() == Value::BasicBlockVal;
364 template <> struct isa_impl<Function, Value> {
365 static inline bool doit(const Value &Val) {
366 return Val.getValueID() == Value::FunctionVal;
370 template <> struct isa_impl<GlobalVariable, Value> {
371 static inline bool doit(const Value &Val) {
372 return Val.getValueID() == Value::GlobalVariableVal;
376 template <> struct isa_impl<GlobalAlias, Value> {
377 static inline bool doit(const Value &Val) {
378 return Val.getValueID() == Value::GlobalAliasVal;
382 template <> struct isa_impl<GlobalValue, Value> {
383 static inline bool doit(const Value &Val) {
384 return isa<GlobalVariable>(Val) || isa<Function>(Val) ||
385 isa<GlobalAlias>(Val);
389 template <> struct isa_impl<MDNode, Value> {
390 static inline bool doit(const Value &Val) {
391 return Val.getValueID() == Value::MDNodeVal;
395 // Value* is only 4-byte aligned.
397 class PointerLikeTypeTraits<Value*> {
400 static inline void *getAsVoidPointer(PT P) { return P; }
401 static inline PT getFromVoidPointer(void *P) {
402 return static_cast<PT>(P);
404 enum { NumLowBitsAvailable = 2 };
407 } // End llvm namespace