1 //===- llvm/Value.h - Definition of the Value class -------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file declares the Value class.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_IR_VALUE_H
14 #define LLVM_IR_VALUE_H
16 #include "llvm-c/Types.h"
17 #include "llvm/ADT/iterator_range.h"
18 #include "llvm/IR/Use.h"
19 #include "llvm/Support/CBindingWrapping.h"
20 #include "llvm/Support/Casting.h"
32 class ConstantAggregate
;
37 class GlobalIndirectSymbol
;
45 class ModuleSlotTracker
;
47 template<typename ValueTy
> class StringMapEntry
;
53 using ValueName
= StringMapEntry
<Value
*>;
55 //===----------------------------------------------------------------------===//
57 //===----------------------------------------------------------------------===//
59 /// LLVM Value Representation
61 /// This is a very important LLVM class. It is the base class of all values
62 /// computed by a program that may be used as operands to other values. Value is
63 /// the super class of other important classes such as Instruction and Function.
64 /// All Values have a Type. Type is not a subclass of Value. Some values can
65 /// have a name and they belong to some Module. Setting the name on the Value
66 /// automatically updates the module's symbol table.
68 /// Every value has a "use list" that keeps track of which other Values are
69 /// using this Value. A Value can also have an arbitrary number of ValueHandle
70 /// objects that watch it and listen to RAUW and Destroy events. See
71 /// llvm/IR/ValueHandle.h for details.
73 // The least-significant bit of the first word of Value *must* be zero:
74 // http://www.llvm.org/docs/ProgrammersManual.html#the-waymarking-algorithm
78 friend class ValueAsMetadata
; // Allow access to IsUsedByMD.
79 friend class ValueHandleBase
;
81 const unsigned char SubclassID
; // Subclass identifier (for isa/dyn_cast)
82 unsigned char HasValueHandle
: 1; // Has a ValueHandle pointing to this?
85 /// Hold subclass data that can be dropped.
87 /// This member is similar to SubclassData, however it is for holding
88 /// information which may be used to aid optimization, but which may be
89 /// cleared to zero without affecting conservative interpretation.
90 unsigned char SubclassOptionalData
: 7;
93 /// Hold arbitrary subclass data.
95 /// This member is defined by this class, but is not used for anything.
96 /// Subclasses can use it to hold whatever state they find useful. This
97 /// field is initialized to zero by the ctor.
98 unsigned short SubclassData
;
101 /// The number of operands in the subclass.
103 /// This member is defined by this class, but not used for anything.
104 /// Subclasses can use it to store their number of operands, if they have
107 /// This is stored here to save space in User on 64-bit hosts. Since most
108 /// instances of Value have operands, 32-bit hosts aren't significantly
111 /// Note, this should *NOT* be used directly by any class other than User.
112 /// User uses this value to find the Use list.
113 enum : unsigned { NumUserOperandsBits
= 28 };
114 unsigned NumUserOperands
: NumUserOperandsBits
;
116 // Use the same type as the bitfield above so that MSVC will pack them.
117 unsigned IsUsedByMD
: 1;
118 unsigned HasName
: 1;
119 unsigned HasHungOffUses
: 1;
120 unsigned HasDescriptor
: 1;
123 template <typename UseT
> // UseT == 'Use' or 'const Use'
124 class use_iterator_impl
125 : public std::iterator
<std::forward_iterator_tag
, UseT
*> {
130 explicit use_iterator_impl(UseT
*u
) : U(u
) {}
133 use_iterator_impl() : U() {}
135 bool operator==(const use_iterator_impl
&x
) const { return U
== x
.U
; }
136 bool operator!=(const use_iterator_impl
&x
) const { return !operator==(x
); }
138 use_iterator_impl
&operator++() { // Preincrement
139 assert(U
&& "Cannot increment end iterator!");
144 use_iterator_impl
operator++(int) { // Postincrement
150 UseT
&operator*() const {
151 assert(U
&& "Cannot dereference end iterator!");
155 UseT
*operator->() const { return &operator*(); }
157 operator use_iterator_impl
<const UseT
>() const {
158 return use_iterator_impl
<const UseT
>(U
);
162 template <typename UserTy
> // UserTy == 'User' or 'const User'
163 class user_iterator_impl
164 : public std::iterator
<std::forward_iterator_tag
, UserTy
*> {
165 use_iterator_impl
<Use
> UI
;
166 explicit user_iterator_impl(Use
*U
) : UI(U
) {}
170 user_iterator_impl() = default;
172 bool operator==(const user_iterator_impl
&x
) const { return UI
== x
.UI
; }
173 bool operator!=(const user_iterator_impl
&x
) const { return !operator==(x
); }
175 /// Returns true if this iterator is equal to user_end() on the value.
176 bool atEnd() const { return *this == user_iterator_impl(); }
178 user_iterator_impl
&operator++() { // Preincrement
183 user_iterator_impl
operator++(int) { // Postincrement
189 // Retrieve a pointer to the current User.
190 UserTy
*operator*() const {
191 return UI
->getUser();
194 UserTy
*operator->() const { return operator*(); }
196 operator user_iterator_impl
<const UserTy
>() const {
197 return user_iterator_impl
<const UserTy
>(*UI
);
200 Use
&getUse() const { return *UI
; }
204 Value(Type
*Ty
, unsigned scid
);
206 /// Value's destructor should be virtual by design, but that would require
207 /// that Value and all of its subclasses have a vtable that effectively
208 /// duplicates the information in the value ID. As a size optimization, the
209 /// destructor has been protected, and the caller should manually call
211 ~Value(); // Use deleteValue() to delete a generic Value.
214 Value(const Value
&) = delete;
215 Value
&operator=(const Value
&) = delete;
217 /// Delete a pointer to a generic Value.
220 /// Support for debugging, callable in GDB: V->dump()
223 /// Implement operator<< on Value.
225 void print(raw_ostream
&O
, bool IsForDebug
= false) const;
226 void print(raw_ostream
&O
, ModuleSlotTracker
&MST
,
227 bool IsForDebug
= false) const;
230 /// Print the name of this Value out to the specified raw_ostream.
232 /// This is useful when you just want to print 'int %reg126', not the
233 /// instruction that generated it. If you specify a Module for context, then
234 /// even constanst get pretty-printed; for example, the type of a null
235 /// pointer is printed symbolically.
237 void printAsOperand(raw_ostream
&O
, bool PrintType
= true,
238 const Module
*M
= nullptr) const;
239 void printAsOperand(raw_ostream
&O
, bool PrintType
,
240 ModuleSlotTracker
&MST
) const;
243 /// All values are typed, get the type of this value.
244 Type
*getType() const { return VTy
; }
246 /// All values hold a context through their type.
247 LLVMContext
&getContext() const;
249 // All values can potentially be named.
250 bool hasName() const { return HasName
; }
251 ValueName
*getValueName() const;
252 void setValueName(ValueName
*VN
);
255 void destroyValueName();
256 enum class ReplaceMetadataUses
{ No
, Yes
};
257 void doRAUW(Value
*New
, ReplaceMetadataUses
);
258 void setNameImpl(const Twine
&Name
);
261 /// Return a constant reference to the value's name.
263 /// This guaranteed to return the same reference as long as the value is not
264 /// modified. If the value has a name, this does a hashtable lookup, so it's
266 StringRef
getName() const;
268 /// Change the name of the value.
270 /// Choose a new unique name if the provided name is taken.
272 /// \param Name The new name; or "" if the value's name should be removed.
273 void setName(const Twine
&Name
);
275 /// Transfer the name from V to this value.
277 /// After taking V's name, sets V's name to empty.
279 /// \note It is an error to call V->takeName(V).
280 void takeName(Value
*V
);
282 /// Change all uses of this to point to a new Value.
284 /// Go through the uses list for this definition and make each use point to
285 /// "V" instead of "this". After this completes, 'this's use list is
286 /// guaranteed to be empty.
287 void replaceAllUsesWith(Value
*V
);
289 /// Change non-metadata uses of this to point to a new Value.
291 /// Go through the uses list for this definition and make each use point to
292 /// "V" instead of "this". This function skips metadata entries in the list.
293 void replaceNonMetadataUsesWith(Value
*V
);
295 /// replaceUsesOutsideBlock - Go through the uses list for this definition and
296 /// make each use point to "V" instead of "this" when the use is outside the
297 /// block. 'This's use list is expected to have at least one element.
298 /// Unlike replaceAllUsesWith this function does not support basic block
299 /// values or constant users.
300 void replaceUsesOutsideBlock(Value
*V
, BasicBlock
*BB
);
302 //----------------------------------------------------------------------
303 // Methods for handling the chain of uses of this Value.
305 // Materializing a function can introduce new uses, so these methods come in
307 // The methods that start with materialized_ check the uses that are
308 // currently known given which functions are materialized. Be very careful
309 // when using them since you might not get all uses.
310 // The methods that don't start with materialized_ assert that modules is
311 // fully materialized.
312 void assertModuleIsMaterializedImpl() const;
313 // This indirection exists so we can keep assertModuleIsMaterializedImpl()
314 // around in release builds of Value.cpp to be linked with other code built
315 // in debug mode. But this avoids calling it in any of the release built code.
316 void assertModuleIsMaterialized() const {
318 assertModuleIsMaterializedImpl();
322 bool use_empty() const {
323 assertModuleIsMaterialized();
324 return UseList
== nullptr;
327 bool materialized_use_empty() const {
328 return UseList
== nullptr;
331 using use_iterator
= use_iterator_impl
<Use
>;
332 using const_use_iterator
= use_iterator_impl
<const Use
>;
334 use_iterator
materialized_use_begin() { return use_iterator(UseList
); }
335 const_use_iterator
materialized_use_begin() const {
336 return const_use_iterator(UseList
);
338 use_iterator
use_begin() {
339 assertModuleIsMaterialized();
340 return materialized_use_begin();
342 const_use_iterator
use_begin() const {
343 assertModuleIsMaterialized();
344 return materialized_use_begin();
346 use_iterator
use_end() { return use_iterator(); }
347 const_use_iterator
use_end() const { return const_use_iterator(); }
348 iterator_range
<use_iterator
> materialized_uses() {
349 return make_range(materialized_use_begin(), use_end());
351 iterator_range
<const_use_iterator
> materialized_uses() const {
352 return make_range(materialized_use_begin(), use_end());
354 iterator_range
<use_iterator
> uses() {
355 assertModuleIsMaterialized();
356 return materialized_uses();
358 iterator_range
<const_use_iterator
> uses() const {
359 assertModuleIsMaterialized();
360 return materialized_uses();
363 bool user_empty() const {
364 assertModuleIsMaterialized();
365 return UseList
== nullptr;
368 using user_iterator
= user_iterator_impl
<User
>;
369 using const_user_iterator
= user_iterator_impl
<const User
>;
371 user_iterator
materialized_user_begin() { return user_iterator(UseList
); }
372 const_user_iterator
materialized_user_begin() const {
373 return const_user_iterator(UseList
);
375 user_iterator
user_begin() {
376 assertModuleIsMaterialized();
377 return materialized_user_begin();
379 const_user_iterator
user_begin() const {
380 assertModuleIsMaterialized();
381 return materialized_user_begin();
383 user_iterator
user_end() { return user_iterator(); }
384 const_user_iterator
user_end() const { return const_user_iterator(); }
386 assertModuleIsMaterialized();
387 return *materialized_user_begin();
389 const User
*user_back() const {
390 assertModuleIsMaterialized();
391 return *materialized_user_begin();
393 iterator_range
<user_iterator
> materialized_users() {
394 return make_range(materialized_user_begin(), user_end());
396 iterator_range
<const_user_iterator
> materialized_users() const {
397 return make_range(materialized_user_begin(), user_end());
399 iterator_range
<user_iterator
> users() {
400 assertModuleIsMaterialized();
401 return materialized_users();
403 iterator_range
<const_user_iterator
> users() const {
404 assertModuleIsMaterialized();
405 return materialized_users();
408 /// Return true if there is exactly one user of this value.
410 /// This is specialized because it is a common request and does not require
411 /// traversing the whole use list.
412 bool hasOneUse() const {
413 const_use_iterator I
= use_begin(), E
= use_end();
414 if (I
== E
) return false;
418 /// Return true if this Value has exactly N users.
419 bool hasNUses(unsigned N
) const;
421 /// Return true if this value has N users or more.
423 /// This is logically equivalent to getNumUses() >= N.
424 bool hasNUsesOrMore(unsigned N
) const;
426 /// Check if this value is used in the specified basic block.
427 bool isUsedInBasicBlock(const BasicBlock
*BB
) const;
429 /// This method computes the number of uses of this Value.
431 /// This is a linear time operation. Use hasOneUse, hasNUses, or
432 /// hasNUsesOrMore to check for specific values.
433 unsigned getNumUses() const;
435 /// This method should only be used by the Use class.
436 void addUse(Use
&U
) { U
.addToList(&UseList
); }
438 /// Concrete subclass of this.
440 /// An enumeration for keeping track of the concrete subclass of Value that
441 /// is actually instantiated. Values of this enumeration are kept in the
442 /// Value classes SubclassID field. They are used for concrete type
445 #define HANDLE_VALUE(Name) Name##Val,
446 #include "llvm/IR/Value.def"
449 #define HANDLE_CONSTANT_MARKER(Marker, Constant) Marker = Constant##Val,
450 #include "llvm/IR/Value.def"
453 /// Return an ID for the concrete type of this object.
455 /// This is used to implement the classof checks. This should not be used
456 /// for any other purpose, as the values may change as LLVM evolves. Also,
457 /// note that for instructions, the Instruction's opcode is added to
458 /// InstructionVal. So this means three things:
459 /// # there is no value with code InstructionVal (no opcode==0).
460 /// # there are more possible values for the value type than in ValueTy enum.
461 /// # the InstructionVal enumerator must be the highest valued enumerator in
462 /// the ValueTy enum.
463 unsigned getValueID() const {
467 /// Return the raw optional flags value contained in this value.
469 /// This should only be used when testing two Values for equivalence.
470 unsigned getRawSubclassOptionalData() const {
471 return SubclassOptionalData
;
474 /// Clear the optional flags contained in this value.
475 void clearSubclassOptionalData() {
476 SubclassOptionalData
= 0;
479 /// Check the optional flags for equality.
480 bool hasSameSubclassOptionalData(const Value
*V
) const {
481 return SubclassOptionalData
== V
->SubclassOptionalData
;
484 /// Return true if there is a value handle associated with this value.
485 bool hasValueHandle() const { return HasValueHandle
; }
487 /// Return true if there is metadata referencing this value.
488 bool isUsedByMetadata() const { return IsUsedByMD
; }
490 /// Return true if this value is a swifterror value.
492 /// swifterror values can be either a function argument or an alloca with a
493 /// swifterror attribute.
494 bool isSwiftError() const;
496 /// Strip off pointer casts, all-zero GEPs, address space casts, and aliases.
498 /// Returns the original uncasted value. If this is called on a non-pointer
499 /// value, it returns 'this'.
500 const Value
*stripPointerCasts() const;
501 Value
*stripPointerCasts() {
502 return const_cast<Value
*>(
503 static_cast<const Value
*>(this)->stripPointerCasts());
506 /// Strip off pointer casts, all-zero GEPs, address space casts, and aliases
507 /// but ensures the representation of the result stays the same.
509 /// Returns the original uncasted value with the same representation. If this
510 /// is called on a non-pointer value, it returns 'this'.
511 const Value
*stripPointerCastsSameRepresentation() const;
512 Value
*stripPointerCastsSameRepresentation() {
513 return const_cast<Value
*>(static_cast<const Value
*>(this)
514 ->stripPointerCastsSameRepresentation());
517 /// Strip off pointer casts, all-zero GEPs, aliases and invariant group
520 /// Returns the original uncasted value. If this is called on a non-pointer
521 /// value, it returns 'this'. This function should be used only in
523 const Value
*stripPointerCastsAndInvariantGroups() const;
524 Value
*stripPointerCastsAndInvariantGroups() {
525 return const_cast<Value
*>(
526 static_cast<const Value
*>(this)->stripPointerCastsAndInvariantGroups());
529 /// Strip off pointer casts and all-zero GEPs.
531 /// Returns the original uncasted value. If this is called on a non-pointer
532 /// value, it returns 'this'.
533 const Value
*stripPointerCastsNoFollowAliases() const;
534 Value
*stripPointerCastsNoFollowAliases() {
535 return const_cast<Value
*>(
536 static_cast<const Value
*>(this)->stripPointerCastsNoFollowAliases());
539 /// Strip off pointer casts and all-constant inbounds GEPs.
541 /// Returns the original pointer value. If this is called on a non-pointer
542 /// value, it returns 'this'.
543 const Value
*stripInBoundsConstantOffsets() const;
544 Value
*stripInBoundsConstantOffsets() {
545 return const_cast<Value
*>(
546 static_cast<const Value
*>(this)->stripInBoundsConstantOffsets());
549 /// Accumulate the constant offset this value has compared to a base pointer.
550 /// Only 'getelementptr' instructions (GEPs) with constant indices are
551 /// accumulated but other instructions, e.g., casts, are stripped away as
552 /// well. The accumulated constant offset is added to \p Offset and the base
553 /// pointer is returned.
555 /// The APInt \p Offset has to have a bit-width equal to the IntPtr type for
556 /// the address space of 'this' pointer value, e.g., use
557 /// DataLayout::getIndexTypeSizeInBits(Ty).
559 /// If \p AllowNonInbounds is true, constant offsets in GEPs are stripped and
560 /// accumulated even if the GEP is not "inbounds".
562 /// If this is called on a non-pointer value, it returns 'this' and the
563 /// \p Offset is not modified.
565 /// Note that this function will never return a nullptr. It will also never
566 /// manipulate the \p Offset in a way that would not match the difference
567 /// between the underlying value and the returned one. Thus, if no constant
568 /// offset was found, the returned value is the underlying one and \p Offset
570 const Value
*stripAndAccumulateConstantOffsets(const DataLayout
&DL
,
572 bool AllowNonInbounds
) const;
573 Value
*stripAndAccumulateConstantOffsets(const DataLayout
&DL
, APInt
&Offset
,
574 bool AllowNonInbounds
) {
575 return const_cast<Value
*>(
576 static_cast<const Value
*>(this)->stripAndAccumulateConstantOffsets(
577 DL
, Offset
, AllowNonInbounds
));
580 /// This is a wrapper around stripAndAccumulateConstantOffsets with the
581 /// in-bounds requirement set to false.
582 const Value
*stripAndAccumulateInBoundsConstantOffsets(const DataLayout
&DL
,
583 APInt
&Offset
) const {
584 return stripAndAccumulateConstantOffsets(DL
, Offset
,
585 /* AllowNonInbounds */ false);
587 Value
*stripAndAccumulateInBoundsConstantOffsets(const DataLayout
&DL
,
589 return stripAndAccumulateConstantOffsets(DL
, Offset
,
590 /* AllowNonInbounds */ false);
593 /// Strip off pointer casts and inbounds GEPs.
595 /// Returns the original pointer value. If this is called on a non-pointer
596 /// value, it returns 'this'.
597 const Value
*stripInBoundsOffsets() const;
598 Value
*stripInBoundsOffsets() {
599 return const_cast<Value
*>(
600 static_cast<const Value
*>(this)->stripInBoundsOffsets());
603 /// Returns the number of bytes known to be dereferenceable for the
606 /// If CanBeNull is set by this function the pointer can either be null or be
607 /// dereferenceable up to the returned number of bytes.
608 uint64_t getPointerDereferenceableBytes(const DataLayout
&DL
,
609 bool &CanBeNull
) const;
611 /// Returns an alignment of the pointer value.
613 /// Returns an alignment which is either specified explicitly, e.g. via
614 /// align attribute of a function argument, or guaranteed by DataLayout.
615 unsigned getPointerAlignment(const DataLayout
&DL
) const;
617 /// Translate PHI node to its predecessor from the given basic block.
619 /// If this value is a PHI node with CurBB as its parent, return the value in
620 /// the PHI node corresponding to PredBB. If not, return ourself. This is
621 /// useful if you want to know the value something has in a predecessor
623 const Value
*DoPHITranslation(const BasicBlock
*CurBB
,
624 const BasicBlock
*PredBB
) const;
625 Value
*DoPHITranslation(const BasicBlock
*CurBB
, const BasicBlock
*PredBB
) {
626 return const_cast<Value
*>(
627 static_cast<const Value
*>(this)->DoPHITranslation(CurBB
, PredBB
));
630 /// The maximum alignment for instructions.
632 /// This is the greatest alignment value supported by load, store, and alloca
633 /// instructions, and global values.
634 static const unsigned MaxAlignmentExponent
= 29;
635 static const unsigned MaximumAlignment
= 1u << MaxAlignmentExponent
;
637 /// Mutate the type of this Value to be of the specified type.
639 /// Note that this is an extremely dangerous operation which can create
640 /// completely invalid IR very easily. It is strongly recommended that you
641 /// recreate IR objects with the right types instead of mutating them in
643 void mutateType(Type
*Ty
) {
647 /// Sort the use-list.
649 /// Sorts the Value's use-list by Cmp using a stable mergesort. Cmp is
650 /// expected to compare two \a Use references.
651 template <class Compare
> void sortUseList(Compare Cmp
);
653 /// Reverse the use-list.
654 void reverseUseList();
657 /// Merge two lists together.
659 /// Merges \c L and \c R using \c Cmp. To enable stable sorts, always pushes
660 /// "equal" items from L before items from R.
662 /// \return the first element in the list.
664 /// \note Completely ignores \a Use::Prev (doesn't read, doesn't update).
665 template <class Compare
>
666 static Use
*mergeUseLists(Use
*L
, Use
*R
, Compare Cmp
) {
668 Use
**Next
= &Merged
;
694 unsigned short getSubclassDataFromValue() const { return SubclassData
; }
695 void setValueSubclassData(unsigned short D
) { SubclassData
= D
; }
698 struct ValueDeleter
{ void operator()(Value
*V
) { V
->deleteValue(); } };
700 /// Use this instead of std::unique_ptr<Value> or std::unique_ptr<Instruction>.
701 /// Those don't work because Value and Instruction's destructors are protected,
702 /// aren't virtual, and won't destroy the complete object.
703 using unique_value
= std::unique_ptr
<Value
, ValueDeleter
>;
705 inline raw_ostream
&operator<<(raw_ostream
&OS
, const Value
&V
) {
710 void Use::set(Value
*V
) {
711 if (Val
) removeFromList();
713 if (V
) V
->addUse(*this);
716 Value
*Use::operator=(Value
*RHS
) {
721 const Use
&Use::operator=(const Use
&RHS
) {
726 template <class Compare
> void Value::sortUseList(Compare Cmp
) {
727 if (!UseList
|| !UseList
->Next
)
728 // No need to sort 0 or 1 uses.
731 // Note: this function completely ignores Prev pointers until the end when
732 // they're fixed en masse.
734 // Create a binomial vector of sorted lists, visiting uses one at a time and
735 // merging lists as necessary.
736 const unsigned MaxSlots
= 32;
737 Use
*Slots
[MaxSlots
];
739 // Collect the first use, turning it into a single-item list.
740 Use
*Next
= UseList
->Next
;
741 UseList
->Next
= nullptr;
742 unsigned NumSlots
= 1;
745 // Collect all but the last use.
748 Next
= Current
->Next
;
750 // Turn Current into a single-item list.
751 Current
->Next
= nullptr;
753 // Save Current in the first available slot, merging on collisions.
755 for (I
= 0; I
< NumSlots
; ++I
) {
759 // Merge two lists, doubling the size of Current and emptying slot I.
761 // Since the uses in Slots[I] originally preceded those in Current, send
762 // Slots[I] in as the left parameter to maintain a stable sort.
763 Current
= mergeUseLists(Slots
[I
], Current
, Cmp
);
766 // Check if this is a new slot.
769 assert(NumSlots
<= MaxSlots
&& "Use list bigger than 2^32");
772 // Found an open slot.
776 // Merge all the lists together.
777 assert(Next
&& "Expected one more Use");
778 assert(!Next
->Next
&& "Expected only one Use");
780 for (unsigned I
= 0; I
< NumSlots
; ++I
)
782 // Since the uses in Slots[I] originally preceded those in UseList, send
783 // Slots[I] in as the left parameter to maintain a stable sort.
784 UseList
= mergeUseLists(Slots
[I
], UseList
, Cmp
);
786 // Fix the Prev pointers.
787 for (Use
*I
= UseList
, **Prev
= &UseList
; I
; I
= I
->Next
) {
793 // isa - Provide some specializations of isa so that we don't have to include
794 // the subtype header files to test to see if the value is a subclass...
796 template <> struct isa_impl
<Constant
, Value
> {
797 static inline bool doit(const Value
&Val
) {
798 static_assert(Value::ConstantFirstVal
== 0, "Val.getValueID() >= Value::ConstantFirstVal");
799 return Val
.getValueID() <= Value::ConstantLastVal
;
803 template <> struct isa_impl
<ConstantData
, Value
> {
804 static inline bool doit(const Value
&Val
) {
805 return Val
.getValueID() >= Value::ConstantDataFirstVal
&&
806 Val
.getValueID() <= Value::ConstantDataLastVal
;
810 template <> struct isa_impl
<ConstantAggregate
, Value
> {
811 static inline bool doit(const Value
&Val
) {
812 return Val
.getValueID() >= Value::ConstantAggregateFirstVal
&&
813 Val
.getValueID() <= Value::ConstantAggregateLastVal
;
817 template <> struct isa_impl
<Argument
, Value
> {
818 static inline bool doit (const Value
&Val
) {
819 return Val
.getValueID() == Value::ArgumentVal
;
823 template <> struct isa_impl
<InlineAsm
, Value
> {
824 static inline bool doit(const Value
&Val
) {
825 return Val
.getValueID() == Value::InlineAsmVal
;
829 template <> struct isa_impl
<Instruction
, Value
> {
830 static inline bool doit(const Value
&Val
) {
831 return Val
.getValueID() >= Value::InstructionVal
;
835 template <> struct isa_impl
<BasicBlock
, Value
> {
836 static inline bool doit(const Value
&Val
) {
837 return Val
.getValueID() == Value::BasicBlockVal
;
841 template <> struct isa_impl
<Function
, Value
> {
842 static inline bool doit(const Value
&Val
) {
843 return Val
.getValueID() == Value::FunctionVal
;
847 template <> struct isa_impl
<GlobalVariable
, Value
> {
848 static inline bool doit(const Value
&Val
) {
849 return Val
.getValueID() == Value::GlobalVariableVal
;
853 template <> struct isa_impl
<GlobalAlias
, Value
> {
854 static inline bool doit(const Value
&Val
) {
855 return Val
.getValueID() == Value::GlobalAliasVal
;
859 template <> struct isa_impl
<GlobalIFunc
, Value
> {
860 static inline bool doit(const Value
&Val
) {
861 return Val
.getValueID() == Value::GlobalIFuncVal
;
865 template <> struct isa_impl
<GlobalIndirectSymbol
, Value
> {
866 static inline bool doit(const Value
&Val
) {
867 return isa
<GlobalAlias
>(Val
) || isa
<GlobalIFunc
>(Val
);
871 template <> struct isa_impl
<GlobalValue
, Value
> {
872 static inline bool doit(const Value
&Val
) {
873 return isa
<GlobalObject
>(Val
) || isa
<GlobalIndirectSymbol
>(Val
);
877 template <> struct isa_impl
<GlobalObject
, Value
> {
878 static inline bool doit(const Value
&Val
) {
879 return isa
<GlobalVariable
>(Val
) || isa
<Function
>(Val
);
883 // Create wrappers for C Binding types (see CBindingWrapping.h).
884 DEFINE_ISA_CONVERSION_FUNCTIONS(Value
, LLVMValueRef
)
886 // Specialized opaque value conversions.
887 inline Value
**unwrap(LLVMValueRef
*Vals
) {
888 return reinterpret_cast<Value
**>(Vals
);
892 inline T
**unwrap(LLVMValueRef
*Vals
, unsigned Length
) {
894 for (LLVMValueRef
*I
= Vals
, *E
= Vals
+ Length
; I
!= E
; ++I
)
895 unwrap
<T
>(*I
); // For side effect of calling assert on invalid usage.
898 return reinterpret_cast<T
**>(Vals
);
901 inline LLVMValueRef
*wrap(const Value
**Vals
) {
902 return reinterpret_cast<LLVMValueRef
*>(const_cast<Value
**>(Vals
));
905 } // end namespace llvm
907 #endif // LLVM_IR_VALUE_H