Home | History | Annotate | Download | only in IR
      1 //===-- llvm/User.h - User class definition ---------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This class defines the interface that one who uses a Value must implement.
     11 // Each instance of the Value class keeps track of what User's have handles
     12 // to it.
     13 //
     14 //  * Instructions are the largest class of Users.
     15 //  * Constants may be users of other constants (think arrays and stuff)
     16 //
     17 //===----------------------------------------------------------------------===//
     18 
     19 #ifndef LLVM_IR_USER_H
     20 #define LLVM_IR_USER_H
     21 
     22 #include "llvm/IR/Value.h"
     23 #include "llvm/Support/ErrorHandling.h"
     24 
     25 namespace llvm {
     26 
     27 /// OperandTraits - Compile-time customization of
     28 /// operand-related allocators and accessors
     29 /// for use of the User class
     30 template <class>
     31 struct OperandTraits;
     32 
     33 class User : public Value {
     34   User(const User &) LLVM_DELETED_FUNCTION;
     35   void *operator new(size_t) LLVM_DELETED_FUNCTION;
     36   template <unsigned>
     37   friend struct HungoffOperandTraits;
     38   virtual void anchor();
     39 protected:
     40   /// OperandList - This is a pointer to the array of Uses for this User.
     41   /// For nodes of fixed arity (e.g. a binary operator) this array will live
     42   /// prefixed to some derived class instance.  For nodes of resizable variable
     43   /// arity (e.g. PHINodes, SwitchInst etc.), this memory will be dynamically
     44   /// allocated and should be destroyed by the classes' virtual dtor.
     45   Use *OperandList;
     46 
     47   /// NumOperands - The number of values used by this User.
     48   ///
     49   unsigned NumOperands;
     50 
     51   void *operator new(size_t s, unsigned Us);
     52   User(Type *ty, unsigned vty, Use *OpList, unsigned NumOps)
     53     : Value(ty, vty), OperandList(OpList), NumOperands(NumOps) {}
     54   Use *allocHungoffUses(unsigned) const;
     55   void dropHungoffUses() {
     56     Use::zap(OperandList, OperandList + NumOperands, true);
     57     OperandList = 0;
     58     // Reset NumOperands so User::operator delete() does the right thing.
     59     NumOperands = 0;
     60   }
     61 public:
     62   ~User() {
     63     Use::zap(OperandList, OperandList + NumOperands);
     64   }
     65   /// operator delete - free memory allocated for User and Use objects
     66   void operator delete(void *Usr);
     67   /// placement delete - required by std, but never called.
     68   void operator delete(void*, unsigned) {
     69     llvm_unreachable("Constructor throws?");
     70   }
     71   /// placement delete - required by std, but never called.
     72   void operator delete(void*, unsigned, bool) {
     73     llvm_unreachable("Constructor throws?");
     74   }
     75 protected:
     76   template <int Idx, typename U> static Use &OpFrom(const U *that) {
     77     return Idx < 0
     78       ? OperandTraits<U>::op_end(const_cast<U*>(that))[Idx]
     79       : OperandTraits<U>::op_begin(const_cast<U*>(that))[Idx];
     80   }
     81   template <int Idx> Use &Op() {
     82     return OpFrom<Idx>(this);
     83   }
     84   template <int Idx> const Use &Op() const {
     85     return OpFrom<Idx>(this);
     86   }
     87 public:
     88   Value *getOperand(unsigned i) const {
     89     assert(i < NumOperands && "getOperand() out of range!");
     90     return OperandList[i];
     91   }
     92   void setOperand(unsigned i, Value *Val) {
     93     assert(i < NumOperands && "setOperand() out of range!");
     94     assert((!isa<Constant>((const Value*)this) ||
     95             isa<GlobalValue>((const Value*)this)) &&
     96            "Cannot mutate a constant with setOperand!");
     97     OperandList[i] = Val;
     98   }
     99   const Use &getOperandUse(unsigned i) const {
    100     assert(i < NumOperands && "getOperandUse() out of range!");
    101     return OperandList[i];
    102   }
    103   Use &getOperandUse(unsigned i) {
    104     assert(i < NumOperands && "getOperandUse() out of range!");
    105     return OperandList[i];
    106   }
    107 
    108   unsigned getNumOperands() const { return NumOperands; }
    109 
    110   // ---------------------------------------------------------------------------
    111   // Operand Iterator interface...
    112   //
    113   typedef Use*       op_iterator;
    114   typedef const Use* const_op_iterator;
    115 
    116   inline op_iterator       op_begin()       { return OperandList; }
    117   inline const_op_iterator op_begin() const { return OperandList; }
    118   inline op_iterator       op_end()         { return OperandList+NumOperands; }
    119   inline const_op_iterator op_end()   const { return OperandList+NumOperands; }
    120 
    121   /// Convenience iterator for directly iterating over the Values in the
    122   /// OperandList
    123   class value_op_iterator : public std::iterator<std::forward_iterator_tag,
    124                                                  Value*> {
    125     op_iterator OI;
    126   public:
    127     explicit value_op_iterator(Use *U) : OI(U) {}
    128 
    129     bool operator==(const value_op_iterator &x) const {
    130       return OI == x.OI;
    131     }
    132     bool operator!=(const value_op_iterator &x) const {
    133       return !operator==(x);
    134     }
    135 
    136     /// Iterator traversal: forward iteration only
    137     value_op_iterator &operator++() {          // Preincrement
    138       ++OI;
    139       return *this;
    140     }
    141     value_op_iterator operator++(int) {        // Postincrement
    142       value_op_iterator tmp = *this; ++*this; return tmp;
    143     }
    144 
    145     /// Retrieve a pointer to the current Value.
    146     Value *operator*() const {
    147       return *OI;
    148     }
    149 
    150     Value *operator->() const { return operator*(); }
    151   };
    152 
    153   inline value_op_iterator value_op_begin() {
    154     return value_op_iterator(op_begin());
    155   }
    156   inline value_op_iterator value_op_end() {
    157     return value_op_iterator(op_end());
    158   }
    159 
    160   // dropAllReferences() - This function is in charge of "letting go" of all
    161   // objects that this User refers to.  This allows one to
    162   // 'delete' a whole class at a time, even though there may be circular
    163   // references...  First all references are dropped, and all use counts go to
    164   // zero.  Then everything is deleted for real.  Note that no operations are
    165   // valid on an object that has "dropped all references", except operator
    166   // delete.
    167   //
    168   void dropAllReferences() {
    169     for (op_iterator i = op_begin(), e = op_end(); i != e; ++i)
    170       i->set(0);
    171   }
    172 
    173   /// replaceUsesOfWith - Replaces all references to the "From" definition with
    174   /// references to the "To" definition.
    175   ///
    176   void replaceUsesOfWith(Value *From, Value *To);
    177 
    178   // Methods for support type inquiry through isa, cast, and dyn_cast:
    179   static inline bool classof(const Value *V) {
    180     return isa<Instruction>(V) || isa<Constant>(V);
    181   }
    182 };
    183 
    184 template<> struct simplify_type<User::op_iterator> {
    185   typedef Value* SimpleType;
    186   static SimpleType getSimplifiedValue(User::op_iterator &Val) {
    187     return Val->get();
    188   }
    189 };
    190 template<> struct simplify_type<User::const_op_iterator> {
    191   typedef /*const*/ Value* SimpleType;
    192   static SimpleType getSimplifiedValue(User::const_op_iterator &Val) {
    193     return Val->get();
    194   }
    195 };
    196 
    197 // value_use_iterator::getOperandNo - Requires the definition of the User class.
    198 template<typename UserTy>
    199 unsigned value_use_iterator<UserTy>::getOperandNo() const {
    200   return U - U->getUser()->op_begin();
    201 }
    202 
    203 } // End llvm namespace
    204 
    205 #endif
    206