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