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 template <typename ValueTy = Value>
39 typedef StringMapEntry<Value*> ValueName;
41 class AssemblyAnnotationWriter;
42 class ValueHandleBase;
45 //===----------------------------------------------------------------------===//
47 //===----------------------------------------------------------------------===//
49 /// This is a very important LLVM class. It is the base class of all values
50 /// computed by a program that may be used as operands to other values. Value is
51 /// the super class of other important classes such as Instruction and Function.
52 /// All Values have a Type. Type is not a subclass of Value. All types can have
53 /// a name and they should belong to some Module. Setting the name on the Value
54 /// automatically updates the module's symbol table.
56 /// Every value has a "use list" that keeps track of which other Values are
57 /// using this Value. A Value can also have an arbitrary number of ValueHandle
58 /// objects that watch it and listen to RAUW and Destroy events see
59 /// llvm/Support/ValueHandle.h for details.
61 /// @brief LLVM Value Representation
63 const unsigned char SubclassID; // Subclass identifier (for isa/dyn_cast)
64 unsigned char HasValueHandle : 1; // Has a ValueHandle pointing to this?
66 /// SubclassOptionalData - This member is similar to SubclassData, however it
67 /// is for holding information which may be used to aid optimization, but
68 /// which may be cleared to zero without affecting conservative
70 unsigned char SubclassOptionalData : 7;
72 /// SubclassData - This member is defined by this class, but is not used for
73 /// anything. Subclasses can use it to hold whatever state they find useful.
74 /// This field is initialized to zero by the ctor.
75 unsigned short SubclassData;
80 friend class ValueSymbolTable; // Allow ValueSymbolTable to directly mod Name.
81 friend class SymbolTable; // Allow SymbolTable to directly poke Name.
82 friend class ValueHandleBase;
85 void operator=(const Value &); // Do not implement
86 Value(const Value &); // Do not implement
89 Value(const Type *Ty, unsigned scid);
92 /// dump - Support for debugging, callable in GDB: V->dump()
94 virtual void dump() const;
96 /// print - Implement operator<< on Value.
98 void print(std::ostream &O, AssemblyAnnotationWriter *AAW = 0) const;
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 /// getNameStart - Return a pointer to a null terminated string for this name.
113 /// Note that names can have null characters within the string as well as at
114 /// their end. This always returns a non-null pointer.
115 const char *getNameStart() const;
116 /// getNameEnd - Return a pointer to the end of the name.
117 const char *getNameEnd() const { return getNameStart() + getNameLen(); }
119 /// isName - Return true if this value has the name specified by the provided
120 /// nul terminated string.
121 bool isName(const char *N) const;
123 /// getNameLen - Return the length of the string, correctly handling nul
124 /// characters embedded into them.
125 unsigned getNameLen() const;
127 /// getName()/getNameStr() - Return the name of the specified value,
128 /// *constructing a string* to hold it. Because these are guaranteed to
129 /// construct a string, they are very expensive and should be avoided.
130 std::string getName() const { return getNameStr(); }
131 std::string getNameStr() const;
134 void setName(const std::string &name);
135 void setName(const char *Name, unsigned NameLen);
136 void setName(const char *Name); // Takes a null-terminated string.
139 /// takeName - transfer the name from V to this value, setting V's name to
140 /// empty. It is an error to call V->takeName(V).
141 void takeName(Value *V);
143 /// replaceAllUsesWith - Go through the uses list for this definition and make
144 /// each use point to "V" instead of "this". After this completes, 'this's
145 /// use list is guaranteed to be empty.
147 void replaceAllUsesWith(Value *V);
149 // uncheckedReplaceAllUsesWith - Just like replaceAllUsesWith but dangerous.
150 // Only use when in type resolution situations!
151 void uncheckedReplaceAllUsesWith(Value *V);
153 /// clearOptionalData - Clear any optional optimization data from this Value.
154 /// Transformation passes must call this method whenever changing the IR
155 /// in a way that would affect the values produced by this Value, unless
156 /// it takes special care to ensure correctness in some other way.
157 void clearOptionalData() { SubclassOptionalData = 0; }
159 //----------------------------------------------------------------------
160 // Methods for handling the chain of uses of this Value.
162 typedef value_use_iterator<User> use_iterator;
163 typedef value_use_iterator<const User> use_const_iterator;
165 bool use_empty() const { return UseList == 0; }
166 use_iterator use_begin() { return use_iterator(UseList); }
167 use_const_iterator use_begin() const { return use_const_iterator(UseList); }
168 use_iterator use_end() { return use_iterator(0); }
169 use_const_iterator use_end() const { return use_const_iterator(0); }
170 User *use_back() { return *use_begin(); }
171 const User *use_back() const { return *use_begin(); }
173 /// hasOneUse - Return true if there is exactly one user of this value. This
174 /// is specialized because it is a common request and does not require
175 /// traversing the whole use list.
177 bool hasOneUse() const {
178 use_const_iterator I = use_begin(), E = use_end();
179 if (I == E) return false;
183 /// hasNUses - Return true if this Value has exactly N users.
185 bool hasNUses(unsigned N) const;
187 /// hasNUsesOrMore - Return true if this value has N users or more. This is
188 /// logically equivalent to getNumUses() >= N.
190 bool hasNUsesOrMore(unsigned N) const;
192 bool isUsedInBasicBlock(const BasicBlock *BB) const;
194 /// getNumUses - This method computes the number of uses of this Value. This
195 /// is a linear time operation. Use hasOneUse, hasNUses, or hasMoreThanNUses
196 /// to check for specific values.
197 unsigned getNumUses() const;
199 /// addUse - This method should only be used by the Use class.
201 void addUse(Use &U) { U.addToList(&UseList); }
203 /// An enumeration for keeping track of the concrete subclass of Value that
204 /// is actually instantiated. Values of this enumeration are kept in the
205 /// Value classes SubclassID field. They are used for concrete type
208 ArgumentVal, // This is an instance of Argument
209 BasicBlockVal, // This is an instance of BasicBlock
210 FunctionVal, // This is an instance of Function
211 GlobalAliasVal, // This is an instance of GlobalAlias
212 GlobalVariableVal, // This is an instance of GlobalVariable
213 UndefValueVal, // This is an instance of UndefValue
214 ConstantExprVal, // This is an instance of ConstantExpr
215 ConstantAggregateZeroVal, // This is an instance of ConstantAggregateNull
216 ConstantIntVal, // This is an instance of ConstantInt
217 ConstantFPVal, // This is an instance of ConstantFP
218 ConstantArrayVal, // This is an instance of ConstantArray
219 ConstantStructVal, // This is an instance of ConstantStruct
220 ConstantVectorVal, // This is an instance of ConstantVector
221 ConstantPointerNullVal, // This is an instance of ConstantPointerNull
222 MDStringVal, // This is an instance of MDString
223 MDNodeVal, // This is an instance of MDNode
224 InlineAsmVal, // This is an instance of InlineAsm
225 PseudoSourceValueVal, // This is an instance of PseudoSourceValue
226 InstructionVal, // This is an instance of Instruction
229 ConstantFirstVal = FunctionVal,
230 ConstantLastVal = MDNodeVal
233 /// getValueID - Return an ID for the concrete type of this object. This is
234 /// used to implement the classof checks. This should not be used for any
235 /// other purpose, as the values may change as LLVM evolves. Also, note that
236 /// for instructions, the Instruction's opcode is added to InstructionVal. So
237 /// this means three things:
238 /// # there is no value with code InstructionVal (no opcode==0).
239 /// # there are more possible values for the value type than in ValueTy enum.
240 /// # the InstructionVal enumerator must be the highest valued enumerator in
241 /// the ValueTy enum.
242 unsigned getValueID() const {
246 // Methods for support type inquiry through isa, cast, and dyn_cast:
247 static inline bool classof(const Value *) {
248 return true; // Values are always values.
251 /// getRawType - This should only be used to implement the vmcore library.
253 const Type *getRawType() const { return VTy.getRawType(); }
255 /// stripPointerCasts - This method strips off any unneeded pointer
256 /// casts from the specified value, returning the original uncasted value.
257 /// Note that the returned value has pointer type if the specified value does.
258 Value *stripPointerCasts();
259 const Value *stripPointerCasts() const {
260 return const_cast<Value*>(this)->stripPointerCasts();
263 /// getUnderlyingObject - This method strips off any GEP address adjustments
264 /// and pointer casts from the specified value, returning the original object
265 /// being addressed. Note that the returned value has pointer type if the
266 /// specified value does.
267 Value *getUnderlyingObject();
268 const Value *getUnderlyingObject() const {
269 return const_cast<Value*>(this)->getUnderlyingObject();
272 /// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
273 /// return the value in the PHI node corresponding to PredBB. If not, return
274 /// ourself. This is useful if you want to know the value something has in a
275 /// predecessor block.
276 Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB);
278 const Value *DoPHITranslation(const BasicBlock *CurBB,
279 const BasicBlock *PredBB) const{
280 return const_cast<Value*>(this)->DoPHITranslation(CurBB, PredBB);
284 inline std::ostream &operator<<(std::ostream &OS, const Value &V) {
288 inline raw_ostream &operator<<(raw_ostream &OS, const Value &V) {
293 void Use::set(Value *V) {
294 if (Val) removeFromList();
296 if (V) V->addUse(*this);
300 // isa - Provide some specializations of isa so that we don't have to include
301 // the subtype header files to test to see if the value is a subclass...
303 template <> inline bool isa_impl<Constant, Value>(const Value &Val) {
304 return Val.getValueID() >= Value::ConstantFirstVal &&
305 Val.getValueID() <= Value::ConstantLastVal;
307 template <> inline bool isa_impl<Argument, Value>(const Value &Val) {
308 return Val.getValueID() == Value::ArgumentVal;
310 template <> inline bool isa_impl<InlineAsm, Value>(const Value &Val) {
311 return Val.getValueID() == Value::InlineAsmVal;
313 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) {
314 return Val.getValueID() >= Value::InstructionVal;
316 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) {
317 return Val.getValueID() == Value::BasicBlockVal;
319 template <> inline bool isa_impl<Function, Value>(const Value &Val) {
320 return Val.getValueID() == Value::FunctionVal;
322 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) {
323 return Val.getValueID() == Value::GlobalVariableVal;
325 template <> inline bool isa_impl<GlobalAlias, Value>(const Value &Val) {
326 return Val.getValueID() == Value::GlobalAliasVal;
328 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) {
329 return isa<GlobalVariable>(Val) || isa<Function>(Val) ||
330 isa<GlobalAlias>(Val);
334 // Value* is only 4-byte aligned.
336 class PointerLikeTypeTraits<Value*> {
339 static inline void *getAsVoidPointer(PT P) { return P; }
340 static inline PT getFromVoidPointer(void *P) {
341 return static_cast<PT>(P);
343 enum { NumLowBitsAvailable = 2 };
346 } // End llvm namespace