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