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