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