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 //===----------------------------------------------------------------------===//
18 #include "llvm/Support/Casting.h"
31 class ValueSymbolTable;
32 template<typename ValueTy> class StringMapEntry;
33 typedef StringMapEntry<Value*> ValueName;
35 class AssemblyAnnotationWriter;
36 class ValueHandleBase;
43 //===----------------------------------------------------------------------===//
45 //===----------------------------------------------------------------------===//
47 /// This is a very important LLVM class. It is the base class of all values
48 /// computed by a program that may be used as operands to other values. Value is
49 /// the super class of other important classes such as Instruction and Function.
50 /// All Values have a Type. Type is not a subclass of Value. Some values can
51 /// have a name and they belong to some Module. Setting the name on the Value
52 /// automatically updates the module's symbol table.
54 /// Every value has a "use list" that keeps track of which other Values are
55 /// using this Value. A Value can also have an arbitrary number of ValueHandle
56 /// objects that watch it and listen to RAUW and Destroy events. See
57 /// llvm/Support/ValueHandle.h for details.
59 /// @brief LLVM Value Representation
61 const unsigned char SubclassID; // Subclass identifier (for isa/dyn_cast)
62 unsigned char HasValueHandle : 1; // Has a ValueHandle pointing to this?
64 /// SubclassOptionalData - This member is similar to SubclassData, however it
65 /// is for holding information which may be used to aid optimization, but
66 /// which may be cleared to zero without affecting conservative
68 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 ValueHandleBase;
83 void operator=(const Value &); // Do not implement
84 Value(const Value &); // Do not implement
87 /// printCustom - Value subclasses can override this to implement custom
88 /// printing behavior.
89 virtual void printCustom(raw_ostream &O) const;
91 Value(Type *Ty, unsigned scid);
95 /// dump - Support for debugging, callable in GDB: V->dump()
99 /// print - Implement operator<< on Value.
101 void print(raw_ostream &O, AssemblyAnnotationWriter *AAW = 0) const;
103 /// All values are typed, get the type of this value.
105 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 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 /// setName() - Change the name of the value, choosing a new unique name if
120 /// the provided name is taken.
122 /// \arg Name - The new name; or "" if the value's name should be removed.
123 void setName(const Twine &Name);
126 /// takeName - transfer the name from V to this value, setting V's name to
127 /// empty. It is an error to call V->takeName(V).
128 void takeName(Value *V);
130 /// replaceAllUsesWith - Go through the uses list for this definition and make
131 /// each use point to "V" instead of "this". After this completes, 'this's
132 /// use list is guaranteed to be empty.
134 void replaceAllUsesWith(Value *V);
136 //----------------------------------------------------------------------
137 // Methods for handling the chain of uses of this Value.
139 typedef value_use_iterator<User> use_iterator;
140 typedef value_use_iterator<const User> const_use_iterator;
142 bool use_empty() const { return UseList == 0; }
143 use_iterator use_begin() { return use_iterator(UseList); }
144 const_use_iterator use_begin() const { return const_use_iterator(UseList); }
145 use_iterator use_end() { return use_iterator(0); }
146 const_use_iterator use_end() const { return const_use_iterator(0); }
147 User *use_back() { return *use_begin(); }
148 const User *use_back() const { return *use_begin(); }
150 /// hasOneUse - Return true if there is exactly one user of this value. This
151 /// is specialized because it is a common request and does not require
152 /// traversing the whole use list.
154 bool hasOneUse() const {
155 const_use_iterator I = use_begin(), E = use_end();
156 if (I == E) return false;
160 /// hasNUses - Return true if this Value has exactly N users.
162 bool hasNUses(unsigned N) const;
164 /// hasNUsesOrMore - Return true if this value has N users or more. This is
165 /// logically equivalent to getNumUses() >= N.
167 bool hasNUsesOrMore(unsigned N) const;
169 bool isUsedInBasicBlock(const BasicBlock *BB) const;
171 /// getNumUses - This method computes the number of uses of this Value. This
172 /// is a linear time operation. Use hasOneUse, hasNUses, or hasNUsesOrMore
173 /// to check for specific values.
174 unsigned getNumUses() const;
176 /// addUse - This method should only be used by the Use class.
178 void addUse(Use &U) { U.addToList(&UseList); }
180 /// An enumeration for keeping track of the concrete subclass of Value that
181 /// is actually instantiated. Values of this enumeration are kept in the
182 /// Value classes SubclassID field. They are used for concrete type
185 ArgumentVal, // This is an instance of Argument
186 BasicBlockVal, // This is an instance of BasicBlock
187 FunctionVal, // This is an instance of Function
188 GlobalAliasVal, // This is an instance of GlobalAlias
189 GlobalVariableVal, // This is an instance of GlobalVariable
190 UndefValueVal, // This is an instance of UndefValue
191 BlockAddressVal, // This is an instance of BlockAddress
192 ConstantExprVal, // This is an instance of ConstantExpr
193 ConstantAggregateZeroVal, // This is an instance of ConstantAggregateZero
194 ConstantDataArrayVal, // This is an instance of ConstantDataArray
195 ConstantDataVectorVal, // This is an instance of ConstantDataVector
196 ConstantIntVal, // This is an instance of ConstantInt
197 ConstantFPVal, // This is an instance of ConstantFP
198 ConstantArrayVal, // This is an instance of ConstantArray
199 ConstantStructVal, // This is an instance of ConstantStruct
200 ConstantVectorVal, // This is an instance of ConstantVector
201 ConstantPointerNullVal, // This is an instance of ConstantPointerNull
202 MDNodeVal, // This is an instance of MDNode
203 MDStringVal, // This is an instance of MDString
204 InlineAsmVal, // This is an instance of InlineAsm
205 PseudoSourceValueVal, // This is an instance of PseudoSourceValue
206 FixedStackPseudoSourceValueVal, // This is an instance of
207 // FixedStackPseudoSourceValue
208 InstructionVal, // This is an instance of Instruction
209 // Enum values starting at InstructionVal are used for Instructions;
210 // don't add new values here!
213 ConstantFirstVal = FunctionVal,
214 ConstantLastVal = ConstantPointerNullVal
217 /// getValueID - Return an ID for the concrete type of this object. This is
218 /// used to implement the classof checks. This should not be used for any
219 /// other purpose, as the values may change as LLVM evolves. Also, note that
220 /// for instructions, the Instruction's opcode is added to InstructionVal. So
221 /// this means three things:
222 /// # there is no value with code InstructionVal (no opcode==0).
223 /// # there are more possible values for the value type than in ValueTy enum.
224 /// # the InstructionVal enumerator must be the highest valued enumerator in
225 /// the ValueTy enum.
226 unsigned getValueID() const {
230 /// getRawSubclassOptionalData - Return the raw optional flags value
231 /// contained in this value. This should only be used when testing two
232 /// Values for equivalence.
233 unsigned getRawSubclassOptionalData() const {
234 return SubclassOptionalData;
237 /// clearSubclassOptionalData - Clear the optional flags contained in
239 void clearSubclassOptionalData() {
240 SubclassOptionalData = 0;
243 /// hasSameSubclassOptionalData - Test whether the optional flags contained
244 /// in this value are equal to the optional flags in the given value.
245 bool hasSameSubclassOptionalData(const Value *V) const {
246 return SubclassOptionalData == V->SubclassOptionalData;
249 /// intersectOptionalDataWith - Clear any optional flags in this value
250 /// that are not also set in the given value.
251 void intersectOptionalDataWith(const Value *V) {
252 SubclassOptionalData &= V->SubclassOptionalData;
255 /// hasValueHandle - Return true if there is a value handle associated with
257 bool hasValueHandle() const { return HasValueHandle; }
259 // Methods for support type inquiry through isa, cast, and dyn_cast:
260 static inline bool classof(const Value *) {
261 return true; // Values are always values.
264 /// stripPointerCasts - This method strips off any unneeded pointer casts and
265 /// all-zero GEPs from the specified value, returning the original uncasted
266 /// value. If this is called on a non-pointer value, it returns 'this'.
267 Value *stripPointerCasts();
268 const Value *stripPointerCasts() const {
269 return const_cast<Value*>(this)->stripPointerCasts();
272 /// stripInBoundsConstantOffsets - This method strips off unneeded pointer casts and
273 /// all-constant GEPs from the specified value, returning the original
274 /// pointer value. If this is called on a non-pointer value, it returns
276 Value *stripInBoundsConstantOffsets();
277 const Value *stripInBoundsConstantOffsets() const {
278 return const_cast<Value*>(this)->stripInBoundsConstantOffsets();
281 /// stripInBoundsOffsets - This method strips off unneeded pointer casts and
282 /// any in-bounds Offsets from the specified value, returning the original
283 /// pointer value. If this is called on a non-pointer value, it returns
285 Value *stripInBoundsOffsets();
286 const Value *stripInBoundsOffsets() const {
287 return const_cast<Value*>(this)->stripInBoundsOffsets();
290 /// isDereferenceablePointer - Test if this value is always a pointer to
291 /// allocated and suitably aligned memory for a simple load or store.
292 bool isDereferenceablePointer() const;
294 /// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
295 /// return the value in the PHI node corresponding to PredBB. If not, return
296 /// ourself. This is useful if you want to know the value something has in a
297 /// predecessor block.
298 Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB);
300 const Value *DoPHITranslation(const BasicBlock *CurBB,
301 const BasicBlock *PredBB) const{
302 return const_cast<Value*>(this)->DoPHITranslation(CurBB, PredBB);
305 /// MaximumAlignment - This is the greatest alignment value supported by
306 /// load, store, and alloca instructions, and global values.
307 static const unsigned MaximumAlignment = 1u << 29;
309 /// mutateType - Mutate the type of this Value to be of the specified type.
310 /// Note that this is an extremely dangerous operation which can create
311 /// completely invalid IR very easily. It is strongly recommended that you
312 /// recreate IR objects with the right types instead of mutating them in
314 void mutateType(Type *Ty) {
319 unsigned short getSubclassDataFromValue() const { return SubclassData; }
320 void setValueSubclassData(unsigned short D) { SubclassData = D; }
323 inline raw_ostream &operator<<(raw_ostream &OS, const Value &V) {
328 void Use::set(Value *V) {
329 if (Val) removeFromList();
331 if (V) V->addUse(*this);
335 // isa - Provide some specializations of isa so that we don't have to include
336 // the subtype header files to test to see if the value is a subclass...
338 template <> struct isa_impl<Constant, Value> {
339 static inline bool doit(const Value &Val) {
340 return Val.getValueID() >= Value::ConstantFirstVal &&
341 Val.getValueID() <= Value::ConstantLastVal;
345 template <> struct isa_impl<Argument, Value> {
346 static inline bool doit (const Value &Val) {
347 return Val.getValueID() == Value::ArgumentVal;
351 template <> struct isa_impl<InlineAsm, Value> {
352 static inline bool doit(const Value &Val) {
353 return Val.getValueID() == Value::InlineAsmVal;
357 template <> struct isa_impl<Instruction, Value> {
358 static inline bool doit(const Value &Val) {
359 return Val.getValueID() >= Value::InstructionVal;
363 template <> struct isa_impl<BasicBlock, Value> {
364 static inline bool doit(const Value &Val) {
365 return Val.getValueID() == Value::BasicBlockVal;
369 template <> struct isa_impl<Function, Value> {
370 static inline bool doit(const Value &Val) {
371 return Val.getValueID() == Value::FunctionVal;
375 template <> struct isa_impl<GlobalVariable, Value> {
376 static inline bool doit(const Value &Val) {
377 return Val.getValueID() == Value::GlobalVariableVal;
381 template <> struct isa_impl<GlobalAlias, Value> {
382 static inline bool doit(const Value &Val) {
383 return Val.getValueID() == Value::GlobalAliasVal;
387 template <> struct isa_impl<GlobalValue, Value> {
388 static inline bool doit(const Value &Val) {
389 return isa<GlobalVariable>(Val) || isa<Function>(Val) ||
390 isa<GlobalAlias>(Val);
394 template <> struct isa_impl<MDNode, Value> {
395 static inline bool doit(const Value &Val) {
396 return Val.getValueID() == Value::MDNodeVal;
400 // Value* is only 4-byte aligned.
402 class PointerLikeTypeTraits<Value*> {
405 static inline void *getAsVoidPointer(PT P) { return P; }
406 static inline PT getFromVoidPointer(void *P) {
407 return static_cast<PT>(P);
409 enum { NumLowBitsAvailable = 2 };
412 } // End llvm namespace