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; }
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   /// replaceUsesOutsideBlock - Go through the uses list for this definition and
00262   /// make each use point to "V" instead of "this" when the use is outside the
00263   /// block. 'This's use list is expected to have at least one element.
00264   /// Unlike replaceAllUsesWith this function does not support basic block
00265   /// values or constant users.
00266   void replaceUsesOutsideBlock(Value *V, BasicBlock *BB);
00267 
00268   //----------------------------------------------------------------------
00269   // Methods for handling the chain of uses of this Value.
00270   //
00271   bool               use_empty() const { return UseList == nullptr; }
00272 
00273   typedef use_iterator_impl<Use>       use_iterator;
00274   typedef use_iterator_impl<const Use> const_use_iterator;
00275   use_iterator       use_begin()       { return use_iterator(UseList); }
00276   const_use_iterator use_begin() const { return const_use_iterator(UseList); }
00277   use_iterator       use_end()         { return use_iterator();   }
00278   const_use_iterator use_end()   const { return const_use_iterator();   }
00279   iterator_range<use_iterator> uses() {
00280     return iterator_range<use_iterator>(use_begin(), use_end());
00281   }
00282   iterator_range<const_use_iterator> uses() const {
00283     return iterator_range<const_use_iterator>(use_begin(), use_end());
00284   }
00285 
00286   typedef user_iterator_impl<User>       user_iterator;
00287   typedef user_iterator_impl<const User> const_user_iterator;
00288   user_iterator       user_begin()       { return user_iterator(UseList); }
00289   const_user_iterator user_begin() const { return const_user_iterator(UseList); }
00290   user_iterator       user_end()         { return user_iterator();   }
00291   const_user_iterator user_end()   const { return const_user_iterator();   }
00292   User               *user_back()        { return *user_begin(); }
00293   const User         *user_back()  const { return *user_begin(); }
00294   iterator_range<user_iterator> users() {
00295     return iterator_range<user_iterator>(user_begin(), user_end());
00296   }
00297   iterator_range<const_user_iterator> users() const {
00298     return iterator_range<const_user_iterator>(user_begin(), user_end());
00299   }
00300 
00301   /// \brief Return true if there is exactly one user of this value.
00302   ///
00303   /// This is specialized because it is a common request and does not require
00304   /// traversing the whole use list.
00305   bool hasOneUse() const {
00306     const_use_iterator I = use_begin(), E = use_end();
00307     if (I == E) return false;
00308     return ++I == E;
00309   }
00310 
00311   /// \brief Return true if this Value has exactly N users.
00312   bool hasNUses(unsigned N) const;
00313 
00314   /// \brief Return true if this value has N users or more.
00315   ///
00316   /// This is logically equivalent to getNumUses() >= N.
00317   bool hasNUsesOrMore(unsigned N) const;
00318 
00319   /// \brief Check if this value is used in the specified basic block.
00320   bool isUsedInBasicBlock(const BasicBlock *BB) const;
00321 
00322   /// \brief This method computes the number of uses of this Value.
00323   ///
00324   /// This is a linear time operation.  Use hasOneUse, hasNUses, or
00325   /// hasNUsesOrMore to check for specific values.
00326   unsigned getNumUses() const;
00327 
00328   /// \brief This method should only be used by the Use class.
00329   void addUse(Use &U) { U.addToList(&UseList); }
00330 
00331   /// \brief Concrete subclass of this.
00332   ///
00333   /// An enumeration for keeping track of the concrete subclass of Value that
00334   /// is actually instantiated. Values of this enumeration are kept in the
00335   /// Value classes SubclassID field. They are used for concrete type
00336   /// identification.
00337   enum ValueTy {
00338     ArgumentVal,              // This is an instance of Argument
00339     BasicBlockVal,            // This is an instance of BasicBlock
00340     FunctionVal,              // This is an instance of Function
00341     GlobalAliasVal,           // This is an instance of GlobalAlias
00342     GlobalVariableVal,        // This is an instance of GlobalVariable
00343     UndefValueVal,            // This is an instance of UndefValue
00344     BlockAddressVal,          // This is an instance of BlockAddress
00345     ConstantExprVal,          // This is an instance of ConstantExpr
00346     ConstantAggregateZeroVal, // This is an instance of ConstantAggregateZero
00347     ConstantDataArrayVal,     // This is an instance of ConstantDataArray
00348     ConstantDataVectorVal,    // This is an instance of ConstantDataVector
00349     ConstantIntVal,           // This is an instance of ConstantInt
00350     ConstantFPVal,            // This is an instance of ConstantFP
00351     ConstantArrayVal,         // This is an instance of ConstantArray
00352     ConstantStructVal,        // This is an instance of ConstantStruct
00353     ConstantVectorVal,        // This is an instance of ConstantVector
00354     ConstantPointerNullVal,   // This is an instance of ConstantPointerNull
00355     GenericMDNodeVal,         // This is an instance of GenericMDNode
00356     MDNodeFwdDeclVal,         // This is an instance of MDNodeFwdDecl
00357     MDStringVal,              // This is an instance of MDString
00358     InlineAsmVal,             // This is an instance of InlineAsm
00359     InstructionVal,           // This is an instance of Instruction
00360     // Enum values starting at InstructionVal are used for Instructions;
00361     // don't add new values here!
00362 
00363     // Markers:
00364     ConstantFirstVal = FunctionVal,
00365     ConstantLastVal  = ConstantPointerNullVal
00366   };
00367 
00368   /// \brief Return an ID for the concrete type of this object.
00369   ///
00370   /// This is used to implement the classof checks.  This should not be used
00371   /// for any other purpose, as the values may change as LLVM evolves.  Also,
00372   /// note that for instructions, the Instruction's opcode is added to
00373   /// InstructionVal. So this means three things:
00374   /// # there is no value with code InstructionVal (no opcode==0).
00375   /// # there are more possible values for the value type than in ValueTy enum.
00376   /// # the InstructionVal enumerator must be the highest valued enumerator in
00377   ///   the ValueTy enum.
00378   unsigned getValueID() const {
00379     return SubclassID;
00380   }
00381 
00382   /// \brief Return the raw optional flags value contained in this value.
00383   ///
00384   /// This should only be used when testing two Values for equivalence.
00385   unsigned getRawSubclassOptionalData() const {
00386     return SubclassOptionalData;
00387   }
00388 
00389   /// \brief Clear the optional flags contained in this value.
00390   void clearSubclassOptionalData() {
00391     SubclassOptionalData = 0;
00392   }
00393 
00394   /// \brief Check the optional flags for equality.
00395   bool hasSameSubclassOptionalData(const Value *V) const {
00396     return SubclassOptionalData == V->SubclassOptionalData;
00397   }
00398 
00399   /// \brief Clear any optional flags not set in the given Value.
00400   void intersectOptionalDataWith(const Value *V) {
00401     SubclassOptionalData &= V->SubclassOptionalData;
00402   }
00403 
00404   /// \brief Return true if there is a value handle associated with this value.
00405   bool hasValueHandle() const { return HasValueHandle; }
00406 
00407   /// \brief Strip off pointer casts, all-zero GEPs, and aliases.
00408   ///
00409   /// Returns the original uncasted value.  If this is called on a non-pointer
00410   /// value, it returns 'this'.
00411   Value *stripPointerCasts();
00412   const Value *stripPointerCasts() const {
00413     return const_cast<Value*>(this)->stripPointerCasts();
00414   }
00415 
00416   /// \brief Strip off pointer casts and all-zero GEPs.
00417   ///
00418   /// Returns the original uncasted value.  If this is called on a non-pointer
00419   /// value, it returns 'this'.
00420   Value *stripPointerCastsNoFollowAliases();
00421   const Value *stripPointerCastsNoFollowAliases() const {
00422     return const_cast<Value*>(this)->stripPointerCastsNoFollowAliases();
00423   }
00424 
00425   /// \brief Strip off pointer casts and all-constant inbounds GEPs.
00426   ///
00427   /// Returns the original pointer value.  If this is called on a non-pointer
00428   /// value, it returns 'this'.
00429   Value *stripInBoundsConstantOffsets();
00430   const Value *stripInBoundsConstantOffsets() const {
00431     return const_cast<Value*>(this)->stripInBoundsConstantOffsets();
00432   }
00433 
00434   /// \brief Accumulate offsets from \a stripInBoundsConstantOffsets().
00435   ///
00436   /// Stores the resulting constant offset stripped into the APInt provided.
00437   /// The provided APInt will be extended or truncated as needed to be the
00438   /// correct bitwidth for an offset of this pointer type.
00439   ///
00440   /// If this is called on a non-pointer value, it returns 'this'.
00441   Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
00442                                                    APInt &Offset);
00443   const Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
00444                                                          APInt &Offset) const {
00445     return const_cast<Value *>(this)
00446         ->stripAndAccumulateInBoundsConstantOffsets(DL, Offset);
00447   }
00448 
00449   /// \brief Strip off pointer casts and inbounds GEPs.
00450   ///
00451   /// Returns the original pointer value.  If this is called on a non-pointer
00452   /// value, it returns 'this'.
00453   Value *stripInBoundsOffsets();
00454   const Value *stripInBoundsOffsets() const {
00455     return const_cast<Value*>(this)->stripInBoundsOffsets();
00456   }
00457 
00458   /// \brief Check if this is always a dereferenceable pointer.
00459   ///
00460   /// Test if this value is always a pointer to allocated and suitably aligned
00461   /// memory for a simple load or store.
00462   bool isDereferenceablePointer(const DataLayout *DL = nullptr) const;
00463 
00464   /// \brief Translate PHI node to its predecessor from the given basic block.
00465   ///
00466   /// If this value is a PHI node with CurBB as its parent, return the value in
00467   /// the PHI node corresponding to PredBB.  If not, return ourself.  This is
00468   /// useful if you want to know the value something has in a predecessor
00469   /// block.
00470   Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB);
00471 
00472   const Value *DoPHITranslation(const BasicBlock *CurBB,
00473                                 const BasicBlock *PredBB) const{
00474     return const_cast<Value*>(this)->DoPHITranslation(CurBB, PredBB);
00475   }
00476 
00477   /// \brief The maximum alignment for instructions.
00478   ///
00479   /// This is the greatest alignment value supported by load, store, and alloca
00480   /// instructions, and global values.
00481   static const unsigned MaximumAlignment = 1u << 29;
00482 
00483   /// \brief Mutate the type of this Value to be of the specified type.
00484   ///
00485   /// Note that this is an extremely dangerous operation which can create
00486   /// completely invalid IR very easily.  It is strongly recommended that you
00487   /// recreate IR objects with the right types instead of mutating them in
00488   /// place.
00489   void mutateType(Type *Ty) {
00490     VTy = Ty;
00491   }
00492 
00493   /// \brief Sort the use-list.
00494   ///
00495   /// Sorts the Value's use-list by Cmp using a stable mergesort.  Cmp is
00496   /// expected to compare two \a Use references.
00497   template <class Compare> void sortUseList(Compare Cmp);
00498 
00499   /// \brief Reverse the use-list.
00500   void reverseUseList();
00501 
00502 private:
00503   /// \brief Merge two lists together.
00504   ///
00505   /// Merges \c L and \c R using \c Cmp.  To enable stable sorts, always pushes
00506   /// "equal" items from L before items from R.
00507   ///
00508   /// \return the first element in the list.
00509   ///
00510   /// \note Completely ignores \a Use::Prev (doesn't read, doesn't update).
00511   template <class Compare>
00512   static Use *mergeUseLists(Use *L, Use *R, Compare Cmp) {
00513     Use *Merged;
00514     mergeUseListsImpl(L, R, &Merged, Cmp);
00515     return Merged;
00516   }
00517 
00518   /// \brief Tail-recursive helper for \a mergeUseLists().
00519   ///
00520   /// \param[out] Next the first element in the list.
00521   template <class Compare>
00522   static void mergeUseListsImpl(Use *L, Use *R, Use **Next, Compare Cmp);
00523 
00524 protected:
00525   unsigned short getSubclassDataFromValue() const { return SubclassData; }
00526   void setValueSubclassData(unsigned short D) { SubclassData = D; }
00527 };
00528 
00529 inline raw_ostream &operator<<(raw_ostream &OS, const Value &V) {
00530   V.print(OS);
00531   return OS;
00532 }
00533 
00534 void Use::set(Value *V) {
00535   if (Val) removeFromList();
00536   Val = V;
00537   if (V) V->addUse(*this);
00538 }
00539 
00540 template <class Compare> void Value::sortUseList(Compare Cmp) {
00541   if (!UseList || !UseList->Next)
00542     // No need to sort 0 or 1 uses.
00543     return;
00544 
00545   // Note: this function completely ignores Prev pointers until the end when
00546   // they're fixed en masse.
00547 
00548   // Create a binomial vector of sorted lists, visiting uses one at a time and
00549   // merging lists as necessary.
00550   const unsigned MaxSlots = 32;
00551   Use *Slots[MaxSlots];
00552 
00553   // Collect the first use, turning it into a single-item list.
00554   Use *Next = UseList->Next;
00555   UseList->Next = nullptr;
00556   unsigned NumSlots = 1;
00557   Slots[0] = UseList;
00558 
00559   // Collect all but the last use.
00560   while (Next->Next) {
00561     Use *Current = Next;
00562     Next = Current->Next;
00563 
00564     // Turn Current into a single-item list.
00565     Current->Next = nullptr;
00566 
00567     // Save Current in the first available slot, merging on collisions.
00568     unsigned I;
00569     for (I = 0; I < NumSlots; ++I) {
00570       if (!Slots[I])
00571         break;
00572 
00573       // Merge two lists, doubling the size of Current and emptying slot I.
00574       //
00575       // Since the uses in Slots[I] originally preceded those in Current, send
00576       // Slots[I] in as the left parameter to maintain a stable sort.
00577       Current = mergeUseLists(Slots[I], Current, Cmp);
00578       Slots[I] = nullptr;
00579     }
00580     // Check if this is a new slot.
00581     if (I == NumSlots) {
00582       ++NumSlots;
00583       assert(NumSlots <= MaxSlots && "Use list bigger than 2^32");
00584     }
00585 
00586     // Found an open slot.
00587     Slots[I] = Current;
00588   }
00589 
00590   // Merge all the lists together.
00591   assert(Next && "Expected one more Use");
00592   assert(!Next->Next && "Expected only one Use");
00593   UseList = Next;
00594   for (unsigned I = 0; I < NumSlots; ++I)
00595     if (Slots[I])
00596       // Since the uses in Slots[I] originally preceded those in UseList, send
00597       // Slots[I] in as the left parameter to maintain a stable sort.
00598       UseList = mergeUseLists(Slots[I], UseList, Cmp);
00599 
00600   // Fix the Prev pointers.
00601   for (Use *I = UseList, **Prev = &UseList; I; I = I->Next) {
00602     I->setPrev(Prev);
00603     Prev = &I->Next;
00604   }
00605 }
00606 
00607 template <class Compare>
00608 void Value::mergeUseListsImpl(Use *L, Use *R, Use **Next, Compare Cmp) {
00609   if (!L) {
00610     *Next = R;
00611     return;
00612   }
00613   if (!R) {
00614     *Next = L;
00615     return;
00616   }
00617   if (Cmp(*R, *L)) {
00618     *Next = R;
00619     mergeUseListsImpl(L, R->Next, &R->Next, Cmp);
00620     return;
00621   }
00622   *Next = L;
00623   mergeUseListsImpl(L->Next, R, &L->Next, Cmp);
00624 }
00625 
00626 // isa - Provide some specializations of isa so that we don't have to include
00627 // the subtype header files to test to see if the value is a subclass...
00628 //
00629 template <> struct isa_impl<Constant, Value> {
00630   static inline bool doit(const Value &Val) {
00631     return Val.getValueID() >= Value::ConstantFirstVal &&
00632       Val.getValueID() <= Value::ConstantLastVal;
00633   }
00634 };
00635 
00636 template <> struct isa_impl<Argument, Value> {
00637   static inline bool doit (const Value &Val) {
00638     return Val.getValueID() == Value::ArgumentVal;
00639   }
00640 };
00641 
00642 template <> struct isa_impl<InlineAsm, Value> {
00643   static inline bool doit(const Value &Val) {
00644     return Val.getValueID() == Value::InlineAsmVal;
00645   }
00646 };
00647 
00648 template <> struct isa_impl<Instruction, Value> {
00649   static inline bool doit(const Value &Val) {
00650     return Val.getValueID() >= Value::InstructionVal;
00651   }
00652 };
00653 
00654 template <> struct isa_impl<BasicBlock, Value> {
00655   static inline bool doit(const Value &Val) {
00656     return Val.getValueID() == Value::BasicBlockVal;
00657   }
00658 };
00659 
00660 template <> struct isa_impl<Function, Value> {
00661   static inline bool doit(const Value &Val) {
00662     return Val.getValueID() == Value::FunctionVal;
00663   }
00664 };
00665 
00666 template <> struct isa_impl<GlobalVariable, Value> {
00667   static inline bool doit(const Value &Val) {
00668     return Val.getValueID() == Value::GlobalVariableVal;
00669   }
00670 };
00671 
00672 template <> struct isa_impl<GlobalAlias, Value> {
00673   static inline bool doit(const Value &Val) {
00674     return Val.getValueID() == Value::GlobalAliasVal;
00675   }
00676 };
00677 
00678 template <> struct isa_impl<GlobalValue, Value> {
00679   static inline bool doit(const Value &Val) {
00680     return isa<GlobalObject>(Val) || isa<GlobalAlias>(Val);
00681   }
00682 };
00683 
00684 template <> struct isa_impl<GlobalObject, Value> {
00685   static inline bool doit(const Value &Val) {
00686     return isa<GlobalVariable>(Val) || isa<Function>(Val);
00687   }
00688 };
00689 
00690 template <> struct isa_impl<MDNode, Value> {
00691   static inline bool doit(const Value &Val) {
00692     return Val.getValueID() == Value::GenericMDNodeVal ||
00693            Val.getValueID() == Value::MDNodeFwdDeclVal;
00694   }
00695 };
00696 
00697 // Value* is only 4-byte aligned.
00698 template<>
00699 class PointerLikeTypeTraits<Value*> {
00700   typedef Value* PT;
00701 public:
00702   static inline void *getAsVoidPointer(PT P) { return P; }
00703   static inline PT getFromVoidPointer(void *P) {
00704     return static_cast<PT>(P);
00705   }
00706   enum { NumLowBitsAvailable = 2 };
00707 };
00708 
00709 // Create wrappers for C Binding types (see CBindingWrapping.h).
00710 DEFINE_ISA_CONVERSION_FUNCTIONS(Value, LLVMValueRef)
00711 
00712 /* Specialized opaque value conversions.
00713  */
00714 inline Value **unwrap(LLVMValueRef *Vals) {
00715   return reinterpret_cast<Value**>(Vals);
00716 }
00717 
00718 template<typename T>
00719 inline T **unwrap(LLVMValueRef *Vals, unsigned Length) {
00720 #ifdef DEBUG
00721   for (LLVMValueRef *I = Vals, *E = Vals + Length; I != E; ++I)
00722     cast<T>(*I);
00723 #endif
00724   (void)Length;
00725   return reinterpret_cast<T**>(Vals);
00726 }
00727 
00728 inline LLVMValueRef *wrap(const Value **Vals) {
00729   return reinterpret_cast<LLVMValueRef*>(const_cast<Value**>(Vals));
00730 }
00731 
00732 } // End llvm namespace
00733 
00734 #endif