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