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