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