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