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;
44 //===----------------------------------------------------------------------===//
46 //===----------------------------------------------------------------------===//
48 /// This is a very important LLVM class. It is the base class of all values
49 /// computed by a program that may be used as operands to other values. Value is
50 /// the super class of other important classes such as Instruction and Function.
51 /// All Values have a Type. Type is not a subclass of Value. All types can have
52 /// a name and they should belong to some Module. Setting the name on the Value
53 /// automatically updates the module's symbol table.
55 /// Every value has a "use list" that keeps track of which other Values are
56 /// using this Value. A Value can also have an arbitrary number of ValueHandle
57 /// objects that watch it and listen to RAUW and Destroy events see
58 /// llvm/Support/ValueHandle.h for details.
60 /// @brief LLVM Value Representation
62 const unsigned char SubclassID; // Subclass identifier (for isa/dyn_cast)
63 unsigned char HasValueHandle : 1; // Has a ValueHandle pointing to this?
65 /// SubclassOptionalData - This member is similar to SubclassData, however it
66 /// is for holding information which may be used to aid optimization, but
67 /// which may be cleared to zero without affecting conservative
69 unsigned char SubclassOptionalData : 7;
71 /// SubclassData - This member is defined by this class, but is not used for
72 /// anything. Subclasses can use it to hold whatever state they find useful.
73 /// This field is initialized to zero by the ctor.
74 unsigned short SubclassData;
79 friend class ValueSymbolTable; // Allow ValueSymbolTable to directly mod Name.
80 friend class SymbolTable; // Allow SymbolTable to directly poke Name.
81 friend class ValueHandleBase;
84 void operator=(const Value &); // Do not implement
85 Value(const Value &); // Do not implement
88 Value(const Type *Ty, unsigned scid);
91 /// dump - Support for debugging, callable in GDB: V->dump()
93 virtual void dump() const;
95 /// print - Implement operator<< on Value.
97 void print(std::ostream &O, AssemblyAnnotationWriter *AAW = 0) const;
98 void print(raw_ostream &O, AssemblyAnnotationWriter *AAW = 0) const;
100 /// All values are typed, get the type of this value.
102 inline const Type *getType() const { return VTy; }
104 // All values can potentially be named...
105 inline bool hasName() const { return Name != 0; }
106 ValueName *getValueName() const { return Name; }
108 /// getNameStart - Return a pointer to a null terminated string for this name.
109 /// Note that names can have null characters within the string as well as at
110 /// their end. This always returns a non-null pointer.
111 const char *getNameStart() const;
112 /// getNameEnd - Return a pointer to the end of the name.
113 const char *getNameEnd() const { return getNameStart() + getNameLen(); }
115 /// isName - Return true if this value has the name specified by the provided
116 /// nul terminated string.
117 bool isName(const char *N) const;
119 /// getNameLen - Return the length of the string, correctly handling nul
120 /// characters embedded into them.
121 unsigned getNameLen() const;
123 /// getName()/getNameStr() - Return the name of the specified value,
124 /// *constructing a string* to hold it. Because these are guaranteed to
125 /// construct a string, they are very expensive and should be avoided.
126 std::string getName() const { return getNameStr(); }
127 std::string getNameStr() const;
130 void setName(const std::string &name);
131 void setName(const char *Name, unsigned NameLen);
132 void setName(const char *Name); // Takes a null-terminated string.
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 /// clearOptionalData - Clear any optional optimization data from this Value.
150 /// Transformation passes must call this method whenever changing the IR
151 /// in a way that would affect the values produced by this Value, unless
152 /// it takes special care to ensure correctness in some other way.
153 void clearOptionalData() { SubclassOptionalData = 0; }
155 //----------------------------------------------------------------------
156 // Methods for handling the chain of uses of this Value.
158 typedef value_use_iterator<User> use_iterator;
159 typedef value_use_iterator<const User> use_const_iterator;
161 bool use_empty() const { return UseList == 0; }
162 use_iterator use_begin() { return use_iterator(UseList); }
163 use_const_iterator use_begin() const { return use_const_iterator(UseList); }
164 use_iterator use_end() { return use_iterator(0); }
165 use_const_iterator use_end() const { return use_const_iterator(0); }
166 User *use_back() { return *use_begin(); }
167 const User *use_back() const { return *use_begin(); }
169 /// hasOneUse - Return true if there is exactly one user of this value. This
170 /// is specialized because it is a common request and does not require
171 /// traversing the whole use list.
173 bool hasOneUse() const {
174 use_const_iterator I = use_begin(), E = use_end();
175 if (I == E) return false;
179 /// hasNUses - Return true if this Value has exactly N users.
181 bool hasNUses(unsigned N) const;
183 /// hasNUsesOrMore - Return true if this value has N users or more. This is
184 /// logically equivalent to getNumUses() >= N.
186 bool hasNUsesOrMore(unsigned N) const;
188 bool isUsedInBasicBlock(const BasicBlock *BB) const;
190 /// getNumUses - This method computes the number of uses of this Value. This
191 /// is a linear time operation. Use hasOneUse, hasNUses, or hasMoreThanNUses
192 /// to check for specific values.
193 unsigned getNumUses() const;
195 /// addUse - This method should only be used by the Use class.
197 void addUse(Use &U) { U.addToList(&UseList); }
199 /// An enumeration for keeping track of the concrete subclass of Value that
200 /// is actually instantiated. Values of this enumeration are kept in the
201 /// Value classes SubclassID field. They are used for concrete type
204 ArgumentVal, // This is an instance of Argument
205 BasicBlockVal, // This is an instance of BasicBlock
206 FunctionVal, // This is an instance of Function
207 GlobalAliasVal, // This is an instance of GlobalAlias
208 GlobalVariableVal, // This is an instance of GlobalVariable
209 UndefValueVal, // This is an instance of UndefValue
210 ConstantExprVal, // This is an instance of ConstantExpr
211 ConstantAggregateZeroVal, // This is an instance of ConstantAggregateNull
212 ConstantIntVal, // This is an instance of ConstantInt
213 ConstantFPVal, // This is an instance of ConstantFP
214 ConstantArrayVal, // This is an instance of ConstantArray
215 ConstantStructVal, // This is an instance of ConstantStruct
216 ConstantVectorVal, // This is an instance of ConstantVector
217 ConstantPointerNullVal, // This is an instance of ConstantPointerNull
218 MDStringVal, // This is an instance of MDString
219 MDNodeVal, // This is an instance of MDNode
220 InlineAsmVal, // This is an instance of InlineAsm
221 PseudoSourceValueVal, // This is an instance of PseudoSourceValue
222 InstructionVal, // This is an instance of Instruction
225 ConstantFirstVal = FunctionVal,
226 ConstantLastVal = MDNodeVal
229 /// getValueID - Return an ID for the concrete type of this object. This is
230 /// used to implement the classof checks. This should not be used for any
231 /// other purpose, as the values may change as LLVM evolves. Also, note that
232 /// for instructions, the Instruction's opcode is added to InstructionVal. So
233 /// this means three things:
234 /// # there is no value with code InstructionVal (no opcode==0).
235 /// # there are more possible values for the value type than in ValueTy enum.
236 /// # the InstructionVal enumerator must be the highest valued enumerator in
237 /// the ValueTy enum.
238 unsigned getValueID() const {
242 // Methods for support type inquiry through isa, cast, and dyn_cast:
243 static inline bool classof(const Value *) {
244 return true; // Values are always values.
247 /// getRawType - This should only be used to implement the vmcore library.
249 const Type *getRawType() const { return VTy.getRawType(); }
251 /// stripPointerCasts - This method strips off any unneeded pointer
252 /// casts from the specified value, returning the original uncasted value.
253 /// Note that the returned value has pointer type if the specified value does.
254 Value *stripPointerCasts();
255 const Value *stripPointerCasts() const {
256 return const_cast<Value*>(this)->stripPointerCasts();
259 /// getUnderlyingObject - This method strips off any GEP address adjustments
260 /// and pointer casts from the specified value, returning the original object
261 /// being addressed. Note that the returned value has pointer type if the
262 /// specified value does.
263 Value *getUnderlyingObject();
264 const Value *getUnderlyingObject() const {
265 return const_cast<Value*>(this)->getUnderlyingObject();
268 /// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
269 /// return the value in the PHI node corresponding to PredBB. If not, return
270 /// ourself. This is useful if you want to know the value something has in a
271 /// predecessor block.
272 Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB);
274 const Value *DoPHITranslation(const BasicBlock *CurBB,
275 const BasicBlock *PredBB) const{
276 return const_cast<Value*>(this)->DoPHITranslation(CurBB, PredBB);
280 inline std::ostream &operator<<(std::ostream &OS, const Value &V) {
284 inline raw_ostream &operator<<(raw_ostream &OS, const Value &V) {
289 void Use::set(Value *V) {
290 if (Val) removeFromList();
292 if (V) V->addUse(*this);
296 // isa - Provide some specializations of isa so that we don't have to include
297 // the subtype header files to test to see if the value is a subclass...
299 template <> inline bool isa_impl<Constant, Value>(const Value &Val) {
300 return Val.getValueID() >= Value::ConstantFirstVal &&
301 Val.getValueID() <= Value::ConstantLastVal;
303 template <> inline bool isa_impl<Argument, Value>(const Value &Val) {
304 return Val.getValueID() == Value::ArgumentVal;
306 template <> inline bool isa_impl<InlineAsm, Value>(const Value &Val) {
307 return Val.getValueID() == Value::InlineAsmVal;
309 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) {
310 return Val.getValueID() >= Value::InstructionVal;
312 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) {
313 return Val.getValueID() == Value::BasicBlockVal;
315 template <> inline bool isa_impl<Function, Value>(const Value &Val) {
316 return Val.getValueID() == Value::FunctionVal;
318 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) {
319 return Val.getValueID() == Value::GlobalVariableVal;
321 template <> inline bool isa_impl<GlobalAlias, Value>(const Value &Val) {
322 return Val.getValueID() == Value::GlobalAliasVal;
324 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) {
325 return isa<GlobalVariable>(Val) || isa<Function>(Val) ||
326 isa<GlobalAlias>(Val);
330 // Value* is only 4-byte aligned.
332 class PointerLikeTypeTraits<Value*> {
335 static inline void *getAsVoidPointer(PT P) { return P; }
336 static inline PT getFromVoidPointer(void *P) {
337 return static_cast<PT>(P);
339 enum { NumLowBitsAvailable = 2 };
342 } // End llvm namespace