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 MDNode;
00041 class Module;
00042 class StringRef;
00043 class Twine;
00044 class Type;
00045 class ValueHandleBase;
00046 class ValueSymbolTable;
00047 class raw_ostream;
00048 
00049 template<typename ValueTy> class StringMapEntry;
00050 typedef StringMapEntry<Value*> ValueName;
00051 
00052 //===----------------------------------------------------------------------===//
00053 //                                 Value Class
00054 //===----------------------------------------------------------------------===//
00055 
00056 /// \brief LLVM Value Representation
00057 ///
00058 /// This is a very important LLVM class. It is the base class of all values
00059 /// computed by a program that may be used as operands to other values. Value is
00060 /// the super class of other important classes such as Instruction and Function.
00061 /// All Values have a Type. Type is not a subclass of Value. Some values can
00062 /// have a name and they belong to some Module.  Setting the name on the Value
00063 /// automatically updates the module's symbol table.
00064 ///
00065 /// Every value has a "use list" that keeps track of which other Values are
00066 /// using this Value.  A Value can also have an arbitrary number of ValueHandle
00067 /// objects that watch it and listen to RAUW and Destroy events.  See
00068 /// llvm/IR/ValueHandle.h for details.
00069 class Value {
00070   Type *VTy;
00071   Use *UseList;
00072 
00073   friend class ValueSymbolTable; // Allow ValueSymbolTable to directly mod Name.
00074   friend class ValueHandleBase;
00075   ValueName *Name;
00076 
00077   const unsigned char SubclassID;   // Subclass identifier (for isa/dyn_cast)
00078   unsigned char HasValueHandle : 1; // Has a ValueHandle pointing to this?
00079 protected:
00080   /// \brief Hold subclass data that can be dropped.
00081   ///
00082   /// This member is similar to SubclassData, however it is for holding
00083   /// information which may be used to aid optimization, but which may be
00084   /// cleared to zero without affecting conservative interpretation.
00085   unsigned char SubclassOptionalData : 7;
00086 
00087 private:
00088   /// \brief Hold arbitrary subclass data.
00089   ///
00090   /// This member is defined by this class, but is not used for anything.
00091   /// Subclasses can use it to hold whatever state they find useful.  This
00092   /// field is initialized to zero by the ctor.
00093   unsigned short SubclassData;
00094 
00095 protected:
00096   /// \brief The number of operands in the subclass.
00097   ///
00098   /// This member is defined by this class, but not used for anything.
00099   /// Subclasses can use it to store their number of operands, if they have
00100   /// any.
00101   ///
00102   /// This is stored here to save space in User on 64-bit hosts.  Since most
00103   /// instances of Value have operands, 32-bit hosts aren't significantly
00104   /// affected.
00105   unsigned NumOperands;
00106 
00107 private:
00108   template <typename UseT> // UseT == 'Use' or 'const Use'
00109   class use_iterator_impl
00110       : public std::iterator<std::forward_iterator_tag, UseT *, ptrdiff_t> {
00111     typedef std::iterator<std::forward_iterator_tag, UseT *, ptrdiff_t> super;
00112 
00113     UseT *U;
00114     explicit use_iterator_impl(UseT *u) : U(u) {}
00115     friend class Value;
00116 
00117   public:
00118     typedef typename super::reference reference;
00119     typedef typename super::pointer pointer;
00120 
00121     use_iterator_impl() : U() {}
00122 
00123     bool operator==(const use_iterator_impl &x) const { return U == x.U; }
00124     bool operator!=(const use_iterator_impl &x) const { return !operator==(x); }
00125 
00126     use_iterator_impl &operator++() { // Preincrement
00127       assert(U && "Cannot increment end iterator!");
00128       U = U->getNext();
00129       return *this;
00130     }
00131     use_iterator_impl operator++(int) { // Postincrement
00132       auto tmp = *this;
00133       ++*this;
00134       return tmp;
00135     }
00136 
00137     UseT &operator*() const {
00138       assert(U && "Cannot dereference end iterator!");
00139       return *U;
00140     }
00141 
00142     UseT *operator->() const { return &operator*(); }
00143 
00144     operator use_iterator_impl<const UseT>() const {
00145       return use_iterator_impl<const UseT>(U);
00146     }
00147   };
00148 
00149   template <typename UserTy> // UserTy == 'User' or 'const User'
00150   class user_iterator_impl
00151       : public std::iterator<std::forward_iterator_tag, UserTy *, ptrdiff_t> {
00152     typedef std::iterator<std::forward_iterator_tag, UserTy *, ptrdiff_t> super;
00153 
00154     use_iterator_impl<Use> UI;
00155     explicit user_iterator_impl(Use *U) : UI(U) {}
00156     friend class Value;
00157 
00158   public:
00159     typedef typename super::reference reference;
00160     typedef typename super::pointer pointer;
00161 
00162     user_iterator_impl() {}
00163 
00164     bool operator==(const user_iterator_impl &x) const { return UI == x.UI; }
00165     bool operator!=(const user_iterator_impl &x) const { return !operator==(x); }
00166 
00167     /// \brief Returns true if this iterator is equal to user_end() on the value.
00168     bool atEnd() const { return *this == user_iterator_impl(); }
00169 
00170     user_iterator_impl &operator++() { // Preincrement
00171       ++UI;
00172       return *this;
00173     }
00174     user_iterator_impl operator++(int) { // Postincrement
00175       auto tmp = *this;
00176       ++*this;
00177       return tmp;
00178     }
00179 
00180     // Retrieve a pointer to the current User.
00181     UserTy *operator*() const {
00182       return UI->getUser();
00183     }
00184 
00185     UserTy *operator->() const { return operator*(); }
00186 
00187     operator user_iterator_impl<const UserTy>() const {
00188       return user_iterator_impl<const UserTy>(*UI);
00189     }
00190 
00191     Use &getUse() const { return *UI; }
00192 
00193     /// \brief Return the operand # of this use in its User.
00194     ///
00195     /// FIXME: Replace all callers with a direct call to Use::getOperandNo.
00196     unsigned getOperandNo() const { return UI->getOperandNo(); }
00197   };
00198 
00199   void operator=(const Value &) LLVM_DELETED_FUNCTION;
00200   Value(const Value &) LLVM_DELETED_FUNCTION;
00201 
00202 protected:
00203   Value(Type *Ty, unsigned scid);
00204 public:
00205   virtual ~Value();
00206 
00207   /// \brief Support for debugging, callable in GDB: V->dump()
00208   void dump() const;
00209 
00210   /// \brief Implement operator<< on Value.
00211   void print(raw_ostream &O) const;
00212 
00213   /// \brief Print the name of this Value out to the specified raw_ostream.
00214   ///
00215   /// This is useful when you just want to print 'int %reg126', not the
00216   /// instruction that generated it. If you specify a Module for context, then
00217   /// even constanst get pretty-printed; for example, the type of a null
00218   /// pointer is printed symbolically.
00219   void printAsOperand(raw_ostream &O, bool PrintType = true,
00220                       const Module *M = nullptr) const;
00221 
00222   /// \brief All values are typed, get the type of this value.
00223   Type *getType() const { return VTy; }
00224 
00225   /// \brief All values hold a context through their type.
00226   LLVMContext &getContext() const;
00227 
00228   // \brief All values can potentially be named.
00229   bool hasName() const { return Name != nullptr && SubclassID != MDStringVal; }
00230   ValueName *getValueName() const { return Name; }
00231   void setValueName(ValueName *VN) { Name = VN; }
00232 
00233   /// \brief Return a constant reference to the value's name.
00234   ///
00235   /// This is cheap and guaranteed to return the same reference as long as the
00236   /// value is not modified.
00237   StringRef getName() const;
00238 
00239   /// \brief Change the name of the value.
00240   ///
00241   /// Choose a new unique name if the provided name is taken.
00242   ///
00243   /// \param Name The new name; or "" if the value's name should be removed.
00244   void setName(const Twine &Name);
00245 
00246 
00247   /// \brief Transfer the name from V to this value.
00248   ///
00249   /// After taking V's name, sets V's name to empty.
00250   ///
00251   /// \note It is an error to call V->takeName(V).
00252   void takeName(Value *V);
00253 
00254   /// \brief Change all uses of this to point to a new Value.
00255   ///
00256   /// Go through the uses list for this definition and make each use point to
00257   /// "V" instead of "this".  After this completes, 'this's use list is
00258   /// guaranteed to be empty.
00259   void replaceAllUsesWith(Value *V);
00260 
00261   //----------------------------------------------------------------------
00262   // Methods for handling the chain of uses of this Value.
00263   //
00264   bool               use_empty() const { return UseList == nullptr; }
00265 
00266   typedef use_iterator_impl<Use>       use_iterator;
00267   typedef use_iterator_impl<const Use> const_use_iterator;
00268   use_iterator       use_begin()       { return use_iterator(UseList); }
00269   const_use_iterator use_begin() const { return const_use_iterator(UseList); }
00270   use_iterator       use_end()         { return use_iterator();   }
00271   const_use_iterator use_end()   const { return const_use_iterator();   }
00272   iterator_range<use_iterator> uses() {
00273     return iterator_range<use_iterator>(use_begin(), use_end());
00274   }
00275   iterator_range<const_use_iterator> uses() const {
00276     return iterator_range<const_use_iterator>(use_begin(), use_end());
00277   }
00278 
00279   typedef user_iterator_impl<User>       user_iterator;
00280   typedef user_iterator_impl<const User> const_user_iterator;
00281   user_iterator       user_begin()       { return user_iterator(UseList); }
00282   const_user_iterator user_begin() const { return const_user_iterator(UseList); }
00283   user_iterator       user_end()         { return user_iterator();   }
00284   const_user_iterator user_end()   const { return const_user_iterator();   }
00285   User               *user_back()        { return *user_begin(); }
00286   const User         *user_back()  const { return *user_begin(); }
00287   iterator_range<user_iterator> users() {
00288     return iterator_range<user_iterator>(user_begin(), user_end());
00289   }
00290   iterator_range<const_user_iterator> users() const {
00291     return iterator_range<const_user_iterator>(user_begin(), user_end());
00292   }
00293 
00294   /// \brief Return true if there is exactly one user of this value.
00295   ///
00296   /// This is specialized because it is a common request and does not require
00297   /// traversing the whole use list.
00298   bool hasOneUse() const {
00299     const_use_iterator I = use_begin(), E = use_end();
00300     if (I == E) return false;
00301     return ++I == E;
00302   }
00303 
00304   /// \brief Return true if this Value has exactly N users.
00305   bool hasNUses(unsigned N) const;
00306 
00307   /// \brief Return true if this value has N users or more.
00308   ///
00309   /// This is logically equivalent to getNumUses() >= N.
00310   bool hasNUsesOrMore(unsigned N) const;
00311 
00312   /// \brief Check if this value is used in the specified basic block.
00313   bool isUsedInBasicBlock(const BasicBlock *BB) const;
00314 
00315   /// \brief This method computes the number of uses of this Value.
00316   ///
00317   /// This is a linear time operation.  Use hasOneUse, hasNUses, or
00318   /// hasNUsesOrMore to check for specific values.
00319   unsigned getNumUses() const;
00320 
00321   /// \brief This method should only be used by the Use class.
00322   void addUse(Use &U) { U.addToList(&UseList); }
00323 
00324   /// \brief Concrete subclass of this.
00325   ///
00326   /// An enumeration for keeping track of the concrete subclass of Value that
00327   /// is actually instantiated. Values of this enumeration are kept in the
00328   /// Value classes SubclassID field. They are used for concrete type
00329   /// identification.
00330   enum ValueTy {
00331     ArgumentVal,              // This is an instance of Argument
00332     BasicBlockVal,            // This is an instance of BasicBlock
00333     FunctionVal,              // This is an instance of Function
00334     GlobalAliasVal,           // This is an instance of GlobalAlias
00335     GlobalVariableVal,        // This is an instance of GlobalVariable
00336     UndefValueVal,            // This is an instance of UndefValue
00337     BlockAddressVal,          // This is an instance of BlockAddress
00338     ConstantExprVal,          // This is an instance of ConstantExpr
00339     ConstantAggregateZeroVal, // This is an instance of ConstantAggregateZero
00340     ConstantDataArrayVal,     // This is an instance of ConstantDataArray
00341     ConstantDataVectorVal,    // This is an instance of ConstantDataVector
00342     ConstantIntVal,           // This is an instance of ConstantInt
00343     ConstantFPVal,            // This is an instance of ConstantFP
00344     ConstantArrayVal,         // This is an instance of ConstantArray
00345     ConstantStructVal,        // This is an instance of ConstantStruct
00346     ConstantVectorVal,        // This is an instance of ConstantVector
00347     ConstantPointerNullVal,   // This is an instance of ConstantPointerNull
00348     MDNodeVal,                // This is an instance of MDNode
00349     MDStringVal,              // This is an instance of MDString
00350     InlineAsmVal,             // This is an instance of InlineAsm
00351     InstructionVal,           // This is an instance of Instruction
00352     // Enum values starting at InstructionVal are used for Instructions;
00353     // don't add new values here!
00354 
00355     // Markers:
00356     ConstantFirstVal = FunctionVal,
00357     ConstantLastVal  = ConstantPointerNullVal
00358   };
00359 
00360   /// \brief Return an ID for the concrete type of this object.
00361   ///
00362   /// This is used to implement the classof checks.  This should not be used
00363   /// for any other purpose, as the values may change as LLVM evolves.  Also,
00364   /// note that for instructions, the Instruction's opcode is added to
00365   /// InstructionVal. So this means three things:
00366   /// # there is no value with code InstructionVal (no opcode==0).
00367   /// # there are more possible values for the value type than in ValueTy enum.
00368   /// # the InstructionVal enumerator must be the highest valued enumerator in
00369   ///   the ValueTy enum.
00370   unsigned getValueID() const {
00371     return SubclassID;
00372   }
00373 
00374   /// \brief Return the raw optional flags value contained in this value.
00375   ///
00376   /// This should only be used when testing two Values for equivalence.
00377   unsigned getRawSubclassOptionalData() const {
00378     return SubclassOptionalData;
00379   }
00380 
00381   /// \brief Clear the optional flags contained in this value.
00382   void clearSubclassOptionalData() {
00383     SubclassOptionalData = 0;
00384   }
00385 
00386   /// \brief Check the optional flags for equality.
00387   bool hasSameSubclassOptionalData(const Value *V) const {
00388     return SubclassOptionalData == V->SubclassOptionalData;
00389   }
00390 
00391   /// \brief Clear any optional flags not set in the given Value.
00392   void intersectOptionalDataWith(const Value *V) {
00393     SubclassOptionalData &= V->SubclassOptionalData;
00394   }
00395 
00396   /// \brief Return true if there is a value handle associated with this value.
00397   bool hasValueHandle() const { return HasValueHandle; }
00398 
00399   /// \brief Strip off pointer casts, all-zero GEPs, and aliases.
00400   ///
00401   /// Returns the original uncasted value.  If this is called on a non-pointer
00402   /// value, it returns 'this'.
00403   Value *stripPointerCasts();
00404   const Value *stripPointerCasts() const {
00405     return const_cast<Value*>(this)->stripPointerCasts();
00406   }
00407 
00408   /// \brief Strip off pointer casts and all-zero GEPs.
00409   ///
00410   /// Returns the original uncasted value.  If this is called on a non-pointer
00411   /// value, it returns 'this'.
00412   Value *stripPointerCastsNoFollowAliases();
00413   const Value *stripPointerCastsNoFollowAliases() const {
00414     return const_cast<Value*>(this)->stripPointerCastsNoFollowAliases();
00415   }
00416 
00417   /// \brief Strip off pointer casts and all-constant inbounds GEPs.
00418   ///
00419   /// Returns the original pointer value.  If this is called on a non-pointer
00420   /// value, it returns 'this'.
00421   Value *stripInBoundsConstantOffsets();
00422   const Value *stripInBoundsConstantOffsets() const {
00423     return const_cast<Value*>(this)->stripInBoundsConstantOffsets();
00424   }
00425 
00426   /// \brief Accumulate offsets from \a stripInBoundsConstantOffsets().
00427   ///
00428   /// Stores the resulting constant offset stripped into the APInt provided.
00429   /// The provided APInt will be extended or truncated as needed to be the
00430   /// correct bitwidth for an offset of this pointer type.
00431   ///
00432   /// If this is called on a non-pointer value, it returns 'this'.
00433   Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
00434                                                    APInt &Offset);
00435   const Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
00436                                                          APInt &Offset) const {
00437     return const_cast<Value *>(this)
00438         ->stripAndAccumulateInBoundsConstantOffsets(DL, Offset);
00439   }
00440 
00441   /// \brief Strip off pointer casts and inbounds GEPs.
00442   ///
00443   /// Returns the original pointer value.  If this is called on a non-pointer
00444   /// value, it returns 'this'.
00445   Value *stripInBoundsOffsets();
00446   const Value *stripInBoundsOffsets() const {
00447     return const_cast<Value*>(this)->stripInBoundsOffsets();
00448   }
00449 
00450   /// \brief Check if this is always a dereferenceable pointer.
00451   ///
00452   /// Test if this value is always a pointer to allocated and suitably aligned
00453   /// memory for a simple load or store.
00454   bool isDereferenceablePointer(const DataLayout *DL = nullptr) const;
00455 
00456   /// \brief Translate PHI node to its predecessor from the given basic block.
00457   ///
00458   /// If this value is a PHI node with CurBB as its parent, return the value in
00459   /// the PHI node corresponding to PredBB.  If not, return ourself.  This is
00460   /// useful if you want to know the value something has in a predecessor
00461   /// block.
00462   Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB);
00463 
00464   const Value *DoPHITranslation(const BasicBlock *CurBB,
00465                                 const BasicBlock *PredBB) const{
00466     return const_cast<Value*>(this)->DoPHITranslation(CurBB, PredBB);
00467   }
00468 
00469   /// \brief The maximum alignment for instructions.
00470   ///
00471   /// This is the greatest alignment value supported by load, store, and alloca
00472   /// instructions, and global values.
00473   static const unsigned MaximumAlignment = 1u << 29;
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 template <> struct isa_impl<MDNode, Value> {
00683   static inline bool doit(const Value &Val) {
00684     return Val.getValueID() == Value::MDNodeVal;
00685   }
00686 };
00687 
00688 // Value* is only 4-byte aligned.
00689 template<>
00690 class PointerLikeTypeTraits<Value*> {
00691   typedef Value* PT;
00692 public:
00693   static inline void *getAsVoidPointer(PT P) { return P; }
00694   static inline PT getFromVoidPointer(void *P) {
00695     return static_cast<PT>(P);
00696   }
00697   enum { NumLowBitsAvailable = 2 };
00698 };
00699 
00700 // Create wrappers for C Binding types (see CBindingWrapping.h).
00701 DEFINE_ISA_CONVERSION_FUNCTIONS(Value, LLVMValueRef)
00702 
00703 /* Specialized opaque value conversions.
00704  */
00705 inline Value **unwrap(LLVMValueRef *Vals) {
00706   return reinterpret_cast<Value**>(Vals);
00707 }
00708 
00709 template<typename T>
00710 inline T **unwrap(LLVMValueRef *Vals, unsigned Length) {
00711 #ifdef DEBUG
00712   for (LLVMValueRef *I = Vals, *E = Vals + Length; I != E; ++I)
00713     cast<T>(*I);
00714 #endif
00715   (void)Length;
00716   return reinterpret_cast<T**>(Vals);
00717 }
00718 
00719 inline LLVMValueRef *wrap(const Value **Vals) {
00720   return reinterpret_cast<LLVMValueRef*>(const_cast<Value**>(Vals));
00721 }
00722 
00723 } // End llvm namespace
00724 
00725 #endif