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 /// getName() - Return a constant reference to the value's name. This is cheap
115 /// and guaranteed to return the same reference as long as the value is not
117 StringRef getName() const;
119 /// getNameStr() - Return the name of the specified value, *constructing a
120 /// string* to hold it. This is guaranteed to construct a string and is very
121 /// expensive, clients should use getName() unless necessary.
122 std::string getNameStr() const;
124 /// setName() - Change the name of the value, choosing a new unique name if
125 /// the provided name is taken.
127 /// \arg Name - The new name; or "" if the value's name should be removed.
128 void setName(const Twine &Name);
131 /// takeName - transfer the name from V to this value, setting V's name to
132 /// empty. It is an error to call V->takeName(V).
133 void takeName(Value *V);
135 /// replaceAllUsesWith - Go through the uses list for this definition and make
136 /// each use point to "V" instead of "this". After this completes, 'this's
137 /// use list is guaranteed to be empty.
139 void replaceAllUsesWith(Value *V);
141 // uncheckedReplaceAllUsesWith - Just like replaceAllUsesWith but dangerous.
142 // Only use when in type resolution situations!
143 void uncheckedReplaceAllUsesWith(Value *V);
145 /// clearOptionalData - Clear any optional optimization data from this Value.
146 /// Transformation passes must call this method whenever changing the IR
147 /// in a way that would affect the values produced by this Value, unless
148 /// it takes special care to ensure correctness in some other way.
149 void clearOptionalData() { SubclassOptionalData = 0; }
151 //----------------------------------------------------------------------
152 // Methods for handling the chain of uses of this Value.
154 typedef value_use_iterator<User> use_iterator;
155 typedef value_use_iterator<const User> use_const_iterator;
157 bool use_empty() const { return UseList == 0; }
158 use_iterator use_begin() { return use_iterator(UseList); }
159 use_const_iterator use_begin() const { return use_const_iterator(UseList); }
160 use_iterator use_end() { return use_iterator(0); }
161 use_const_iterator use_end() const { return use_const_iterator(0); }
162 User *use_back() { return *use_begin(); }
163 const User *use_back() const { return *use_begin(); }
165 /// hasOneUse - Return true if there is exactly one user of this value. This
166 /// is specialized because it is a common request and does not require
167 /// traversing the whole use list.
169 bool hasOneUse() const {
170 use_const_iterator I = use_begin(), E = use_end();
171 if (I == E) return false;
175 /// hasNUses - Return true if this Value has exactly N users.
177 bool hasNUses(unsigned N) const;
179 /// hasNUsesOrMore - Return true if this value has N users or more. This is
180 /// logically equivalent to getNumUses() >= N.
182 bool hasNUsesOrMore(unsigned N) const;
184 bool isUsedInBasicBlock(const BasicBlock *BB) const;
186 /// getNumUses - This method computes the number of uses of this Value. This
187 /// is a linear time operation. Use hasOneUse, hasNUses, or hasMoreThanNUses
188 /// to check for specific values.
189 unsigned getNumUses() const;
191 /// addUse - This method should only be used by the Use class.
193 void addUse(Use &U) { U.addToList(&UseList); }
195 /// An enumeration for keeping track of the concrete subclass of Value that
196 /// is actually instantiated. Values of this enumeration are kept in the
197 /// Value classes SubclassID field. They are used for concrete type
200 ArgumentVal, // This is an instance of Argument
201 BasicBlockVal, // This is an instance of BasicBlock
202 FunctionVal, // This is an instance of Function
203 GlobalAliasVal, // This is an instance of GlobalAlias
204 GlobalVariableVal, // This is an instance of GlobalVariable
205 UndefValueVal, // This is an instance of UndefValue
206 ConstantExprVal, // This is an instance of ConstantExpr
207 ConstantAggregateZeroVal, // This is an instance of ConstantAggregateNull
208 ConstantIntVal, // This is an instance of ConstantInt
209 ConstantFPVal, // This is an instance of ConstantFP
210 ConstantArrayVal, // This is an instance of ConstantArray
211 ConstantStructVal, // This is an instance of ConstantStruct
212 ConstantVectorVal, // This is an instance of ConstantVector
213 ConstantPointerNullVal, // This is an instance of ConstantPointerNull
214 MDNodeVal, // This is an instance of MDNode
215 MDStringVal, // This is an instance of MDString
216 InlineAsmVal, // This is an instance of InlineAsm
217 PseudoSourceValueVal, // This is an instance of PseudoSourceValue
218 InstructionVal, // This is an instance of Instruction
221 ConstantFirstVal = FunctionVal,
222 ConstantLastVal = ConstantPointerNullVal
225 /// getValueID - Return an ID for the concrete type of this object. This is
226 /// used to implement the classof checks. This should not be used for any
227 /// other purpose, as the values may change as LLVM evolves. Also, note that
228 /// for instructions, the Instruction's opcode is added to InstructionVal. So
229 /// this means three things:
230 /// # there is no value with code InstructionVal (no opcode==0).
231 /// # there are more possible values for the value type than in ValueTy enum.
232 /// # the InstructionVal enumerator must be the highest valued enumerator in
233 /// the ValueTy enum.
234 unsigned getValueID() const {
238 // Methods for support type inquiry through isa, cast, and dyn_cast:
239 static inline bool classof(const Value *) {
240 return true; // Values are always values.
243 /// getRawType - This should only be used to implement the vmcore library.
245 const Type *getRawType() const { return VTy.getRawType(); }
247 /// stripPointerCasts - This method strips off any unneeded pointer
248 /// casts from the specified value, returning the original uncasted value.
249 /// Note that the returned value has pointer type if the specified value does.
250 Value *stripPointerCasts();
251 const Value *stripPointerCasts() const {
252 return const_cast<Value*>(this)->stripPointerCasts();
255 /// getUnderlyingObject - This method strips off any GEP address adjustments
256 /// and pointer casts from the specified value, returning the original object
257 /// being addressed. Note that the returned value has pointer type if the
258 /// specified value does.
259 Value *getUnderlyingObject();
260 const Value *getUnderlyingObject() const {
261 return const_cast<Value*>(this)->getUnderlyingObject();
264 /// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
265 /// return the value in the PHI node corresponding to PredBB. If not, return
266 /// ourself. This is useful if you want to know the value something has in a
267 /// predecessor block.
268 Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB);
270 const Value *DoPHITranslation(const BasicBlock *CurBB,
271 const BasicBlock *PredBB) const{
272 return const_cast<Value*>(this)->DoPHITranslation(CurBB, PredBB);
276 inline std::ostream &operator<<(std::ostream &OS, const Value &V) {
280 inline raw_ostream &operator<<(raw_ostream &OS, const Value &V) {
285 void Use::set(Value *V) {
286 if (Val) removeFromList();
288 if (V) V->addUse(*this);
292 // isa - Provide some specializations of isa so that we don't have to include
293 // the subtype header files to test to see if the value is a subclass...
295 template <> inline bool isa_impl<Constant, Value>(const Value &Val) {
296 return Val.getValueID() >= Value::ConstantFirstVal &&
297 Val.getValueID() <= Value::ConstantLastVal;
299 template <> inline bool isa_impl<Argument, Value>(const Value &Val) {
300 return Val.getValueID() == Value::ArgumentVal;
302 template <> inline bool isa_impl<InlineAsm, Value>(const Value &Val) {
303 return Val.getValueID() == Value::InlineAsmVal;
305 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) {
306 return Val.getValueID() >= Value::InstructionVal;
308 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) {
309 return Val.getValueID() == Value::BasicBlockVal;
311 template <> inline bool isa_impl<Function, Value>(const Value &Val) {
312 return Val.getValueID() == Value::FunctionVal;
314 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) {
315 return Val.getValueID() == Value::GlobalVariableVal;
317 template <> inline bool isa_impl<GlobalAlias, Value>(const Value &Val) {
318 return Val.getValueID() == Value::GlobalAliasVal;
320 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) {
321 return isa<GlobalVariable>(Val) || isa<Function>(Val) ||
322 isa<GlobalAlias>(Val);
326 // Value* is only 4-byte aligned.
328 class PointerLikeTypeTraits<Value*> {
331 static inline void *getAsVoidPointer(PT P) { return P; }
332 static inline PT getFromVoidPointer(void *P) {
333 return static_cast<PT>(P);
335 enum { NumLowBitsAvailable = 2 };
338 } // End llvm namespace