LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

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     OperandList[i] = Val;
00099   }
00100   unsigned getNumOperands() const { return NumOperands; }
00101 
00102   // ---------------------------------------------------------------------------
00103   // Operand Iterator interface...
00104   //
00105   typedef Use*       op_iterator;
00106   typedef const Use* const_op_iterator;
00107 
00108   inline op_iterator       op_begin()       { return OperandList; }
00109   inline const_op_iterator op_begin() const { return OperandList; }
00110   inline op_iterator       op_end()         { return OperandList+NumOperands; }
00111   inline const_op_iterator op_end()   const { return OperandList+NumOperands; }
00112 
00113   // dropAllReferences() - This function is in charge of "letting go" of all
00114   // objects that this User refers to.  This allows one to
00115   // 'delete' a whole class at a time, even though there may be circular
00116   // references...  First all references are dropped, and all use counts go to
00117   // zero.  Then everything is deleted for real.  Note that no operations are
00118   // valid on an object that has "dropped all references", except operator
00119   // delete.
00120   //
00121   void dropAllReferences() {
00122     for (op_iterator i = op_begin(), e = op_end(); i != e; ++i)
00123       i->set(0);
00124   }
00125 
00126   /// replaceUsesOfWith - Replaces all references to the "From" definition with
00127   /// references to the "To" definition.
00128   ///
00129   void replaceUsesOfWith(Value *From, Value *To);
00130 
00131   // Methods for support type inquiry through isa, cast, and dyn_cast:
00132   static inline bool classof(const User *) { return true; }
00133   static inline bool classof(const Value *V) {
00134     return isa<Instruction>(V) || isa<Constant>(V);
00135   }
00136 };
00137 
00138 inline Use *OperandTraits<User>::op_begin(User *U) {
00139   return U->op_begin();
00140 }
00141 
00142 inline Use *OperandTraits<User>::op_end(User *U) {
00143   return U->op_end();
00144 }
00145 
00146 inline unsigned OperandTraits<User>::operands(const User *U) {
00147   return U->getNumOperands();
00148 }
00149 
00150 template<> struct simplify_type<User::op_iterator> {
00151   typedef Value* SimpleType;
00152 
00153   static SimpleType getSimplifiedValue(const User::op_iterator &Val) {
00154     return static_cast<SimpleType>(Val->get());
00155   }
00156 };
00157 
00158 template<> struct simplify_type<const User::op_iterator>
00159   : public simplify_type<User::op_iterator> {};
00160 
00161 template<> struct simplify_type<User::const_op_iterator> {
00162   typedef Value* SimpleType;
00163 
00164   static SimpleType getSimplifiedValue(const User::const_op_iterator &Val) {
00165     return static_cast<SimpleType>(Val->get());
00166   }
00167 };
00168 
00169 template<> struct simplify_type<const User::const_op_iterator>
00170   : public simplify_type<User::const_op_iterator> {};
00171 
00172 
00173 // value_use_iterator::getOperandNo - Requires the definition of the User class.
00174 template<typename UserTy>
00175 unsigned value_use_iterator<UserTy>::getOperandNo() const {
00176   return U - U->getUser()->op_begin();
00177 }
00178 
00179 } // End llvm namespace
00180 
00181 #endif



This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.