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/ADT/iterator.h"
     23 #include "llvm/ADT/iterator_range.h"
     24 #include "llvm/IR/Value.h"
     25 #include "llvm/Support/AlignOf.h"
     26 #include "llvm/Support/ErrorHandling.h"
     27 
     28 namespace llvm {
     29 
     30 template <typename T> class ArrayRef;
     31 template <typename T> class MutableArrayRef;
     32 
     33 /// \brief Compile-time customization of User operands.
     34 ///
     35 /// Customizes operand-related allocators and accessors.
     36 template <class>
     37 struct OperandTraits;
     38 
     39 class User : public Value {
     40   User(const User &) = delete;
     41   template <unsigned>
     42   friend struct HungoffOperandTraits;
     43   virtual void anchor();
     44 
     45   LLVM_ATTRIBUTE_ALWAYS_INLINE inline static void *
     46   allocateFixedOperandUser(size_t, unsigned, unsigned);
     47 
     48 protected:
     49   /// Allocate a User with an operand pointer co-allocated.
     50   ///
     51   /// This is used for subclasses which need to allocate a variable number
     52   /// of operands, ie, 'hung off uses'.
     53   void *operator new(size_t Size);
     54 
     55   /// Allocate a User with the operands co-allocated.
     56   ///
     57   /// This is used for subclasses which have a fixed number of operands.
     58   void *operator new(size_t Size, unsigned Us);
     59 
     60   /// Allocate a User with the operands co-allocated.  If DescBytes is non-zero
     61   /// then allocate an additional DescBytes bytes before the operands. These
     62   /// bytes can be accessed by calling getDescriptor.
     63   ///
     64   /// DescBytes needs to be divisible by sizeof(void *).  The allocated
     65   /// descriptor, if any, is aligned to sizeof(void *) bytes.
     66   ///
     67   /// This is used for subclasses which have a fixed number of operands.
     68   void *operator new(size_t Size, unsigned Us, unsigned DescBytes);
     69 
     70   User(Type *ty, unsigned vty, Use *, unsigned NumOps)
     71       : Value(ty, vty) {
     72     assert(NumOps < (1u << NumUserOperandsBits) && "Too many operands");
     73     NumUserOperands = NumOps;
     74     // If we have hung off uses, then the operand list should initially be
     75     // null.
     76     assert((!HasHungOffUses || !getOperandList()) &&
     77            "Error in initializing hung off uses for User");
     78   }
     79 
     80   /// \brief Allocate the array of Uses, followed by a pointer
     81   /// (with bottom bit set) to the User.
     82   /// \param IsPhi identifies callers which are phi nodes and which need
     83   /// N BasicBlock* allocated along with N
     84   void allocHungoffUses(unsigned N, bool IsPhi = false);
     85 
     86   /// \brief Grow the number of hung off uses.  Note that allocHungoffUses
     87   /// should be called if there are no uses.
     88   void growHungoffUses(unsigned N, bool IsPhi = false);
     89 
     90 public:
     91   ~User() override {
     92   }
     93   /// \brief Free memory allocated for User and Use objects.
     94   void operator delete(void *Usr);
     95   /// \brief Placement delete - required by std, but never called.
     96   void operator delete(void*, unsigned) {
     97     llvm_unreachable("Constructor throws?");
     98   }
     99   /// \brief Placement delete - required by std, but never called.
    100   void operator delete(void*, unsigned, bool) {
    101     llvm_unreachable("Constructor throws?");
    102   }
    103 protected:
    104   template <int Idx, typename U> static Use &OpFrom(const U *that) {
    105     return Idx < 0
    106       ? OperandTraits<U>::op_end(const_cast<U*>(that))[Idx]
    107       : OperandTraits<U>::op_begin(const_cast<U*>(that))[Idx];
    108   }
    109   template <int Idx> Use &Op() {
    110     return OpFrom<Idx>(this);
    111   }
    112   template <int Idx> const Use &Op() const {
    113     return OpFrom<Idx>(this);
    114   }
    115 private:
    116   Use *&getHungOffOperands() { return *(reinterpret_cast<Use **>(this) - 1); }
    117 
    118   Use *getIntrusiveOperands() {
    119     return reinterpret_cast<Use *>(this) - NumUserOperands;
    120   }
    121 
    122   void setOperandList(Use *NewList) {
    123     assert(HasHungOffUses &&
    124            "Setting operand list only required for hung off uses");
    125     getHungOffOperands() = NewList;
    126   }
    127 public:
    128   Use *getOperandList() {
    129     return HasHungOffUses ? getHungOffOperands() : getIntrusiveOperands();
    130   }
    131   const Use *getOperandList() const {
    132     return const_cast<User *>(this)->getOperandList();
    133   }
    134   Value *getOperand(unsigned i) const {
    135     assert(i < NumUserOperands && "getOperand() out of range!");
    136     return getOperandList()[i];
    137   }
    138   void setOperand(unsigned i, Value *Val) {
    139     assert(i < NumUserOperands && "setOperand() out of range!");
    140     assert((!isa<Constant>((const Value*)this) ||
    141             isa<GlobalValue>((const Value*)this)) &&
    142            "Cannot mutate a constant with setOperand!");
    143     getOperandList()[i] = Val;
    144   }
    145   const Use &getOperandUse(unsigned i) const {
    146     assert(i < NumUserOperands && "getOperandUse() out of range!");
    147     return getOperandList()[i];
    148   }
    149   Use &getOperandUse(unsigned i) {
    150     assert(i < NumUserOperands && "getOperandUse() out of range!");
    151     return getOperandList()[i];
    152   }
    153 
    154   unsigned getNumOperands() const { return NumUserOperands; }
    155 
    156   /// Returns the descriptor co-allocated with this User instance.
    157   ArrayRef<const uint8_t> getDescriptor() const;
    158 
    159   /// Returns the descriptor co-allocated with this User instance.
    160   MutableArrayRef<uint8_t> getDescriptor();
    161 
    162   /// Set the number of operands on a GlobalVariable.
    163   ///
    164   /// GlobalVariable always allocates space for a single operands, but
    165   /// doesn't always use it.
    166   ///
    167   /// FIXME: As that the number of operands is used to find the start of
    168   /// the allocated memory in operator delete, we need to always think we have
    169   /// 1 operand before delete.
    170   void setGlobalVariableNumOperands(unsigned NumOps) {
    171     assert(NumOps <= 1 && "GlobalVariable can only have 0 or 1 operands");
    172     NumUserOperands = NumOps;
    173   }
    174 
    175   /// \brief Subclasses with hung off uses need to manage the operand count
    176   /// themselves.  In these instances, the operand count isn't used to find the
    177   /// OperandList, so there's no issue in having the operand count change.
    178   void setNumHungOffUseOperands(unsigned NumOps) {
    179     assert(HasHungOffUses && "Must have hung off uses to use this method");
    180     assert(NumOps < (1u << NumUserOperandsBits) && "Too many operands");
    181     NumUserOperands = NumOps;
    182   }
    183 
    184   // ---------------------------------------------------------------------------
    185   // Operand Iterator interface...
    186   //
    187   typedef Use*       op_iterator;
    188   typedef const Use* const_op_iterator;
    189   typedef iterator_range<op_iterator> op_range;
    190   typedef iterator_range<const_op_iterator> const_op_range;
    191 
    192   op_iterator       op_begin()       { return getOperandList(); }
    193   const_op_iterator op_begin() const { return getOperandList(); }
    194   op_iterator       op_end()         {
    195     return getOperandList() + NumUserOperands;
    196   }
    197   const_op_iterator op_end()   const {
    198     return getOperandList() + NumUserOperands;
    199   }
    200   op_range operands() {
    201     return op_range(op_begin(), op_end());
    202   }
    203   const_op_range operands() const {
    204     return const_op_range(op_begin(), op_end());
    205   }
    206 
    207   /// \brief Iterator for directly iterating over the operand Values.
    208   struct value_op_iterator
    209       : iterator_adaptor_base<value_op_iterator, op_iterator,
    210                               std::random_access_iterator_tag, Value *,
    211                               ptrdiff_t, Value *, Value *> {
    212     explicit value_op_iterator(Use *U = nullptr) : iterator_adaptor_base(U) {}
    213 
    214     Value *operator*() const { return *I; }
    215     Value *operator->() const { return operator*(); }
    216   };
    217 
    218   value_op_iterator value_op_begin() {
    219     return value_op_iterator(op_begin());
    220   }
    221   value_op_iterator value_op_end() {
    222     return value_op_iterator(op_end());
    223   }
    224   iterator_range<value_op_iterator> operand_values() {
    225     return make_range(value_op_begin(), value_op_end());
    226   }
    227 
    228   /// \brief Drop all references to operands.
    229   ///
    230   /// This function is in charge of "letting go" of all objects that this User
    231   /// refers to.  This allows one to 'delete' a whole class at a time, even
    232   /// though there may be circular references...  First all references are
    233   /// dropped, and all use counts go to zero.  Then everything is deleted for
    234   /// real.  Note that no operations are valid on an object that has "dropped
    235   /// all references", except operator delete.
    236   void dropAllReferences() {
    237     for (Use &U : operands())
    238       U.set(nullptr);
    239   }
    240 
    241   /// \brief Replace uses of one Value with another.
    242   ///
    243   /// Replaces all references to the "From" definition with references to the
    244   /// "To" definition.
    245   void replaceUsesOfWith(Value *From, Value *To);
    246 
    247   // Methods for support type inquiry through isa, cast, and dyn_cast:
    248   static inline bool classof(const Value *V) {
    249     return isa<Instruction>(V) || isa<Constant>(V);
    250   }
    251 };
    252 // Either Use objects, or a Use pointer can be prepended to User.
    253 static_assert(AlignOf<Use>::Alignment >= AlignOf<User>::Alignment,
    254               "Alignment is insufficient after objects prepended to User");
    255 static_assert(AlignOf<Use *>::Alignment >= AlignOf<User>::Alignment,
    256               "Alignment is insufficient after objects prepended to User");
    257 
    258 template<> struct simplify_type<User::op_iterator> {
    259   typedef Value* SimpleType;
    260   static SimpleType getSimplifiedValue(User::op_iterator &Val) {
    261     return Val->get();
    262   }
    263 };
    264 template<> struct simplify_type<User::const_op_iterator> {
    265   typedef /*const*/ Value* SimpleType;
    266   static SimpleType getSimplifiedValue(User::const_op_iterator &Val) {
    267     return Val->get();
    268   }
    269 };
    270 
    271 } // End llvm namespace
    272 
    273 #endif
    274