Home | History | Annotate | Download | only in llvm
      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 'use's 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 User's.
     15 //  * Constants may be users of other constants (think arrays and stuff)
     16 //
     17 //===----------------------------------------------------------------------===//
     18 
     19 #ifndef LLVM_USER_H
     20 #define LLVM_USER_H
     21 
     22 #include "llvm/Value.h"
     23 
     24 namespace llvm {
     25 
     26 /// OperandTraits - Compile-time customization of
     27 /// operand-related allocators and accessors
     28 /// for use of the User class
     29 template <class>
     30 struct OperandTraits;
     31 
     32 class User : public Value {
     33   User(const User &);             // Do not implement
     34   void *operator new(size_t);     // Do not implement
     35   template <unsigned>
     36   friend struct HungoffOperandTraits;
     37 protected:
     38   /// OperandList - This is a pointer to the array of Uses for this User.
     39   /// For nodes of fixed arity (e.g. a binary operator) this array will live
     40   /// prefixed to some derived class instance.  For nodes of resizable variable
     41   /// arity (e.g. PHINodes, SwitchInst etc.), this memory will be dynamically
     42   /// allocated and should be destroyed by the classes' virtual dtor.
     43   Use *OperandList;
     44 
     45   /// NumOperands - The number of values used by this User.
     46   ///
     47   unsigned NumOperands;
     48 
     49   void *operator new(size_t s, unsigned Us);
     50   User(Type *ty, unsigned vty, Use *OpList, unsigned NumOps)
     51     : Value(ty, vty), OperandList(OpList), NumOperands(NumOps) {}
     52   Use *allocHungoffUses(unsigned) const;
     53   void dropHungoffUses() {
     54     Use::zap(OperandList, OperandList + NumOperands, true);
     55     OperandList = 0;
     56     // Reset NumOperands so User::operator delete() does the right thing.
     57     NumOperands = 0;
     58   }
     59 public:
     60   ~User() {
     61     Use::zap(OperandList, OperandList + NumOperands);
     62   }
     63   /// operator delete - free memory allocated for User and Use objects
     64   void operator delete(void *Usr);
     65   /// placement delete - required by std, but never called.
     66   void operator delete(void*, unsigned) {
     67     assert(0 && "Constructor throws?");
     68   }
     69   /// placement delete - required by std, but never called.
     70   void operator delete(void*, unsigned, bool) {
     71     assert(0 && "Constructor throws?");
     72   }
     73 protected:
     74   template <int Idx, typename U> static Use &OpFrom(const U *that) {
     75     return Idx < 0
     76       ? OperandTraits<U>::op_end(const_cast<U*>(that))[Idx]
     77       : OperandTraits<U>::op_begin(const_cast<U*>(that))[Idx];
     78   }
     79   template <int Idx> Use &Op() {
     80     return OpFrom<Idx>(this);
     81   }
     82   template <int Idx> const Use &Op() const {
     83     return OpFrom<Idx>(this);
     84   }
     85 public:
     86   Value *getOperand(unsigned i) const {
     87     assert(i < NumOperands && "getOperand() out of range!");
     88     return OperandList[i];
     89   }
     90   void setOperand(unsigned i, Value *Val) {
     91     assert(i < NumOperands && "setOperand() out of range!");
     92     assert((!isa<Constant>((const Value*)this) ||
     93             isa<GlobalValue>((const Value*)this)) &&
     94            "Cannot mutate a constant with setOperand!");
     95     OperandList[i] = Val;
     96   }
     97   const Use &getOperandUse(unsigned i) const {
     98     assert(i < NumOperands && "getOperandUse() out of range!");
     99     return OperandList[i];
    100   }
    101   Use &getOperandUse(unsigned i) {
    102     assert(i < NumOperands && "getOperandUse() out of range!");
    103     return OperandList[i];
    104   }
    105 
    106   unsigned getNumOperands() const { return NumOperands; }
    107 
    108   // ---------------------------------------------------------------------------
    109   // Operand Iterator interface...
    110   //
    111   typedef Use*       op_iterator;
    112   typedef const Use* const_op_iterator;
    113 
    114   inline op_iterator       op_begin()       { return OperandList; }
    115   inline const_op_iterator op_begin() const { return OperandList; }
    116   inline op_iterator       op_end()         { return OperandList+NumOperands; }
    117   inline const_op_iterator op_end()   const { return OperandList+NumOperands; }
    118 
    119   // dropAllReferences() - This function is in charge of "letting go" of all
    120   // objects that this User refers to.  This allows one to
    121   // 'delete' a whole class at a time, even though there may be circular
    122   // references...  First all references are dropped, and all use counts go to
    123   // zero.  Then everything is deleted for real.  Note that no operations are
    124   // valid on an object that has "dropped all references", except operator
    125   // delete.
    126   //
    127   void dropAllReferences() {
    128     for (op_iterator i = op_begin(), e = op_end(); i != e; ++i)
    129       i->set(0);
    130   }
    131 
    132   /// replaceUsesOfWith - Replaces all references to the "From" definition with
    133   /// references to the "To" definition.
    134   ///
    135   void replaceUsesOfWith(Value *From, Value *To);
    136 
    137   // Methods for support type inquiry through isa, cast, and dyn_cast:
    138   static inline bool classof(const User *) { return true; }
    139   static inline bool classof(const Value *V) {
    140     return isa<Instruction>(V) || isa<Constant>(V);
    141   }
    142 };
    143 
    144 template<> struct simplify_type<User::op_iterator> {
    145   typedef Value* SimpleType;
    146 
    147   static SimpleType getSimplifiedValue(const User::op_iterator &Val) {
    148     return static_cast<SimpleType>(Val->get());
    149   }
    150 };
    151 
    152 template<> struct simplify_type<const User::op_iterator>
    153   : public simplify_type<User::op_iterator> {};
    154 
    155 template<> struct simplify_type<User::const_op_iterator> {
    156   typedef Value* SimpleType;
    157 
    158   static SimpleType getSimplifiedValue(const User::const_op_iterator &Val) {
    159     return static_cast<SimpleType>(Val->get());
    160   }
    161 };
    162 
    163 template<> struct simplify_type<const User::const_op_iterator>
    164   : public simplify_type<User::const_op_iterator> {};
    165 
    166 
    167 // value_use_iterator::getOperandNo - Requires the definition of the User class.
    168 template<typename UserTy>
    169 unsigned value_use_iterator<UserTy>::getOperandNo() const {
    170   return U - U->getUser()->op_begin();
    171 }
    172 
    173 } // End llvm namespace
    174 
    175 #endif
    176