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