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"
36 class ValueSymbolTable;
37 class TypeSymbolTable;
38 template<typename ValueTy> class StringMapEntry;
39 template <typename ValueTy = Value>
41 typedef StringMapEntry<Value*> ValueName;
43 class AssemblyAnnotationWriter;
44 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;
74 /// SubclassData - This member is defined by this class, but is not used for
75 /// anything. Subclasses can use it to hold whatever state they find useful.
76 /// This field is initialized to zero by the ctor.
77 unsigned short SubclassData;
82 friend class ValueSymbolTable; // Allow ValueSymbolTable to directly mod Name.
83 friend class SymbolTable; // Allow SymbolTable to directly poke Name.
84 friend class ValueHandleBase;
87 void operator=(const Value &); // Do not implement
88 Value(const Value &); // Do not implement
91 Value(const Type *Ty, unsigned scid);
94 /// dump - Support for debugging, callable in GDB: V->dump()
96 virtual void dump() const;
98 /// print - Implement operator<< on Value.
100 void print(std::ostream &O, AssemblyAnnotationWriter *AAW = 0) const;
101 void print(raw_ostream &O, AssemblyAnnotationWriter *AAW = 0) const;
103 /// All values are typed, get the type of this value.
105 inline const Type *getType() const { return VTy; }
107 /// All values hold a context through their type.
108 LLVMContext &getContext() const;
110 // All values can potentially be named...
111 inline bool hasName() const { return Name != 0; }
112 ValueName *getValueName() const { return Name; }
114 /// getNameStart - Return a pointer to a null terminated string for this name.
115 /// Note that names can have null characters within the string as well as at
116 /// their end. This always returns a non-null pointer.
117 const char *getNameStart() const;
118 /// getNameEnd - Return a pointer to the end of the name.
119 const char *getNameEnd() const { return getNameStart() + getNameLen(); }
121 /// isName - Return true if this value has the name specified by the provided
122 /// nul terminated string.
123 bool isName(const char *N) const;
125 /// getNameLen - Return the length of the string, correctly handling nul
126 /// characters embedded into them.
127 unsigned getNameLen() const;
129 /// getName()/getNameStr() - Return the name of the specified value,
130 /// *constructing a string* to hold it. Because these are guaranteed to
131 /// construct a string, they are very expensive and should be avoided.
132 StringRef getName() const { return StringRef(getNameStart(), getNameLen()); }
133 std::string getNameStr() const;
134 StringRef getNameRef() const;
136 void setName(const Twine &Name);
137 void setName(const char *Name, unsigned NameLen);
138 void setName(const char *Name); // Takes a null-terminated string.
141 /// takeName - transfer the name from V to this value, setting V's name to
142 /// empty. It is an error to call V->takeName(V).
143 void takeName(Value *V);
145 /// replaceAllUsesWith - Go through the uses list for this definition and make
146 /// each use point to "V" instead of "this". After this completes, 'this's
147 /// use list is guaranteed to be empty.
149 void replaceAllUsesWith(Value *V);
151 // uncheckedReplaceAllUsesWith - Just like replaceAllUsesWith but dangerous.
152 // Only use when in type resolution situations!
153 void uncheckedReplaceAllUsesWith(Value *V);
155 /// clearOptionalData - Clear any optional optimization data from this Value.
156 /// Transformation passes must call this method whenever changing the IR
157 /// in a way that would affect the values produced by this Value, unless
158 /// it takes special care to ensure correctness in some other way.
159 void clearOptionalData() { SubclassOptionalData = 0; }
161 //----------------------------------------------------------------------
162 // Methods for handling the chain of uses of this Value.
164 typedef value_use_iterator<User> use_iterator;
165 typedef value_use_iterator<const User> use_const_iterator;
167 bool use_empty() const { return UseList == 0; }
168 use_iterator use_begin() { return use_iterator(UseList); }
169 use_const_iterator use_begin() const { return use_const_iterator(UseList); }
170 use_iterator use_end() { return use_iterator(0); }
171 use_const_iterator use_end() const { return use_const_iterator(0); }
172 User *use_back() { return *use_begin(); }
173 const User *use_back() const { return *use_begin(); }
175 /// hasOneUse - Return true if there is exactly one user of this value. This
176 /// is specialized because it is a common request and does not require
177 /// traversing the whole use list.
179 bool hasOneUse() const {
180 use_const_iterator I = use_begin(), E = use_end();
181 if (I == E) return false;
185 /// hasNUses - Return true if this Value has exactly N users.
187 bool hasNUses(unsigned N) const;
189 /// hasNUsesOrMore - Return true if this value has N users or more. This is
190 /// logically equivalent to getNumUses() >= N.
192 bool hasNUsesOrMore(unsigned N) const;
194 bool isUsedInBasicBlock(const BasicBlock *BB) const;
196 /// getNumUses - This method computes the number of uses of this Value. This
197 /// is a linear time operation. Use hasOneUse, hasNUses, or hasMoreThanNUses
198 /// to check for specific values.
199 unsigned getNumUses() const;
201 /// addUse - This method should only be used by the Use class.
203 void addUse(Use &U) { U.addToList(&UseList); }
205 /// An enumeration for keeping track of the concrete subclass of Value that
206 /// is actually instantiated. Values of this enumeration are kept in the
207 /// Value classes SubclassID field. They are used for concrete type
210 ArgumentVal, // This is an instance of Argument
211 BasicBlockVal, // This is an instance of BasicBlock
212 FunctionVal, // This is an instance of Function
213 GlobalAliasVal, // This is an instance of GlobalAlias
214 GlobalVariableVal, // This is an instance of GlobalVariable
215 UndefValueVal, // This is an instance of UndefValue
216 ConstantExprVal, // This is an instance of ConstantExpr
217 ConstantAggregateZeroVal, // This is an instance of ConstantAggregateNull
218 ConstantIntVal, // This is an instance of ConstantInt
219 ConstantFPVal, // This is an instance of ConstantFP
220 ConstantArrayVal, // This is an instance of ConstantArray
221 ConstantStructVal, // This is an instance of ConstantStruct
222 ConstantVectorVal, // This is an instance of ConstantVector
223 ConstantPointerNullVal, // This is an instance of ConstantPointerNull
224 MDNodeVal, // This is an instance of MDNode
225 MDStringVal, // This is an instance of MDString
226 InlineAsmVal, // This is an instance of InlineAsm
227 PseudoSourceValueVal, // This is an instance of PseudoSourceValue
228 InstructionVal, // This is an instance of Instruction
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 // Methods for support type inquiry through isa, cast, and dyn_cast:
249 static inline bool classof(const Value *) {
250 return true; // Values are always values.
253 /// getRawType - This should only be used to implement the vmcore library.
255 const Type *getRawType() const { return VTy.getRawType(); }
257 /// stripPointerCasts - This method strips off any unneeded pointer
258 /// casts from the specified value, returning the original uncasted value.
259 /// Note that the returned value has pointer type if the specified value does.
260 Value *stripPointerCasts();
261 const Value *stripPointerCasts() const {
262 return const_cast<Value*>(this)->stripPointerCasts();
265 /// getUnderlyingObject - This method strips off any GEP address adjustments
266 /// and pointer casts from the specified value, returning the original object
267 /// being addressed. Note that the returned value has pointer type if the
268 /// specified value does.
269 Value *getUnderlyingObject();
270 const Value *getUnderlyingObject() const {
271 return const_cast<Value*>(this)->getUnderlyingObject();
274 /// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
275 /// return the value in the PHI node corresponding to PredBB. If not, return
276 /// ourself. This is useful if you want to know the value something has in a
277 /// predecessor block.
278 Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB);
280 const Value *DoPHITranslation(const BasicBlock *CurBB,
281 const BasicBlock *PredBB) const{
282 return const_cast<Value*>(this)->DoPHITranslation(CurBB, PredBB);
286 inline std::ostream &operator<<(std::ostream &OS, const Value &V) {
290 inline raw_ostream &operator<<(raw_ostream &OS, const Value &V) {
295 void Use::set(Value *V) {
296 if (Val) removeFromList();
298 if (V) V->addUse(*this);
302 // isa - Provide some specializations of isa so that we don't have to include
303 // the subtype header files to test to see if the value is a subclass...
305 template <> inline bool isa_impl<Constant, Value>(const Value &Val) {
306 return Val.getValueID() >= Value::ConstantFirstVal &&
307 Val.getValueID() <= Value::ConstantLastVal;
309 template <> inline bool isa_impl<Argument, Value>(const Value &Val) {
310 return Val.getValueID() == Value::ArgumentVal;
312 template <> inline bool isa_impl<InlineAsm, Value>(const Value &Val) {
313 return Val.getValueID() == Value::InlineAsmVal;
315 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) {
316 return Val.getValueID() >= Value::InstructionVal;
318 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) {
319 return Val.getValueID() == Value::BasicBlockVal;
321 template <> inline bool isa_impl<Function, Value>(const Value &Val) {
322 return Val.getValueID() == Value::FunctionVal;
324 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) {
325 return Val.getValueID() == Value::GlobalVariableVal;
327 template <> inline bool isa_impl<GlobalAlias, Value>(const Value &Val) {
328 return Val.getValueID() == Value::GlobalAliasVal;
330 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) {
331 return isa<GlobalVariable>(Val) || isa<Function>(Val) ||
332 isa<GlobalAlias>(Val);
336 // Value* is only 4-byte aligned.
338 class PointerLikeTypeTraits<Value*> {
341 static inline void *getAsVoidPointer(PT P) { return P; }
342 static inline PT getFromVoidPointer(void *P) {
343 return static_cast<PT>(P);
345 enum { NumLowBitsAvailable = 2 };
348 } // End llvm namespace