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 class AssemblyAnnotationWriter;
40 class ValueHandleBase;
42 //===----------------------------------------------------------------------===//
44 //===----------------------------------------------------------------------===//
46 /// This is a very important LLVM class. It is the base class of all values
47 /// computed by a program that may be used as operands to other values. Value is
48 /// the super class of other important classes such as Instruction and Function.
49 /// All Values have a Type. Type is not a subclass of Value. All types can have
50 /// a name and they should belong to some Module. Setting the name on the Value
51 /// automatically updates the module's symbol table.
53 /// Every value has a "use list" that keeps track of which other Values are
54 /// using this Value. A Value can also have an arbitrary number of ValueHandle
55 /// objects that watch it and listen to RAUW and Destroy events see
56 /// llvm/Support/ValueHandle.h for details.
58 /// @brief LLVM Value Representation
60 const unsigned char SubclassID; // Subclass identifier (for isa/dyn_cast)
61 unsigned char HasValueHandle : 1; // Has a ValueHandle pointing to this?
63 /// SubclassData - This member is defined by this class, but is not used for
64 /// anything. Subclasses can use it to hold whatever state they find useful.
65 /// This field is initialized to zero by the ctor.
66 unsigned short SubclassData;
71 friend class ValueSymbolTable; // Allow ValueSymbolTable to directly mod Name.
72 friend class SymbolTable; // Allow SymbolTable to directly poke Name.
73 friend class ValueHandleBase;
76 void operator=(const Value &); // Do not implement
77 Value(const Value &); // Do not implement
80 Value(const Type *Ty, unsigned scid);
83 /// dump - Support for debugging, callable in GDB: V->dump()
85 virtual void dump() const;
87 /// print - Implement operator<< on Value.
89 void print(std::ostream &O, AssemblyAnnotationWriter *AAW = 0) const;
90 void print(raw_ostream &O, AssemblyAnnotationWriter *AAW = 0) const;
92 /// All values are typed, get the type of this value.
94 inline const Type *getType() const { return VTy; }
96 // All values can potentially be named...
97 inline bool hasName() const { return Name != 0; }
98 ValueName *getValueName() const { return Name; }
100 /// getNameStart - Return a pointer to a null terminated string for this name.
101 /// Note that names can have null characters within the string as well as at
102 /// their end. This always returns a non-null pointer.
103 const char *getNameStart() const;
104 /// getNameEnd - Return a pointer to the end of the name.
105 const char *getNameEnd() const { return getNameStart() + getNameLen(); }
107 /// isName - Return true if this value has the name specified by the provided
108 /// nul terminated string.
109 bool isName(const char *N) const;
111 /// getNameLen - Return the length of the string, correctly handling nul
112 /// characters embedded into them.
113 unsigned getNameLen() const;
115 /// getName()/getNameStr() - Return the name of the specified value,
116 /// *constructing a string* to hold it. Because these are guaranteed to
117 /// construct a string, they are very expensive and should be avoided.
118 std::string getName() const { return getNameStr(); }
119 std::string getNameStr() const;
122 void setName(const std::string &name);
123 void setName(const char *Name, unsigned NameLen);
124 void setName(const char *Name); // Takes a null-terminated string.
127 /// takeName - transfer the name from V to this value, setting V's name to
128 /// empty. It is an error to call V->takeName(V).
129 void takeName(Value *V);
131 /// replaceAllUsesWith - Go through the uses list for this definition and make
132 /// each use point to "V" instead of "this". After this completes, 'this's
133 /// use list is guaranteed to be empty.
135 void replaceAllUsesWith(Value *V);
137 // uncheckedReplaceAllUsesWith - Just like replaceAllUsesWith but dangerous.
138 // Only use when in type resolution situations!
139 void uncheckedReplaceAllUsesWith(Value *V);
141 //----------------------------------------------------------------------
142 // Methods for handling the chain of uses of this Value.
144 typedef value_use_iterator<User> use_iterator;
145 typedef value_use_iterator<const User> use_const_iterator;
147 bool use_empty() const { return UseList == 0; }
148 use_iterator use_begin() { return use_iterator(UseList); }
149 use_const_iterator use_begin() const { return use_const_iterator(UseList); }
150 use_iterator use_end() { return use_iterator(0); }
151 use_const_iterator use_end() const { return use_const_iterator(0); }
152 User *use_back() { return *use_begin(); }
153 const User *use_back() const { return *use_begin(); }
155 /// hasOneUse - Return true if there is exactly one user of this value. This
156 /// is specialized because it is a common request and does not require
157 /// traversing the whole use list.
159 bool hasOneUse() const {
160 use_const_iterator I = use_begin(), E = use_end();
161 if (I == E) return false;
165 /// hasNUses - Return true if this Value has exactly N users.
167 bool hasNUses(unsigned N) const;
169 /// hasNUsesOrMore - Return true if this value has N users or more. This is
170 /// logically equivalent to getNumUses() >= N.
172 bool hasNUsesOrMore(unsigned N) const;
174 bool isUsedInBasicBlock(const BasicBlock *BB) const;
176 /// getNumUses - This method computes the number of uses of this Value. This
177 /// is a linear time operation. Use hasOneUse, hasNUses, or hasMoreThanNUses
178 /// to check for specific values.
179 unsigned getNumUses() const;
181 /// addUse - This method should only be used by the Use class.
183 void addUse(Use &U) { U.addToList(&UseList); }
185 /// An enumeration for keeping track of the concrete subclass of Value that
186 /// is actually instantiated. Values of this enumeration are kept in the
187 /// Value classes SubclassID field. They are used for concrete type
190 ArgumentVal, // This is an instance of Argument
191 BasicBlockVal, // This is an instance of BasicBlock
192 FunctionVal, // This is an instance of Function
193 GlobalAliasVal, // This is an instance of GlobalAlias
194 GlobalVariableVal, // This is an instance of GlobalVariable
195 UndefValueVal, // This is an instance of UndefValue
196 ConstantExprVal, // This is an instance of ConstantExpr
197 ConstantAggregateZeroVal, // This is an instance of ConstantAggregateNull
198 ConstantIntVal, // This is an instance of ConstantInt
199 ConstantFPVal, // This is an instance of ConstantFP
200 ConstantArrayVal, // This is an instance of ConstantArray
201 ConstantStructVal, // This is an instance of ConstantStruct
202 ConstantVectorVal, // This is an instance of ConstantVector
203 ConstantPointerNullVal, // This is an instance of ConstantPointerNull
204 InlineAsmVal, // This is an instance of InlineAsm
205 PseudoSourceValueVal, // This is an instance of PseudoSourceValue
206 InstructionVal, // This is an instance of Instruction
209 ConstantFirstVal = FunctionVal,
210 ConstantLastVal = ConstantPointerNullVal
213 /// getValueID - Return an ID for the concrete type of this object. This is
214 /// used to implement the classof checks. This should not be used for any
215 /// other purpose, as the values may change as LLVM evolves. Also, note that
216 /// for instructions, the Instruction's opcode is added to InstructionVal. So
217 /// this means three things:
218 /// # there is no value with code InstructionVal (no opcode==0).
219 /// # there are more possible values for the value type than in ValueTy enum.
220 /// # the InstructionVal enumerator must be the highest valued enumerator in
221 /// the ValueTy enum.
222 unsigned getValueID() const {
226 // Methods for support type inquiry through isa, cast, and dyn_cast:
227 static inline bool classof(const Value *) {
228 return true; // Values are always values.
231 /// getRawType - This should only be used to implement the vmcore library.
233 const Type *getRawType() const { return VTy.getRawType(); }
235 /// stripPointerCasts - This method strips off any unneeded pointer
236 /// casts from the specified value, returning the original uncasted value.
237 /// Note that the returned value has pointer type if the specified value does.
238 Value *stripPointerCasts();
239 const Value *stripPointerCasts() const {
240 return const_cast<Value*>(this)->stripPointerCasts();
243 /// getUnderlyingObject - This method strips off any GEP address adjustments
244 /// and pointer casts from the specified value, returning the original object
245 /// being addressed. Note that the returned value has pointer type if the
246 /// specified value does.
247 Value *getUnderlyingObject();
248 const Value *getUnderlyingObject() const {
249 return const_cast<Value*>(this)->getUnderlyingObject();
252 /// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
253 /// return the value in the PHI node corresponding to PredBB. If not, return
254 /// ourself. This is useful if you want to know the value something has in a
255 /// predecessor block.
256 Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB);
258 const Value *DoPHITranslation(const BasicBlock *CurBB,
259 const BasicBlock *PredBB) const{
260 return const_cast<Value*>(this)->DoPHITranslation(CurBB, PredBB);
264 inline std::ostream &operator<<(std::ostream &OS, const Value &V) {
268 inline raw_ostream &operator<<(raw_ostream &OS, const Value &V) {
273 void Use::set(Value *V) {
274 if (Val) removeFromList();
276 if (V) V->addUse(*this);
280 // isa - Provide some specializations of isa so that we don't have to include
281 // the subtype header files to test to see if the value is a subclass...
283 template <> inline bool isa_impl<Constant, Value>(const Value &Val) {
284 return Val.getValueID() >= Value::ConstantFirstVal &&
285 Val.getValueID() <= Value::ConstantLastVal;
287 template <> inline bool isa_impl<Argument, Value>(const Value &Val) {
288 return Val.getValueID() == Value::ArgumentVal;
290 template <> inline bool isa_impl<InlineAsm, Value>(const Value &Val) {
291 return Val.getValueID() == Value::InlineAsmVal;
293 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) {
294 return Val.getValueID() >= Value::InstructionVal;
296 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) {
297 return Val.getValueID() == Value::BasicBlockVal;
299 template <> inline bool isa_impl<Function, Value>(const Value &Val) {
300 return Val.getValueID() == Value::FunctionVal;
302 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) {
303 return Val.getValueID() == Value::GlobalVariableVal;
305 template <> inline bool isa_impl<GlobalAlias, Value>(const Value &Val) {
306 return Val.getValueID() == Value::GlobalAliasVal;
308 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) {
309 return isa<GlobalVariable>(Val) || isa<Function>(Val) ||
310 isa<GlobalAlias>(Val);
314 // Value* is only 4-byte aligned.
316 class PointerLikeTypeTraits<Value*> {
319 static inline void *getAsVoidPointer(PT P) { return P; }
320 static inline PT getFromVoidPointer(void *P) {
321 return static_cast<PT>(P);
323 enum { NumLowBitsAvailable = 2 };
326 } // End llvm namespace