LLVM API Documentation

User.h
Go to the documentation of this file.
00001 //===-- llvm/User.h - User class definition ---------------------*- 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 class defines the interface that one who uses a Value must implement.
00011 // Each instance of the Value class keeps track of what User's have handles
00012 // to it.
00013 //
00014 //  * Instructions are the largest class of Users.
00015 //  * Constants may be users of other constants (think arrays and stuff)
00016 //
00017 //===----------------------------------------------------------------------===//
00018 
00019 #ifndef LLVM_IR_USER_H
00020 #define LLVM_IR_USER_H
00021 
00022 #include "llvm/ADT/iterator_range.h"
00023 #include "llvm/IR/Value.h"
00024 #include "llvm/Support/ErrorHandling.h"
00025 
00026 namespace llvm {
00027 
00028 /// OperandTraits - Compile-time customization of
00029 /// operand-related allocators and accessors
00030 /// for use of the User class
00031 template <class>
00032 struct OperandTraits;
00033 
00034 class User : public Value {
00035   User(const User &) LLVM_DELETED_FUNCTION;
00036   void *operator new(size_t) LLVM_DELETED_FUNCTION;
00037   template <unsigned>
00038   friend struct HungoffOperandTraits;
00039   virtual void anchor();
00040 protected:
00041   /// OperandList - This is a pointer to the array of Uses for this User.
00042   /// For nodes of fixed arity (e.g. a binary operator) this array will live
00043   /// prefixed to some derived class instance.  For nodes of resizable variable
00044   /// arity (e.g. PHINodes, SwitchInst etc.), this memory will be dynamically
00045   /// allocated and should be destroyed by the classes' virtual dtor.
00046   Use *OperandList;
00047 
00048   /// NumOperands - The number of values used by this User.
00049   ///
00050   unsigned NumOperands;
00051 
00052   void *operator new(size_t s, unsigned Us);
00053   User(Type *ty, unsigned vty, Use *OpList, unsigned NumOps)
00054     : Value(ty, vty), OperandList(OpList), NumOperands(NumOps) {}
00055   Use *allocHungoffUses(unsigned) const;
00056   void dropHungoffUses() {
00057     Use::zap(OperandList, OperandList + NumOperands, true);
00058     OperandList = nullptr;
00059     // Reset NumOperands so User::operator delete() does the right thing.
00060     NumOperands = 0;
00061   }
00062 public:
00063   ~User() {
00064     Use::zap(OperandList, OperandList + NumOperands);
00065   }
00066   /// operator delete - free memory allocated for User and Use objects
00067   void operator delete(void *Usr);
00068   /// placement delete - required by std, but never called.
00069   void operator delete(void*, unsigned) {
00070     llvm_unreachable("Constructor throws?");
00071   }
00072   /// placement delete - required by std, but never called.
00073   void operator delete(void*, unsigned, bool) {
00074     llvm_unreachable("Constructor throws?");
00075   }
00076 protected:
00077   template <int Idx, typename U> static Use &OpFrom(const U *that) {
00078     return Idx < 0
00079       ? OperandTraits<U>::op_end(const_cast<U*>(that))[Idx]
00080       : OperandTraits<U>::op_begin(const_cast<U*>(that))[Idx];
00081   }
00082   template <int Idx> Use &Op() {
00083     return OpFrom<Idx>(this);
00084   }
00085   template <int Idx> const Use &Op() const {
00086     return OpFrom<Idx>(this);
00087   }
00088 public:
00089   Value *getOperand(unsigned i) const {
00090     assert(i < NumOperands && "getOperand() out of range!");
00091     return OperandList[i];
00092   }
00093   void setOperand(unsigned i, Value *Val) {
00094     assert(i < NumOperands && "setOperand() out of range!");
00095     assert((!isa<Constant>((const Value*)this) ||
00096             isa<GlobalValue>((const Value*)this)) &&
00097            "Cannot mutate a constant with setOperand!");
00098     OperandList[i] = Val;
00099   }
00100   const Use &getOperandUse(unsigned i) const {
00101     assert(i < NumOperands && "getOperandUse() out of range!");
00102     return OperandList[i];
00103   }
00104   Use &getOperandUse(unsigned i) {
00105     assert(i < NumOperands && "getOperandUse() out of range!");
00106     return OperandList[i];
00107   }
00108 
00109   unsigned getNumOperands() const { return NumOperands; }
00110 
00111   // ---------------------------------------------------------------------------
00112   // Operand Iterator interface...
00113   //
00114   typedef Use*       op_iterator;
00115   typedef const Use* const_op_iterator;
00116   typedef iterator_range<op_iterator> op_range;
00117   typedef iterator_range<const_op_iterator> const_op_range;
00118 
00119   inline op_iterator       op_begin()       { return OperandList; }
00120   inline const_op_iterator op_begin() const { return OperandList; }
00121   inline op_iterator       op_end()         { return OperandList+NumOperands; }
00122   inline const_op_iterator op_end()   const { return OperandList+NumOperands; }
00123   inline op_range operands() {
00124     return op_range(op_begin(), op_end());
00125   }
00126   inline const_op_range operands() const {
00127     return const_op_range(op_begin(), op_end());
00128   }
00129 
00130   /// Convenience iterator for directly iterating over the Values in the
00131   /// OperandList
00132   class value_op_iterator
00133       : public std::iterator<std::random_access_iterator_tag, Value *,
00134                              ptrdiff_t, Value *, Value *> {
00135     op_iterator OI;
00136   public:
00137     explicit value_op_iterator(Use *U = nullptr) : OI(U) {}
00138 
00139     bool operator==(const value_op_iterator &x) const {
00140       return OI == x.OI;
00141     }
00142     bool operator!=(const value_op_iterator &x) const {
00143       return !operator==(x);
00144     }
00145 
00146     value_op_iterator &operator+=(ptrdiff_t n) {
00147       OI += n;
00148       return *this;
00149     }
00150     value_op_iterator &operator-=(ptrdiff_t n) {
00151       OI -= n;
00152       return *this;
00153     }
00154     value_op_iterator operator+(ptrdiff_t n) const {
00155       return value_op_iterator(OI + n);
00156     }
00157     friend value_op_iterator operator+(ptrdiff_t n,
00158                                        const value_op_iterator &i) {
00159       return i + n;
00160     }
00161     value_op_iterator operator-(ptrdiff_t n) const {
00162       return value_op_iterator(OI - n);
00163     }
00164     ptrdiff_t operator-(const value_op_iterator &RHS) const {
00165       return OI - RHS.OI;
00166     }
00167     bool operator<(const value_op_iterator &RHS) const { return OI < RHS.OI; }
00168     bool operator>(const value_op_iterator &RHS) const { return OI > RHS.OI; }
00169     bool operator<=(const value_op_iterator &RHS) const { return OI <= RHS.OI; }
00170     bool operator>=(const value_op_iterator &RHS) const { return OI >= RHS.OI; }
00171     value_op_iterator &operator++() { return *this += 1; }
00172     value_op_iterator &operator--() { return *this -= 1; }
00173     value_op_iterator operator++(int) {
00174       value_op_iterator tmp = *this;
00175       ++*this;
00176       return tmp;
00177     }
00178     value_op_iterator operator--(int) {
00179       value_op_iterator tmp = *this;
00180       --*this;
00181       return tmp;
00182     }
00183 
00184     Value *operator*() const { return *OI; }
00185     Value *operator->() const { return operator*(); }
00186     Value *operator[](ptrdiff_t n) const { return *(*this + n); }
00187   };
00188 
00189   inline value_op_iterator value_op_begin() {
00190     return value_op_iterator(op_begin());
00191   }
00192   inline value_op_iterator value_op_end() {
00193     return value_op_iterator(op_end());
00194   }
00195   inline iterator_range<value_op_iterator> operand_values() {
00196     return iterator_range<value_op_iterator>(value_op_begin(), value_op_end());
00197   }
00198 
00199   // dropAllReferences() - This function is in charge of "letting go" of all
00200   // objects that this User refers to.  This allows one to
00201   // 'delete' a whole class at a time, even though there may be circular
00202   // references...  First all references are dropped, and all use counts go to
00203   // zero.  Then everything is deleted for real.  Note that no operations are
00204   // valid on an object that has "dropped all references", except operator
00205   // delete.
00206   //
00207   void dropAllReferences() {
00208     for (Use &U : operands())
00209       U.set(nullptr);
00210   }
00211 
00212   /// replaceUsesOfWith - Replaces all references to the "From" definition with
00213   /// references to the "To" definition.
00214   ///
00215   void replaceUsesOfWith(Value *From, Value *To);
00216 
00217   // Methods for support type inquiry through isa, cast, and dyn_cast:
00218   static inline bool classof(const Value *V) {
00219     return isa<Instruction>(V) || isa<Constant>(V);
00220   }
00221 };
00222 
00223 template<> struct simplify_type<User::op_iterator> {
00224   typedef Value* SimpleType;
00225   static SimpleType getSimplifiedValue(User::op_iterator &Val) {
00226     return Val->get();
00227   }
00228 };
00229 template<> struct simplify_type<User::const_op_iterator> {
00230   typedef /*const*/ Value* SimpleType;
00231   static SimpleType getSimplifiedValue(User::const_op_iterator &Val) {
00232     return Val->get();
00233   }
00234 };
00235 
00236 } // End llvm namespace
00237 
00238 #endif