1 //===-- llvm/Value.h - Definition of the Value class ------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the Value class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_IR_VALUE_H
15 #define LLVM_IR_VALUE_H
16
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"
21 #include "llvm-c/Types.h"
22 #include <cassert>
23 #include <iterator>
24
25 namespace llvm {
26
27 class APInt;
28 class Argument;
29 class BasicBlock;
30 class Constant;
31 class ConstantData;
32 class ConstantAggregate;
33 class DataLayout;
34 class Function;
35 class GlobalAlias;
36 class GlobalIFunc;
37 class GlobalIndirectSymbol;
38 class GlobalObject;
39 class GlobalValue;
40 class GlobalVariable;
41 class InlineAsm;
42 class Instruction;
43 class LLVMContext;
44 class Module;
45 class ModuleSlotTracker;
46 class raw_ostream;
47 class StringRef;
48 class Twine;
49 class Type;
50
51 template<typename ValueTy> class StringMapEntry;
52 typedef StringMapEntry<Value*> ValueName;
53
54 //===----------------------------------------------------------------------===//
55 // Value Class
56 //===----------------------------------------------------------------------===//
57
58 /// \brief LLVM Value Representation
59 ///
60 /// This is a very important LLVM class. It is the base class of all values
61 /// computed by a program that may be used as operands to other values. Value is
62 /// the super class of other important classes such as Instruction and Function.
63 /// All Values have a Type. Type is not a subclass of Value. Some values can
64 /// have a name and they belong to some Module. Setting the name on the Value
65 /// automatically updates the module's symbol table.
66 ///
67 /// Every value has a "use list" that keeps track of which other Values are
68 /// using this Value. A Value can also have an arbitrary number of ValueHandle
69 /// objects that watch it and listen to RAUW and Destroy events. See
70 /// llvm/IR/ValueHandle.h for details.
71 class Value {
72 Type *VTy;
73 Use *UseList;
74
75 friend class ValueAsMetadata; // Allow access to IsUsedByMD.
76 friend class ValueHandleBase;
77
78 const unsigned char SubclassID; // Subclass identifier (for isa/dyn_cast)
79 unsigned char HasValueHandle : 1; // Has a ValueHandle pointing to this?
80
81 protected:
82 /// \brief Hold subclass data that can be dropped.
83 ///
84 /// This member is similar to SubclassData, however it is for holding
85 /// information which may be used to aid optimization, but which may be
86 /// cleared to zero without affecting conservative interpretation.
87 unsigned char SubclassOptionalData : 7;
88
89 private:
90 /// \brief Hold arbitrary subclass data.
91 ///
92 /// This member is defined by this class, but is not used for anything.
93 /// Subclasses can use it to hold whatever state they find useful. This
94 /// field is initialized to zero by the ctor.
95 unsigned short SubclassData;
96
97 protected:
98 /// \brief The number of operands in the subclass.
99 ///
100 /// This member is defined by this class, but not used for anything.
101 /// Subclasses can use it to store their number of operands, if they have
102 /// any.
103 ///
104 /// This is stored here to save space in User on 64-bit hosts. Since most
105 /// instances of Value have operands, 32-bit hosts aren't significantly
106 /// affected.
107 ///
108 /// Note, this should *NOT* be used directly by any class other than User.
109 /// User uses this value to find the Use list.
110 enum : unsigned { NumUserOperandsBits = 28 };
111 unsigned NumUserOperands : NumUserOperandsBits;
112
113 // Use the same type as the bitfield above so that MSVC will pack them.
114 unsigned IsUsedByMD : 1;
115 unsigned HasName : 1;
116 unsigned HasHungOffUses : 1;
117 unsigned HasDescriptor : 1;
118
119 private:
120 template <typename UseT> // UseT == 'Use' or 'const Use'
121 class use_iterator_impl
122 : public std::iterator<std::forward_iterator_tag, UseT *> {
123 UseT *U;
use_iterator_impl(UseT * u)124 explicit use_iterator_impl(UseT *u) : U(u) {}
125 friend class Value;
126
127 public:
use_iterator_impl()128 use_iterator_impl() : U() {}
129
130 bool operator==(const use_iterator_impl &x) const { return U == x.U; }
131 bool operator!=(const use_iterator_impl &x) const { return !operator==(x); }
132
133 use_iterator_impl &operator++() { // Preincrement
134 assert(U && "Cannot increment end iterator!");
135 U = U->getNext();
136 return *this;
137 }
138
139 use_iterator_impl operator++(int) { // Postincrement
140 auto tmp = *this;
141 ++*this;
142 return tmp;
143 }
144
145 UseT &operator*() const {
146 assert(U && "Cannot dereference end iterator!");
147 return *U;
148 }
149
150 UseT *operator->() const { return &operator*(); }
151
152 operator use_iterator_impl<const UseT>() const {
153 return use_iterator_impl<const UseT>(U);
154 }
155 };
156
157 template <typename UserTy> // UserTy == 'User' or 'const User'
158 class user_iterator_impl
159 : public std::iterator<std::forward_iterator_tag, UserTy *> {
160 use_iterator_impl<Use> UI;
user_iterator_impl(Use * U)161 explicit user_iterator_impl(Use *U) : UI(U) {}
162 friend class Value;
163
164 public:
165 user_iterator_impl() = default;
166
167 bool operator==(const user_iterator_impl &x) const { return UI == x.UI; }
168 bool operator!=(const user_iterator_impl &x) const { return !operator==(x); }
169
170 /// \brief Returns true if this iterator is equal to user_end() on the value.
atEnd()171 bool atEnd() const { return *this == user_iterator_impl(); }
172
173 user_iterator_impl &operator++() { // Preincrement
174 ++UI;
175 return *this;
176 }
177
178 user_iterator_impl operator++(int) { // Postincrement
179 auto tmp = *this;
180 ++*this;
181 return tmp;
182 }
183
184 // Retrieve a pointer to the current User.
185 UserTy *operator*() const {
186 return UI->getUser();
187 }
188
189 UserTy *operator->() const { return operator*(); }
190
191 operator user_iterator_impl<const UserTy>() const {
192 return user_iterator_impl<const UserTy>(*UI);
193 }
194
getUse()195 Use &getUse() const { return *UI; }
196 };
197
198 protected:
199 Value(Type *Ty, unsigned scid);
200
201 public:
202 Value(const Value &) = delete;
203 void operator=(const Value &) = delete;
204 virtual ~Value();
205
206 /// \brief Support for debugging, callable in GDB: V->dump()
207 void dump() const;
208
209 /// \brief Implement operator<< on Value.
210 /// @{
211 void print(raw_ostream &O, bool IsForDebug = false) const;
212 void print(raw_ostream &O, ModuleSlotTracker &MST,
213 bool IsForDebug = false) const;
214 /// @}
215
216 /// \brief Print the name of this Value out to the specified raw_ostream.
217 ///
218 /// This is useful when you just want to print 'int %reg126', not the
219 /// instruction that generated it. If you specify a Module for context, then
220 /// even constanst get pretty-printed; for example, the type of a null
221 /// pointer is printed symbolically.
222 /// @{
223 void printAsOperand(raw_ostream &O, bool PrintType = true,
224 const Module *M = nullptr) const;
225 void printAsOperand(raw_ostream &O, bool PrintType,
226 ModuleSlotTracker &MST) const;
227 /// @}
228
229 /// \brief All values are typed, get the type of this value.
getType()230 Type *getType() const { return VTy; }
231
232 /// \brief All values hold a context through their type.
233 LLVMContext &getContext() const;
234
235 // \brief All values can potentially be named.
hasName()236 bool hasName() const { return HasName; }
237 ValueName *getValueName() const;
238 void setValueName(ValueName *VN);
239
240 private:
241 void destroyValueName();
242 void doRAUW(Value *New, bool NoMetadata);
243 void setNameImpl(const Twine &Name);
244
245 public:
246 /// \brief Return a constant reference to the value's name.
247 ///
248 /// This is cheap and guaranteed to return the same reference as long as the
249 /// value is not modified.
250 StringRef getName() const;
251
252 /// \brief Change the name of the value.
253 ///
254 /// Choose a new unique name if the provided name is taken.
255 ///
256 /// \param Name The new name; or "" if the value's name should be removed.
257 void setName(const Twine &Name);
258
259 /// \brief Transfer the name from V to this value.
260 ///
261 /// After taking V's name, sets V's name to empty.
262 ///
263 /// \note It is an error to call V->takeName(V).
264 void takeName(Value *V);
265
266 /// \brief Change all uses of this to point to a new Value.
267 ///
268 /// Go through the uses list for this definition and make each use point to
269 /// "V" instead of "this". After this completes, 'this's use list is
270 /// guaranteed to be empty.
271 void replaceAllUsesWith(Value *V);
272
273 /// \brief Change non-metadata uses of this to point to a new Value.
274 ///
275 /// Go through the uses list for this definition and make each use point to
276 /// "V" instead of "this". This function skips metadata entries in the list.
277 void replaceNonMetadataUsesWith(Value *V);
278
279 /// replaceUsesOutsideBlock - Go through the uses list for this definition and
280 /// make each use point to "V" instead of "this" when the use is outside the
281 /// block. 'This's use list is expected to have at least one element.
282 /// Unlike replaceAllUsesWith this function does not support basic block
283 /// values or constant users.
284 void replaceUsesOutsideBlock(Value *V, BasicBlock *BB);
285
286 //----------------------------------------------------------------------
287 // Methods for handling the chain of uses of this Value.
288 //
289 // Materializing a function can introduce new uses, so these methods come in
290 // two variants:
291 // The methods that start with materialized_ check the uses that are
292 // currently known given which functions are materialized. Be very careful
293 // when using them since you might not get all uses.
294 // The methods that don't start with materialized_ assert that modules is
295 // fully materialized.
296 void assertModuleIsMaterialized() const;
297
use_empty()298 bool use_empty() const {
299 assertModuleIsMaterialized();
300 return UseList == nullptr;
301 }
302
303 typedef use_iterator_impl<Use> use_iterator;
304 typedef use_iterator_impl<const Use> const_use_iterator;
materialized_use_begin()305 use_iterator materialized_use_begin() { return use_iterator(UseList); }
materialized_use_begin()306 const_use_iterator materialized_use_begin() const {
307 return const_use_iterator(UseList);
308 }
use_begin()309 use_iterator use_begin() {
310 assertModuleIsMaterialized();
311 return materialized_use_begin();
312 }
use_begin()313 const_use_iterator use_begin() const {
314 assertModuleIsMaterialized();
315 return materialized_use_begin();
316 }
use_end()317 use_iterator use_end() { return use_iterator(); }
use_end()318 const_use_iterator use_end() const { return const_use_iterator(); }
materialized_uses()319 iterator_range<use_iterator> materialized_uses() {
320 return make_range(materialized_use_begin(), use_end());
321 }
materialized_uses()322 iterator_range<const_use_iterator> materialized_uses() const {
323 return make_range(materialized_use_begin(), use_end());
324 }
uses()325 iterator_range<use_iterator> uses() {
326 assertModuleIsMaterialized();
327 return materialized_uses();
328 }
uses()329 iterator_range<const_use_iterator> uses() const {
330 assertModuleIsMaterialized();
331 return materialized_uses();
332 }
333
user_empty()334 bool user_empty() const {
335 assertModuleIsMaterialized();
336 return UseList == nullptr;
337 }
338
339 typedef user_iterator_impl<User> user_iterator;
340 typedef user_iterator_impl<const User> const_user_iterator;
materialized_user_begin()341 user_iterator materialized_user_begin() { return user_iterator(UseList); }
materialized_user_begin()342 const_user_iterator materialized_user_begin() const {
343 return const_user_iterator(UseList);
344 }
user_begin()345 user_iterator user_begin() {
346 assertModuleIsMaterialized();
347 return materialized_user_begin();
348 }
user_begin()349 const_user_iterator user_begin() const {
350 assertModuleIsMaterialized();
351 return materialized_user_begin();
352 }
user_end()353 user_iterator user_end() { return user_iterator(); }
user_end()354 const_user_iterator user_end() const { return const_user_iterator(); }
user_back()355 User *user_back() {
356 assertModuleIsMaterialized();
357 return *materialized_user_begin();
358 }
user_back()359 const User *user_back() const {
360 assertModuleIsMaterialized();
361 return *materialized_user_begin();
362 }
materialized_users()363 iterator_range<user_iterator> materialized_users() {
364 return make_range(materialized_user_begin(), user_end());
365 }
materialized_users()366 iterator_range<const_user_iterator> materialized_users() const {
367 return make_range(materialized_user_begin(), user_end());
368 }
users()369 iterator_range<user_iterator> users() {
370 assertModuleIsMaterialized();
371 return materialized_users();
372 }
users()373 iterator_range<const_user_iterator> users() const {
374 assertModuleIsMaterialized();
375 return materialized_users();
376 }
377
378 /// \brief Return true if there is exactly one user of this value.
379 ///
380 /// This is specialized because it is a common request and does not require
381 /// traversing the whole use list.
hasOneUse()382 bool hasOneUse() const {
383 const_use_iterator I = use_begin(), E = use_end();
384 if (I == E) return false;
385 return ++I == E;
386 }
387
388 /// \brief Return true if this Value has exactly N users.
389 bool hasNUses(unsigned N) const;
390
391 /// \brief Return true if this value has N users or more.
392 ///
393 /// This is logically equivalent to getNumUses() >= N.
394 bool hasNUsesOrMore(unsigned N) const;
395
396 /// \brief Check if this value is used in the specified basic block.
397 bool isUsedInBasicBlock(const BasicBlock *BB) const;
398
399 /// \brief This method computes the number of uses of this Value.
400 ///
401 /// This is a linear time operation. Use hasOneUse, hasNUses, or
402 /// hasNUsesOrMore to check for specific values.
403 unsigned getNumUses() const;
404
405 /// \brief This method should only be used by the Use class.
addUse(Use & U)406 void addUse(Use &U) { U.addToList(&UseList); }
407
408 /// \brief Concrete subclass of this.
409 ///
410 /// An enumeration for keeping track of the concrete subclass of Value that
411 /// is actually instantiated. Values of this enumeration are kept in the
412 /// Value classes SubclassID field. They are used for concrete type
413 /// identification.
414 enum ValueTy {
415 #define HANDLE_VALUE(Name) Name##Val,
416 #include "llvm/IR/Value.def"
417
418 // Markers:
419 #define HANDLE_CONSTANT_MARKER(Marker, Constant) Marker = Constant##Val,
420 #include "llvm/IR/Value.def"
421 };
422
423 /// \brief Return an ID for the concrete type of this object.
424 ///
425 /// This is used to implement the classof checks. This should not be used
426 /// for any other purpose, as the values may change as LLVM evolves. Also,
427 /// note that for instructions, the Instruction's opcode is added to
428 /// InstructionVal. So this means three things:
429 /// # there is no value with code InstructionVal (no opcode==0).
430 /// # there are more possible values for the value type than in ValueTy enum.
431 /// # the InstructionVal enumerator must be the highest valued enumerator in
432 /// the ValueTy enum.
getValueID()433 unsigned getValueID() const {
434 return SubclassID;
435 }
436
437 /// \brief Return the raw optional flags value contained in this value.
438 ///
439 /// This should only be used when testing two Values for equivalence.
getRawSubclassOptionalData()440 unsigned getRawSubclassOptionalData() const {
441 return SubclassOptionalData;
442 }
443
444 /// \brief Clear the optional flags contained in this value.
clearSubclassOptionalData()445 void clearSubclassOptionalData() {
446 SubclassOptionalData = 0;
447 }
448
449 /// \brief Check the optional flags for equality.
hasSameSubclassOptionalData(const Value * V)450 bool hasSameSubclassOptionalData(const Value *V) const {
451 return SubclassOptionalData == V->SubclassOptionalData;
452 }
453
454 /// \brief Return true if there is a value handle associated with this value.
hasValueHandle()455 bool hasValueHandle() const { return HasValueHandle; }
456
457 /// \brief Return true if there is metadata referencing this value.
isUsedByMetadata()458 bool isUsedByMetadata() const { return IsUsedByMD; }
459
460 /// \brief Return true if this value is a swifterror value.
461 ///
462 /// swifterror values can be either a function argument or an alloca with a
463 /// swifterror attribute.
464 bool isSwiftError() const;
465
466 /// \brief Strip off pointer casts, all-zero GEPs, and aliases.
467 ///
468 /// Returns the original uncasted value. If this is called on a non-pointer
469 /// value, it returns 'this'.
470 Value *stripPointerCasts();
stripPointerCasts()471 const Value *stripPointerCasts() const {
472 return const_cast<Value*>(this)->stripPointerCasts();
473 }
474
475 /// \brief Strip off pointer casts and all-zero GEPs.
476 ///
477 /// Returns the original uncasted value. If this is called on a non-pointer
478 /// value, it returns 'this'.
479 Value *stripPointerCastsNoFollowAliases();
stripPointerCastsNoFollowAliases()480 const Value *stripPointerCastsNoFollowAliases() const {
481 return const_cast<Value*>(this)->stripPointerCastsNoFollowAliases();
482 }
483
484 /// \brief Strip off pointer casts and all-constant inbounds GEPs.
485 ///
486 /// Returns the original pointer value. If this is called on a non-pointer
487 /// value, it returns 'this'.
488 Value *stripInBoundsConstantOffsets();
stripInBoundsConstantOffsets()489 const Value *stripInBoundsConstantOffsets() const {
490 return const_cast<Value*>(this)->stripInBoundsConstantOffsets();
491 }
492
493 /// \brief Accumulate offsets from \a stripInBoundsConstantOffsets().
494 ///
495 /// Stores the resulting constant offset stripped into the APInt provided.
496 /// The provided APInt will be extended or truncated as needed to be the
497 /// correct bitwidth for an offset of this pointer type.
498 ///
499 /// If this is called on a non-pointer value, it returns 'this'.
500 Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
501 APInt &Offset);
stripAndAccumulateInBoundsConstantOffsets(const DataLayout & DL,APInt & Offset)502 const Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
503 APInt &Offset) const {
504 return const_cast<Value *>(this)
505 ->stripAndAccumulateInBoundsConstantOffsets(DL, Offset);
506 }
507
508 /// \brief Strip off pointer casts and inbounds GEPs.
509 ///
510 /// Returns the original pointer value. If this is called on a non-pointer
511 /// value, it returns 'this'.
512 Value *stripInBoundsOffsets();
stripInBoundsOffsets()513 const Value *stripInBoundsOffsets() const {
514 return const_cast<Value*>(this)->stripInBoundsOffsets();
515 }
516
517 /// \brief Returns the number of bytes known to be dereferenceable for the
518 /// pointer value.
519 ///
520 /// If CanBeNull is set by this function the pointer can either be null or be
521 /// dereferenceable up to the returned number of bytes.
522 unsigned getPointerDereferenceableBytes(const DataLayout &DL,
523 bool &CanBeNull) const;
524
525 /// \brief Returns an alignment of the pointer value.
526 ///
527 /// Returns an alignment which is either specified explicitly, e.g. via
528 /// align attribute of a function argument, or guaranteed by DataLayout.
529 unsigned getPointerAlignment(const DataLayout &DL) const;
530
531 /// \brief Translate PHI node to its predecessor from the given basic block.
532 ///
533 /// If this value is a PHI node with CurBB as its parent, return the value in
534 /// the PHI node corresponding to PredBB. If not, return ourself. This is
535 /// useful if you want to know the value something has in a predecessor
536 /// block.
537 Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB);
538
DoPHITranslation(const BasicBlock * CurBB,const BasicBlock * PredBB)539 const Value *DoPHITranslation(const BasicBlock *CurBB,
540 const BasicBlock *PredBB) const{
541 return const_cast<Value*>(this)->DoPHITranslation(CurBB, PredBB);
542 }
543
544 /// \brief The maximum alignment for instructions.
545 ///
546 /// This is the greatest alignment value supported by load, store, and alloca
547 /// instructions, and global values.
548 static const unsigned MaxAlignmentExponent = 29;
549 static const unsigned MaximumAlignment = 1u << MaxAlignmentExponent;
550
551 /// \brief Mutate the type of this Value to be of the specified type.
552 ///
553 /// Note that this is an extremely dangerous operation which can create
554 /// completely invalid IR very easily. It is strongly recommended that you
555 /// recreate IR objects with the right types instead of mutating them in
556 /// place.
mutateType(Type * Ty)557 void mutateType(Type *Ty) {
558 VTy = Ty;
559 }
560
561 /// \brief Sort the use-list.
562 ///
563 /// Sorts the Value's use-list by Cmp using a stable mergesort. Cmp is
564 /// expected to compare two \a Use references.
565 template <class Compare> void sortUseList(Compare Cmp);
566
567 /// \brief Reverse the use-list.
568 void reverseUseList();
569
570 private:
571 /// \brief Merge two lists together.
572 ///
573 /// Merges \c L and \c R using \c Cmp. To enable stable sorts, always pushes
574 /// "equal" items from L before items from R.
575 ///
576 /// \return the first element in the list.
577 ///
578 /// \note Completely ignores \a Use::Prev (doesn't read, doesn't update).
579 template <class Compare>
mergeUseLists(Use * L,Use * R,Compare Cmp)580 static Use *mergeUseLists(Use *L, Use *R, Compare Cmp) {
581 Use *Merged;
582 Use **Next = &Merged;
583
584 for (;;) {
585 if (!L) {
586 *Next = R;
587 break;
588 }
589 if (!R) {
590 *Next = L;
591 break;
592 }
593 if (Cmp(*R, *L)) {
594 *Next = R;
595 Next = &R->Next;
596 R = R->Next;
597 } else {
598 *Next = L;
599 Next = &L->Next;
600 L = L->Next;
601 }
602 }
603
604 return Merged;
605 }
606
607 /// \brief Tail-recursive helper for \a mergeUseLists().
608 ///
609 /// \param[out] Next the first element in the list.
610 template <class Compare>
611 static void mergeUseListsImpl(Use *L, Use *R, Use **Next, Compare Cmp);
612
613 protected:
getSubclassDataFromValue()614 unsigned short getSubclassDataFromValue() const { return SubclassData; }
setValueSubclassData(unsigned short D)615 void setValueSubclassData(unsigned short D) { SubclassData = D; }
616 };
617
618 inline raw_ostream &operator<<(raw_ostream &OS, const Value &V) {
619 V.print(OS);
620 return OS;
621 }
622
set(Value * V)623 void Use::set(Value *V) {
624 if (Val) removeFromList();
625 Val = V;
626 if (V) V->addUse(*this);
627 }
628
629 Value *Use::operator=(Value *RHS) {
630 set(RHS);
631 return RHS;
632 }
633
634 const Use &Use::operator=(const Use &RHS) {
635 set(RHS.Val);
636 return *this;
637 }
638
sortUseList(Compare Cmp)639 template <class Compare> void Value::sortUseList(Compare Cmp) {
640 if (!UseList || !UseList->Next)
641 // No need to sort 0 or 1 uses.
642 return;
643
644 // Note: this function completely ignores Prev pointers until the end when
645 // they're fixed en masse.
646
647 // Create a binomial vector of sorted lists, visiting uses one at a time and
648 // merging lists as necessary.
649 const unsigned MaxSlots = 32;
650 Use *Slots[MaxSlots];
651
652 // Collect the first use, turning it into a single-item list.
653 Use *Next = UseList->Next;
654 UseList->Next = nullptr;
655 unsigned NumSlots = 1;
656 Slots[0] = UseList;
657
658 // Collect all but the last use.
659 while (Next->Next) {
660 Use *Current = Next;
661 Next = Current->Next;
662
663 // Turn Current into a single-item list.
664 Current->Next = nullptr;
665
666 // Save Current in the first available slot, merging on collisions.
667 unsigned I;
668 for (I = 0; I < NumSlots; ++I) {
669 if (!Slots[I])
670 break;
671
672 // Merge two lists, doubling the size of Current and emptying slot I.
673 //
674 // Since the uses in Slots[I] originally preceded those in Current, send
675 // Slots[I] in as the left parameter to maintain a stable sort.
676 Current = mergeUseLists(Slots[I], Current, Cmp);
677 Slots[I] = nullptr;
678 }
679 // Check if this is a new slot.
680 if (I == NumSlots) {
681 ++NumSlots;
682 assert(NumSlots <= MaxSlots && "Use list bigger than 2^32");
683 }
684
685 // Found an open slot.
686 Slots[I] = Current;
687 }
688
689 // Merge all the lists together.
690 assert(Next && "Expected one more Use");
691 assert(!Next->Next && "Expected only one Use");
692 UseList = Next;
693 for (unsigned I = 0; I < NumSlots; ++I)
694 if (Slots[I])
695 // Since the uses in Slots[I] originally preceded those in UseList, send
696 // Slots[I] in as the left parameter to maintain a stable sort.
697 UseList = mergeUseLists(Slots[I], UseList, Cmp);
698
699 // Fix the Prev pointers.
700 for (Use *I = UseList, **Prev = &UseList; I; I = I->Next) {
701 I->setPrev(Prev);
702 Prev = &I->Next;
703 }
704 }
705
706 // isa - Provide some specializations of isa so that we don't have to include
707 // the subtype header files to test to see if the value is a subclass...
708 //
709 template <> struct isa_impl<Constant, Value> {
710 static inline bool doit(const Value &Val) {
711 return Val.getValueID() >= Value::ConstantFirstVal &&
712 Val.getValueID() <= Value::ConstantLastVal;
713 }
714 };
715
716 template <> struct isa_impl<ConstantData, Value> {
717 static inline bool doit(const Value &Val) {
718 return Val.getValueID() >= Value::ConstantDataFirstVal &&
719 Val.getValueID() <= Value::ConstantDataLastVal;
720 }
721 };
722
723 template <> struct isa_impl<ConstantAggregate, Value> {
724 static inline bool doit(const Value &Val) {
725 return Val.getValueID() >= Value::ConstantAggregateFirstVal &&
726 Val.getValueID() <= Value::ConstantAggregateLastVal;
727 }
728 };
729
730 template <> struct isa_impl<Argument, Value> {
731 static inline bool doit (const Value &Val) {
732 return Val.getValueID() == Value::ArgumentVal;
733 }
734 };
735
736 template <> struct isa_impl<InlineAsm, Value> {
737 static inline bool doit(const Value &Val) {
738 return Val.getValueID() == Value::InlineAsmVal;
739 }
740 };
741
742 template <> struct isa_impl<Instruction, Value> {
743 static inline bool doit(const Value &Val) {
744 return Val.getValueID() >= Value::InstructionVal;
745 }
746 };
747
748 template <> struct isa_impl<BasicBlock, Value> {
749 static inline bool doit(const Value &Val) {
750 return Val.getValueID() == Value::BasicBlockVal;
751 }
752 };
753
754 template <> struct isa_impl<Function, Value> {
755 static inline bool doit(const Value &Val) {
756 return Val.getValueID() == Value::FunctionVal;
757 }
758 };
759
760 template <> struct isa_impl<GlobalVariable, Value> {
761 static inline bool doit(const Value &Val) {
762 return Val.getValueID() == Value::GlobalVariableVal;
763 }
764 };
765
766 template <> struct isa_impl<GlobalAlias, Value> {
767 static inline bool doit(const Value &Val) {
768 return Val.getValueID() == Value::GlobalAliasVal;
769 }
770 };
771
772 template <> struct isa_impl<GlobalIFunc, Value> {
773 static inline bool doit(const Value &Val) {
774 return Val.getValueID() == Value::GlobalIFuncVal;
775 }
776 };
777
778 template <> struct isa_impl<GlobalIndirectSymbol, Value> {
779 static inline bool doit(const Value &Val) {
780 return isa<GlobalAlias>(Val) || isa<GlobalIFunc>(Val);
781 }
782 };
783
784 template <> struct isa_impl<GlobalValue, Value> {
785 static inline bool doit(const Value &Val) {
786 return isa<GlobalObject>(Val) || isa<GlobalIndirectSymbol>(Val);
787 }
788 };
789
790 template <> struct isa_impl<GlobalObject, Value> {
791 static inline bool doit(const Value &Val) {
792 return isa<GlobalVariable>(Val) || isa<Function>(Val);
793 }
794 };
795
796 // Create wrappers for C Binding types (see CBindingWrapping.h).
797 DEFINE_ISA_CONVERSION_FUNCTIONS(Value, LLVMValueRef)
798
799 // Specialized opaque value conversions.
800 inline Value **unwrap(LLVMValueRef *Vals) {
801 return reinterpret_cast<Value**>(Vals);
802 }
803
804 template<typename T>
805 inline T **unwrap(LLVMValueRef *Vals, unsigned Length) {
806 #ifndef NDEBUG
807 for (LLVMValueRef *I = Vals, *E = Vals + Length; I != E; ++I)
808 unwrap<T>(*I); // For side effect of calling assert on invalid usage.
809 #endif
810 (void)Length;
811 return reinterpret_cast<T**>(Vals);
812 }
813
814 inline LLVMValueRef *wrap(const Value **Vals) {
815 return reinterpret_cast<LLVMValueRef*>(const_cast<Value**>(Vals));
816 }
817
818 } // end namespace llvm
819
820 #endif // LLVM_IR_VALUE_H
821