LLVM API Documentation

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