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