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