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