LLVM  mainline
Value.h
Go to the documentation of this file.
00001 //===-- llvm/Value.h - Definition of the Value class ------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file declares the Value class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_IR_VALUE_H
00015 #define LLVM_IR_VALUE_H
00016 
00017 #include "llvm-c/Core.h"
00018 #include "llvm/ADT/iterator_range.h"
00019 #include "llvm/IR/Use.h"
00020 #include "llvm/Support/CBindingWrapping.h"
00021 #include "llvm/Support/Casting.h"
00022 #include "llvm/Support/Compiler.h"
00023 
00024 namespace llvm {
00025 
00026 class APInt;
00027 class Argument;
00028 class AssemblyAnnotationWriter;
00029 class BasicBlock;
00030 class Constant;
00031 class DataLayout;
00032 class Function;
00033 class GlobalAlias;
00034 class GlobalObject;
00035 class GlobalValue;
00036 class GlobalVariable;
00037 class InlineAsm;
00038 class Instruction;
00039 class LLVMContext;
00040 class Module;
00041 class StringRef;
00042 class Twine;
00043 class Type;
00044 class ValueHandleBase;
00045 class ValueSymbolTable;
00046 class raw_ostream;
00047 
00048 template<typename ValueTy> class StringMapEntry;
00049 typedef StringMapEntry<Value*> ValueName;
00050 
00051 //===----------------------------------------------------------------------===//
00052 //                                 Value Class
00053 //===----------------------------------------------------------------------===//
00054 
00055 /// \brief LLVM Value Representation
00056 ///
00057 /// This is a very important LLVM class. It is the base class of all values
00058 /// computed by a program that may be used as operands to other values. Value is
00059 /// the super class of other important classes such as Instruction and Function.
00060 /// All Values have a Type. Type is not a subclass of Value. Some values can
00061 /// have a name and they belong to some Module.  Setting the name on the Value
00062 /// automatically updates the module's symbol table.
00063 ///
00064 /// Every value has a "use list" that keeps track of which other Values are
00065 /// using this Value.  A Value can also have an arbitrary number of ValueHandle
00066 /// objects that watch it and listen to RAUW and Destroy events.  See
00067 /// llvm/IR/ValueHandle.h for details.
00068 class Value {
00069   Type *VTy;
00070   Use *UseList;
00071 
00072   friend class ValueAsMetadata; // Allow access to IsUsedByMD.
00073   friend class ValueHandleBase;
00074 
00075   const unsigned char SubclassID;   // Subclass identifier (for isa/dyn_cast)
00076   unsigned char HasValueHandle : 1; // Has a ValueHandle pointing to this?
00077 protected:
00078   /// \brief Hold subclass data that can be dropped.
00079   ///
00080   /// This member is similar to SubclassData, however it is for holding
00081   /// information which may be used to aid optimization, but which may be
00082   /// cleared to zero without affecting conservative interpretation.
00083   unsigned char SubclassOptionalData : 7;
00084 
00085 private:
00086   /// \brief Hold arbitrary subclass data.
00087   ///
00088   /// This member is defined by this class, but is not used for anything.
00089   /// Subclasses can use it to hold whatever state they find useful.  This
00090   /// field is initialized to zero by the ctor.
00091   unsigned short SubclassData;
00092 
00093 protected:
00094   /// \brief The number of operands in the subclass.
00095   ///
00096   /// This member is defined by this class, but not used for anything.
00097   /// Subclasses can use it to store their number of operands, if they have
00098   /// any.
00099   ///
00100   /// This is stored here to save space in User on 64-bit hosts.  Since most
00101   /// instances of Value have operands, 32-bit hosts aren't significantly
00102   /// affected.
00103   ///
00104   /// Note, this should *NOT* be used directly by any class other than User.
00105   /// User uses this value to find the Use list.
00106   static const unsigned NumUserOperandsBits = 29;
00107   unsigned NumUserOperands : 29;
00108 
00109   bool IsUsedByMD : 1;
00110   bool HasName : 1;
00111   bool HasHungOffUses : 1;
00112 
00113 private:
00114   template <typename UseT> // UseT == 'Use' or 'const Use'
00115   class use_iterator_impl
00116       : public std::iterator<std::forward_iterator_tag, UseT *> {
00117     UseT *U;
00118     explicit use_iterator_impl(UseT *u) : U(u) {}
00119     friend class Value;
00120 
00121   public:
00122     use_iterator_impl() : U() {}
00123 
00124     bool operator==(const use_iterator_impl &x) const { return U == x.U; }
00125     bool operator!=(const use_iterator_impl &x) const { return !operator==(x); }
00126 
00127     use_iterator_impl &operator++() { // Preincrement
00128       assert(U && "Cannot increment end iterator!");
00129       U = U->getNext();
00130       return *this;
00131     }
00132     use_iterator_impl operator++(int) { // Postincrement
00133       auto tmp = *this;
00134       ++*this;
00135       return tmp;
00136     }
00137 
00138     UseT &operator*() const {
00139       assert(U && "Cannot dereference end iterator!");
00140       return *U;
00141     }
00142 
00143     UseT *operator->() const { return &operator*(); }
00144 
00145     operator use_iterator_impl<const UseT>() const {
00146       return use_iterator_impl<const UseT>(U);
00147     }
00148   };
00149 
00150   template <typename UserTy> // UserTy == 'User' or 'const User'
00151   class user_iterator_impl
00152       : public std::iterator<std::forward_iterator_tag, UserTy *> {
00153     use_iterator_impl<Use> UI;
00154     explicit user_iterator_impl(Use *U) : UI(U) {}
00155     friend class Value;
00156 
00157   public:
00158     user_iterator_impl() {}
00159 
00160     bool operator==(const user_iterator_impl &x) const { return UI == x.UI; }
00161     bool operator!=(const user_iterator_impl &x) const { return !operator==(x); }
00162 
00163     /// \brief Returns true if this iterator is equal to user_end() on the value.
00164     bool atEnd() const { return *this == user_iterator_impl(); }
00165 
00166     user_iterator_impl &operator++() { // Preincrement
00167       ++UI;
00168       return *this;
00169     }
00170     user_iterator_impl operator++(int) { // Postincrement
00171       auto tmp = *this;
00172       ++*this;
00173       return tmp;
00174     }
00175 
00176     // Retrieve a pointer to the current User.
00177     UserTy *operator*() const {
00178       return UI->getUser();
00179     }
00180 
00181     UserTy *operator->() const { return operator*(); }
00182 
00183     operator user_iterator_impl<const UserTy>() const {
00184       return user_iterator_impl<const UserTy>(*UI);
00185     }
00186 
00187     Use &getUse() const { return *UI; }
00188   };
00189 
00190   void operator=(const Value &) = delete;
00191   Value(const Value &) = delete;
00192 
00193 protected:
00194   Value(Type *Ty, unsigned scid);
00195 public:
00196   virtual ~Value();
00197 
00198   /// \brief Support for debugging, callable in GDB: V->dump()
00199   void dump() const;
00200 
00201   /// \brief Implement operator<< on Value.
00202   void print(raw_ostream &O) const;
00203 
00204   /// \brief Print the name of this Value out to the specified raw_ostream.
00205   ///
00206   /// This is useful when you just want to print 'int %reg126', not the
00207   /// instruction that generated it. If you specify a Module for context, then
00208   /// even constanst get pretty-printed; for example, the type of a null
00209   /// pointer is printed symbolically.
00210   void printAsOperand(raw_ostream &O, bool PrintType = true,
00211                       const Module *M = nullptr) const;
00212 
00213   /// \brief All values are typed, get the type of this value.
00214   Type *getType() const { return VTy; }
00215 
00216   /// \brief All values hold a context through their type.
00217   LLVMContext &getContext() const;
00218 
00219   // \brief All values can potentially be named.
00220   bool hasName() const { return HasName; }
00221   ValueName *getValueName() const;
00222   void setValueName(ValueName *VN);
00223 
00224 private:
00225   void destroyValueName();
00226   void setNameImpl(const Twine &Name);
00227 
00228 public:
00229   /// \brief Return a constant reference to the value's name.
00230   ///
00231   /// This is cheap and guaranteed to return the same reference as long as the
00232   /// value is not modified.
00233   StringRef getName() const;
00234 
00235   /// \brief Change the name of the value.
00236   ///
00237   /// Choose a new unique name if the provided name is taken.
00238   ///
00239   /// \param Name The new name; or "" if the value's name should be removed.
00240   void setName(const Twine &Name);
00241 
00242 
00243   /// \brief Transfer the name from V to this value.
00244   ///
00245   /// After taking V's name, sets V's name to empty.
00246   ///
00247   /// \note It is an error to call V->takeName(V).
00248   void takeName(Value *V);
00249 
00250   /// \brief Change all uses of this to point to a new Value.
00251   ///
00252   /// Go through the uses list for this definition and make each use point to
00253   /// "V" instead of "this".  After this completes, 'this's use list is
00254   /// guaranteed to be empty.
00255   void replaceAllUsesWith(Value *V);
00256 
00257   /// replaceUsesOutsideBlock - Go through the uses list for this definition and
00258   /// make each use point to "V" instead of "this" when the use is outside the
00259   /// block. 'This's use list is expected to have at least one element.
00260   /// Unlike replaceAllUsesWith this function does not support basic block
00261   /// values or constant users.
00262   void replaceUsesOutsideBlock(Value *V, BasicBlock *BB);
00263 
00264   //----------------------------------------------------------------------
00265   // Methods for handling the chain of uses of this Value.
00266   //
00267   bool               use_empty() const { return UseList == nullptr; }
00268 
00269   typedef use_iterator_impl<Use>       use_iterator;
00270   typedef use_iterator_impl<const Use> const_use_iterator;
00271   use_iterator       use_begin()       { return use_iterator(UseList); }
00272   const_use_iterator use_begin() const { return const_use_iterator(UseList); }
00273   use_iterator       use_end()         { return use_iterator();   }
00274   const_use_iterator use_end()   const { return const_use_iterator();   }
00275   iterator_range<use_iterator> uses() {
00276     return iterator_range<use_iterator>(use_begin(), use_end());
00277   }
00278   iterator_range<const_use_iterator> uses() const {
00279     return iterator_range<const_use_iterator>(use_begin(), use_end());
00280   }
00281 
00282   bool               user_empty() const { return UseList == nullptr; }
00283 
00284   typedef user_iterator_impl<User>       user_iterator;
00285   typedef user_iterator_impl<const User> const_user_iterator;
00286   user_iterator       user_begin()       { return user_iterator(UseList); }
00287   const_user_iterator user_begin() const { return const_user_iterator(UseList); }
00288   user_iterator       user_end()         { return user_iterator();   }
00289   const_user_iterator user_end()   const { return const_user_iterator();   }
00290   User               *user_back()        { return *user_begin(); }
00291   const User         *user_back()  const { return *user_begin(); }
00292   iterator_range<user_iterator> users() {
00293     return iterator_range<user_iterator>(user_begin(), user_end());
00294   }
00295   iterator_range<const_user_iterator> users() const {
00296     return iterator_range<const_user_iterator>(user_begin(), user_end());
00297   }
00298 
00299   /// \brief Return true if there is exactly one user of this value.
00300   ///
00301   /// This is specialized because it is a common request and does not require
00302   /// traversing the whole use list.
00303   bool hasOneUse() const {
00304     const_use_iterator I = use_begin(), E = use_end();
00305     if (I == E) return false;
00306     return ++I == E;
00307   }
00308 
00309   /// \brief Return true if this Value has exactly N users.
00310   bool hasNUses(unsigned N) const;
00311 
00312   /// \brief Return true if this value has N users or more.
00313   ///
00314   /// This is logically equivalent to getNumUses() >= N.
00315   bool hasNUsesOrMore(unsigned N) const;
00316 
00317   /// \brief Check if this value is used in the specified basic block.
00318   bool isUsedInBasicBlock(const BasicBlock *BB) const;
00319 
00320   /// \brief This method computes the number of uses of this Value.
00321   ///
00322   /// This is a linear time operation.  Use hasOneUse, hasNUses, or
00323   /// hasNUsesOrMore to check for specific values.
00324   unsigned getNumUses() const;
00325 
00326   /// \brief This method should only be used by the Use class.
00327   void addUse(Use &U) { U.addToList(&UseList); }
00328 
00329   /// \brief Concrete subclass of this.
00330   ///
00331   /// An enumeration for keeping track of the concrete subclass of Value that
00332   /// is actually instantiated. Values of this enumeration are kept in the
00333   /// Value classes SubclassID field. They are used for concrete type
00334   /// identification.
00335   enum ValueTy {
00336 #define HANDLE_VALUE(Name) Name##Val,
00337 #include "llvm/IR/Value.def"
00338 
00339     // Markers:
00340 #define HANDLE_CONSTANT_MARKER(Marker, Constant) Marker = Constant##Val,
00341 #include "llvm/IR/Value.def"
00342   };
00343 
00344   /// \brief Return an ID for the concrete type of this object.
00345   ///
00346   /// This is used to implement the classof checks.  This should not be used
00347   /// for any other purpose, as the values may change as LLVM evolves.  Also,
00348   /// note that for instructions, the Instruction's opcode is added to
00349   /// InstructionVal. So this means three things:
00350   /// # there is no value with code InstructionVal (no opcode==0).
00351   /// # there are more possible values for the value type than in ValueTy enum.
00352   /// # the InstructionVal enumerator must be the highest valued enumerator in
00353   ///   the ValueTy enum.
00354   unsigned getValueID() const {
00355     return SubclassID;
00356   }
00357 
00358   /// \brief Return the raw optional flags value contained in this value.
00359   ///
00360   /// This should only be used when testing two Values for equivalence.
00361   unsigned getRawSubclassOptionalData() const {
00362     return SubclassOptionalData;
00363   }
00364 
00365   /// \brief Clear the optional flags contained in this value.
00366   void clearSubclassOptionalData() {
00367     SubclassOptionalData = 0;
00368   }
00369 
00370   /// \brief Check the optional flags for equality.
00371   bool hasSameSubclassOptionalData(const Value *V) const {
00372     return SubclassOptionalData == V->SubclassOptionalData;
00373   }
00374 
00375   /// \brief Clear any optional flags not set in the given Value.
00376   void intersectOptionalDataWith(const Value *V) {
00377     SubclassOptionalData &= V->SubclassOptionalData;
00378   }
00379 
00380   /// \brief Return true if there is a value handle associated with this value.
00381   bool hasValueHandle() const { return HasValueHandle; }
00382 
00383   /// \brief Return true if there is metadata referencing this value.
00384   bool isUsedByMetadata() const { return IsUsedByMD; }
00385 
00386   /// \brief Strip off pointer casts, all-zero GEPs, and aliases.
00387   ///
00388   /// Returns the original uncasted value.  If this is called on a non-pointer
00389   /// value, it returns 'this'.
00390   Value *stripPointerCasts();
00391   const Value *stripPointerCasts() const {
00392     return const_cast<Value*>(this)->stripPointerCasts();
00393   }
00394 
00395   /// \brief Strip off pointer casts and all-zero GEPs.
00396   ///
00397   /// Returns the original uncasted value.  If this is called on a non-pointer
00398   /// value, it returns 'this'.
00399   Value *stripPointerCastsNoFollowAliases();
00400   const Value *stripPointerCastsNoFollowAliases() const {
00401     return const_cast<Value*>(this)->stripPointerCastsNoFollowAliases();
00402   }
00403 
00404   /// \brief Strip off pointer casts and all-constant inbounds GEPs.
00405   ///
00406   /// Returns the original pointer value.  If this is called on a non-pointer
00407   /// value, it returns 'this'.
00408   Value *stripInBoundsConstantOffsets();
00409   const Value *stripInBoundsConstantOffsets() const {
00410     return const_cast<Value*>(this)->stripInBoundsConstantOffsets();
00411   }
00412 
00413   /// \brief Accumulate offsets from \a stripInBoundsConstantOffsets().
00414   ///
00415   /// Stores the resulting constant offset stripped into the APInt provided.
00416   /// The provided APInt will be extended or truncated as needed to be the
00417   /// correct bitwidth for an offset of this pointer type.
00418   ///
00419   /// If this is called on a non-pointer value, it returns 'this'.
00420   Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
00421                                                    APInt &Offset);
00422   const Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
00423                                                          APInt &Offset) const {
00424     return const_cast<Value *>(this)
00425         ->stripAndAccumulateInBoundsConstantOffsets(DL, Offset);
00426   }
00427 
00428   /// \brief Strip off pointer casts and inbounds GEPs.
00429   ///
00430   /// Returns the original pointer value.  If this is called on a non-pointer
00431   /// value, it returns 'this'.
00432   Value *stripInBoundsOffsets();
00433   const Value *stripInBoundsOffsets() const {
00434     return const_cast<Value*>(this)->stripInBoundsOffsets();
00435   }
00436 
00437   /// \brief Translate PHI node to its predecessor from the given basic block.
00438   ///
00439   /// If this value is a PHI node with CurBB as its parent, return the value in
00440   /// the PHI node corresponding to PredBB.  If not, return ourself.  This is
00441   /// useful if you want to know the value something has in a predecessor
00442   /// block.
00443   Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB);
00444 
00445   const Value *DoPHITranslation(const BasicBlock *CurBB,
00446                                 const BasicBlock *PredBB) const{
00447     return const_cast<Value*>(this)->DoPHITranslation(CurBB, PredBB);
00448   }
00449 
00450   /// \brief The maximum alignment for instructions.
00451   ///
00452   /// This is the greatest alignment value supported by load, store, and alloca
00453   /// instructions, and global values.
00454   static const unsigned MaxAlignmentExponent = 29;
00455   static const unsigned MaximumAlignment = 1u << MaxAlignmentExponent;
00456 
00457   /// \brief Mutate the type of this Value to be of the specified type.
00458   ///
00459   /// Note that this is an extremely dangerous operation which can create
00460   /// completely invalid IR very easily.  It is strongly recommended that you
00461   /// recreate IR objects with the right types instead of mutating them in
00462   /// place.
00463   void mutateType(Type *Ty) {
00464     VTy = Ty;
00465   }
00466 
00467   /// \brief Sort the use-list.
00468   ///
00469   /// Sorts the Value's use-list by Cmp using a stable mergesort.  Cmp is
00470   /// expected to compare two \a Use references.
00471   template <class Compare> void sortUseList(Compare Cmp);
00472 
00473   /// \brief Reverse the use-list.
00474   void reverseUseList();
00475 
00476 private:
00477   /// \brief Merge two lists together.
00478   ///
00479   /// Merges \c L and \c R using \c Cmp.  To enable stable sorts, always pushes
00480   /// "equal" items from L before items from R.
00481   ///
00482   /// \return the first element in the list.
00483   ///
00484   /// \note Completely ignores \a Use::Prev (doesn't read, doesn't update).
00485   template <class Compare>
00486   static Use *mergeUseLists(Use *L, Use *R, Compare Cmp) {
00487     Use *Merged;
00488     mergeUseListsImpl(L, R, &Merged, Cmp);
00489     return Merged;
00490   }
00491 
00492   /// \brief Tail-recursive helper for \a mergeUseLists().
00493   ///
00494   /// \param[out] Next the first element in the list.
00495   template <class Compare>
00496   static void mergeUseListsImpl(Use *L, Use *R, Use **Next, Compare Cmp);
00497 
00498 protected:
00499   unsigned short getSubclassDataFromValue() const { return SubclassData; }
00500   void setValueSubclassData(unsigned short D) { SubclassData = D; }
00501 };
00502 
00503 inline raw_ostream &operator<<(raw_ostream &OS, const Value &V) {
00504   V.print(OS);
00505   return OS;
00506 }
00507 
00508 void Use::set(Value *V) {
00509   if (Val) removeFromList();
00510   Val = V;
00511   if (V) V->addUse(*this);
00512 }
00513 
00514 template <class Compare> void Value::sortUseList(Compare Cmp) {
00515   if (!UseList || !UseList->Next)
00516     // No need to sort 0 or 1 uses.
00517     return;
00518 
00519   // Note: this function completely ignores Prev pointers until the end when
00520   // they're fixed en masse.
00521 
00522   // Create a binomial vector of sorted lists, visiting uses one at a time and
00523   // merging lists as necessary.
00524   const unsigned MaxSlots = 32;
00525   Use *Slots[MaxSlots];
00526 
00527   // Collect the first use, turning it into a single-item list.
00528   Use *Next = UseList->Next;
00529   UseList->Next = nullptr;
00530   unsigned NumSlots = 1;
00531   Slots[0] = UseList;
00532 
00533   // Collect all but the last use.
00534   while (Next->Next) {
00535     Use *Current = Next;
00536     Next = Current->Next;
00537 
00538     // Turn Current into a single-item list.
00539     Current->Next = nullptr;
00540 
00541     // Save Current in the first available slot, merging on collisions.
00542     unsigned I;
00543     for (I = 0; I < NumSlots; ++I) {
00544       if (!Slots[I])
00545         break;
00546 
00547       // Merge two lists, doubling the size of Current and emptying slot I.
00548       //
00549       // Since the uses in Slots[I] originally preceded those in Current, send
00550       // Slots[I] in as the left parameter to maintain a stable sort.
00551       Current = mergeUseLists(Slots[I], Current, Cmp);
00552       Slots[I] = nullptr;
00553     }
00554     // Check if this is a new slot.
00555     if (I == NumSlots) {
00556       ++NumSlots;
00557       assert(NumSlots <= MaxSlots && "Use list bigger than 2^32");
00558     }
00559 
00560     // Found an open slot.
00561     Slots[I] = Current;
00562   }
00563 
00564   // Merge all the lists together.
00565   assert(Next && "Expected one more Use");
00566   assert(!Next->Next && "Expected only one Use");
00567   UseList = Next;
00568   for (unsigned I = 0; I < NumSlots; ++I)
00569     if (Slots[I])
00570       // Since the uses in Slots[I] originally preceded those in UseList, send
00571       // Slots[I] in as the left parameter to maintain a stable sort.
00572       UseList = mergeUseLists(Slots[I], UseList, Cmp);
00573 
00574   // Fix the Prev pointers.
00575   for (Use *I = UseList, **Prev = &UseList; I; I = I->Next) {
00576     I->setPrev(Prev);
00577     Prev = &I->Next;
00578   }
00579 }
00580 
00581 template <class Compare>
00582 void Value::mergeUseListsImpl(Use *L, Use *R, Use **Next, Compare Cmp) {
00583   if (!L) {
00584     *Next = R;
00585     return;
00586   }
00587   if (!R) {
00588     *Next = L;
00589     return;
00590   }
00591   if (Cmp(*R, *L)) {
00592     *Next = R;
00593     mergeUseListsImpl(L, R->Next, &R->Next, Cmp);
00594     return;
00595   }
00596   *Next = L;
00597   mergeUseListsImpl(L->Next, R, &L->Next, Cmp);
00598 }
00599 
00600 // isa - Provide some specializations of isa so that we don't have to include
00601 // the subtype header files to test to see if the value is a subclass...
00602 //
00603 template <> struct isa_impl<Constant, Value> {
00604   static inline bool doit(const Value &Val) {
00605     return Val.getValueID() >= Value::ConstantFirstVal &&
00606       Val.getValueID() <= Value::ConstantLastVal;
00607   }
00608 };
00609 
00610 template <> struct isa_impl<Argument, Value> {
00611   static inline bool doit (const Value &Val) {
00612     return Val.getValueID() == Value::ArgumentVal;
00613   }
00614 };
00615 
00616 template <> struct isa_impl<InlineAsm, Value> {
00617   static inline bool doit(const Value &Val) {
00618     return Val.getValueID() == Value::InlineAsmVal;
00619   }
00620 };
00621 
00622 template <> struct isa_impl<Instruction, Value> {
00623   static inline bool doit(const Value &Val) {
00624     return Val.getValueID() >= Value::InstructionVal;
00625   }
00626 };
00627 
00628 template <> struct isa_impl<BasicBlock, Value> {
00629   static inline bool doit(const Value &Val) {
00630     return Val.getValueID() == Value::BasicBlockVal;
00631   }
00632 };
00633 
00634 template <> struct isa_impl<Function, Value> {
00635   static inline bool doit(const Value &Val) {
00636     return Val.getValueID() == Value::FunctionVal;
00637   }
00638 };
00639 
00640 template <> struct isa_impl<GlobalVariable, Value> {
00641   static inline bool doit(const Value &Val) {
00642     return Val.getValueID() == Value::GlobalVariableVal;
00643   }
00644 };
00645 
00646 template <> struct isa_impl<GlobalAlias, Value> {
00647   static inline bool doit(const Value &Val) {
00648     return Val.getValueID() == Value::GlobalAliasVal;
00649   }
00650 };
00651 
00652 template <> struct isa_impl<GlobalValue, Value> {
00653   static inline bool doit(const Value &Val) {
00654     return isa<GlobalObject>(Val) || isa<GlobalAlias>(Val);
00655   }
00656 };
00657 
00658 template <> struct isa_impl<GlobalObject, Value> {
00659   static inline bool doit(const Value &Val) {
00660     return isa<GlobalVariable>(Val) || isa<Function>(Val);
00661   }
00662 };
00663 
00664 // Value* is only 4-byte aligned.
00665 template<>
00666 class PointerLikeTypeTraits<Value*> {
00667   typedef Value* PT;
00668 public:
00669   static inline void *getAsVoidPointer(PT P) { return P; }
00670   static inline PT getFromVoidPointer(void *P) {
00671     return static_cast<PT>(P);
00672   }
00673   enum { NumLowBitsAvailable = 2 };
00674 };
00675 
00676 // Create wrappers for C Binding types (see CBindingWrapping.h).
00677 DEFINE_ISA_CONVERSION_FUNCTIONS(Value, LLVMValueRef)
00678 
00679 /* Specialized opaque value conversions.
00680  */
00681 inline Value **unwrap(LLVMValueRef *Vals) {
00682   return reinterpret_cast<Value**>(Vals);
00683 }
00684 
00685 template<typename T>
00686 inline T **unwrap(LLVMValueRef *Vals, unsigned Length) {
00687 #ifdef DEBUG
00688   for (LLVMValueRef *I = Vals, *E = Vals + Length; I != E; ++I)
00689     cast<T>(*I);
00690 #endif
00691   (void)Length;
00692   return reinterpret_cast<T**>(Vals);
00693 }
00694 
00695 inline LLVMValueRef *wrap(const Value **Vals) {
00696   return reinterpret_cast<LLVMValueRef*>(const_cast<Value**>(Vals));
00697 }
00698 
00699 } // End llvm namespace
00700 
00701 #endif