OSDN Git Service

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