LLVM API Documentation

Use.h

Go to the documentation of this file.
00001 //===-- llvm/Use.h - Definition of the Use class ----------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This defines the Use class.  The Use class represents the operand of an
00011 // instruction or some other User instance which refers to a Value.  The Use
00012 // class keeps the "use list" of the referenced value up to date.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #ifndef LLVM_USE_H
00017 #define LLVM_USE_H
00018 
00019 #include "llvm/Support/Casting.h"
00020 #include "llvm/ADT/iterator"
00021 
00022 namespace llvm {
00023 
00024 class Value;
00025 class User;
00026 
00027 
00028 //===----------------------------------------------------------------------===//
00029 //                                  Use Class
00030 //===----------------------------------------------------------------------===//
00031 
00032 // Use is here to make keeping the "use" list of a Value up-to-date really easy.
00033 //
00034 class Use {
00035 public:
00036   inline void init(Value *V, User *U);
00037 
00038   Use(Value *V, User *U) { init(V, U); }
00039   Use(const Use &U) { init(U.Val, U.U); }
00040   inline ~Use();
00041 
00042   /// Default ctor - This leaves the Use completely unitialized.  The only thing
00043   /// that is valid to do with this use is to call the "init" method.
00044   inline Use() : Val(0) {}
00045 
00046 
00047   operator Value*() const { return Val; }
00048   Value *get() const { return Val; }
00049   User *getUser() const { return U; }
00050 
00051   inline void set(Value *Val);
00052 
00053   Value *operator=(Value *RHS) {
00054     set(RHS);
00055     return RHS;
00056   }
00057   const Use &operator=(const Use &RHS) {
00058     set(RHS.Val);
00059     return *this;
00060   }
00061 
00062         Value *operator->()       { return Val; }
00063   const Value *operator->() const { return Val; }
00064 
00065   Use *getNext() const { return Next; }
00066 private:
00067   Use *Next, **Prev;
00068   Value *Val;
00069   User *U;
00070 
00071   void addToList(Use **List) {
00072     Next = *List;
00073     if (Next) Next->Prev = &Next;
00074     Prev = List;
00075     *List = this;
00076   }
00077   void removeFromList() {
00078     *Prev = Next;
00079     if (Next) Next->Prev = Prev;
00080   }
00081 
00082   friend class Value;
00083 };
00084 
00085 // simplify_type - Allow clients to treat uses just like values when using
00086 // casting operators.
00087 template<> struct simplify_type<Use> {
00088   typedef Value* SimpleType;
00089   static SimpleType getSimplifiedValue(const Use &Val) {
00090     return static_cast<SimpleType>(Val.get());
00091   }
00092 };
00093 template<> struct simplify_type<const Use> {
00094   typedef Value* SimpleType;
00095   static SimpleType getSimplifiedValue(const Use &Val) {
00096     return static_cast<SimpleType>(Val.get());
00097   }
00098 };
00099 
00100 
00101 
00102 template<typename UserTy>  // UserTy == 'User' or 'const User'
00103 class value_use_iterator : public forward_iterator<UserTy*, ptrdiff_t> {
00104   typedef forward_iterator<UserTy*, ptrdiff_t> super;
00105   typedef value_use_iterator<UserTy> _Self;
00106 
00107   Use *U;
00108   value_use_iterator(Use *u) : U(u) {}
00109   friend class Value;
00110 public:
00111   typedef typename super::reference reference;
00112   typedef typename super::pointer pointer;
00113 
00114   value_use_iterator(const _Self &I) : U(I.U) {}
00115   value_use_iterator() {}
00116 
00117   bool operator==(const _Self &x) const {
00118     return U == x.U;
00119   }
00120   bool operator!=(const _Self &x) const {
00121     return !operator==(x);
00122   }
00123 
00124   // Iterator traversal: forward iteration only
00125   _Self &operator++() {          // Preincrement
00126     assert(U && "Cannot increment end iterator!");
00127     U = U->getNext();
00128     return *this;
00129   }
00130   _Self operator++(int) {        // Postincrement
00131     _Self tmp = *this; ++*this; return tmp;
00132   }
00133 
00134   // Retrieve a reference to the current SCC
00135   UserTy *operator*() const {
00136     assert(U && "Cannot increment end iterator!");
00137     return U->getUser();
00138   }
00139 
00140   UserTy *operator->() const { return operator*(); }
00141 
00142   Use &getUse() const { return *U; }
00143 };
00144 
00145 
00146 template<> struct simplify_type<value_use_iterator<User> > {
00147   typedef User* SimpleType;
00148   
00149   static SimpleType getSimplifiedValue(const value_use_iterator<User> &Val) {
00150     return *Val;
00151   }
00152 };
00153 
00154 template<> struct simplify_type<const value_use_iterator<User> >
00155  : public simplify_type<value_use_iterator<User> > {};
00156 
00157 template<> struct simplify_type<value_use_iterator<const User> > {
00158   typedef const User* SimpleType;
00159   
00160   static SimpleType getSimplifiedValue(const 
00161                                        value_use_iterator<const User> &Val) {
00162     return *Val;
00163   }
00164 };
00165 
00166 template<> struct simplify_type<const value_use_iterator<const User> >
00167   : public simplify_type<value_use_iterator<const User> > {};
00168 
00169 } // End llvm namespace
00170 
00171 #endif