LLVM  mainline
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 uses 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 Users.
00015 //  * Constants may be users of other constants (think arrays and stuff)
00016 //
00017 //===----------------------------------------------------------------------===//
00018 
00019 #ifndef LLVM_IR_USER_H
00020 #define LLVM_IR_USER_H
00021 
00022 #include "llvm/ADT/ArrayRef.h"
00023 #include "llvm/ADT/iterator.h"
00024 #include "llvm/ADT/iterator_range.h"
00025 #include "llvm/IR/Value.h"
00026 #include "llvm/Support/AlignOf.h"
00027 #include "llvm/Support/ErrorHandling.h"
00028 
00029 namespace llvm {
00030 
00031 /// \brief Compile-time customization of User operands.
00032 ///
00033 /// Customizes operand-related allocators and accessors.
00034 template <class>
00035 struct OperandTraits;
00036 
00037 class User : public Value {
00038   User(const User &) = delete;
00039   template <unsigned>
00040   friend struct HungoffOperandTraits;
00041   virtual void anchor();
00042 
00043   LLVM_ATTRIBUTE_ALWAYS_INLINE inline static void *
00044   allocateFixedOperandUser(size_t, unsigned, unsigned);
00045 
00046 protected:
00047   /// Allocate a User with an operand pointer co-allocated.
00048   ///
00049   /// This is used for subclasses which need to allocate a variable number
00050   /// of operands, ie, 'hung off uses'.
00051   void *operator new(size_t Size);
00052 
00053   /// Allocate a User with the operands co-allocated.
00054   ///
00055   /// This is used for subclasses which have a fixed number of operands.
00056   void *operator new(size_t Size, unsigned Us);
00057 
00058   /// Allocate a User with the operands co-allocated.  If DescBytes is non-zero
00059   /// then allocate an additional DescBytes bytes before the operands. These
00060   /// bytes can be accessed by calling getDescriptor.
00061   ///
00062   /// DescBytes needs to be divisible by sizeof(void *).  The allocated
00063   /// descriptor, if any, is aligned to sizeof(void *) bytes.
00064   ///
00065   /// This is used for subclasses which have a fixed number of operands.
00066   void *operator new(size_t Size, unsigned Us, unsigned DescBytes);
00067 
00068   User(Type *ty, unsigned vty, Use *, unsigned NumOps)
00069       : Value(ty, vty) {
00070     assert(NumOps < (1u << NumUserOperandsBits) && "Too many operands");
00071     NumUserOperands = NumOps;
00072     // If we have hung off uses, then the operand list should initially be
00073     // null.
00074     assert((!HasHungOffUses || !getOperandList()) &&
00075            "Error in initializing hung off uses for User");
00076   }
00077 
00078   /// \brief Allocate the array of Uses, followed by a pointer
00079   /// (with bottom bit set) to the User.
00080   /// \param IsPhi identifies callers which are phi nodes and which need
00081   /// N BasicBlock* allocated along with N
00082   void allocHungoffUses(unsigned N, bool IsPhi = false);
00083 
00084   /// \brief Grow the number of hung off uses.  Note that allocHungoffUses
00085   /// should be called if there are no uses.
00086   void growHungoffUses(unsigned N, bool IsPhi = false);
00087 
00088 public:
00089   ~User() override {
00090   }
00091   /// \brief Free memory allocated for User and Use objects.
00092   void operator delete(void *Usr);
00093   /// \brief Placement delete - required by std, but never called.
00094   void operator delete(void*, unsigned) {
00095     llvm_unreachable("Constructor throws?");
00096   }
00097   /// \brief Placement delete - required by std, but never called.
00098   void operator delete(void*, unsigned, bool) {
00099     llvm_unreachable("Constructor throws?");
00100   }
00101 protected:
00102   template <int Idx, typename U> static Use &OpFrom(const U *that) {
00103     return Idx < 0
00104       ? OperandTraits<U>::op_end(const_cast<U*>(that))[Idx]
00105       : OperandTraits<U>::op_begin(const_cast<U*>(that))[Idx];
00106   }
00107   template <int Idx> Use &Op() {
00108     return OpFrom<Idx>(this);
00109   }
00110   template <int Idx> const Use &Op() const {
00111     return OpFrom<Idx>(this);
00112   }
00113 private:
00114   Use *&getHungOffOperands() { return *(reinterpret_cast<Use **>(this) - 1); }
00115 
00116   Use *getIntrusiveOperands() {
00117     return reinterpret_cast<Use *>(this) - NumUserOperands;
00118   }
00119 
00120   void setOperandList(Use *NewList) {
00121     assert(HasHungOffUses &&
00122            "Setting operand list only required for hung off uses");
00123     getHungOffOperands() = NewList;
00124   }
00125 public:
00126   Use *getOperandList() {
00127     return HasHungOffUses ? getHungOffOperands() : getIntrusiveOperands();
00128   }
00129   const Use *getOperandList() const {
00130     return const_cast<User *>(this)->getOperandList();
00131   }
00132   Value *getOperand(unsigned i) const {
00133     assert(i < NumUserOperands && "getOperand() out of range!");
00134     return getOperandList()[i];
00135   }
00136   void setOperand(unsigned i, Value *Val) {
00137     assert(i < NumUserOperands && "setOperand() out of range!");
00138     assert((!isa<Constant>((const Value*)this) ||
00139             isa<GlobalValue>((const Value*)this)) &&
00140            "Cannot mutate a constant with setOperand!");
00141     getOperandList()[i] = Val;
00142   }
00143   const Use &getOperandUse(unsigned i) const {
00144     assert(i < NumUserOperands && "getOperandUse() out of range!");
00145     return getOperandList()[i];
00146   }
00147   Use &getOperandUse(unsigned i) {
00148     assert(i < NumUserOperands && "getOperandUse() out of range!");
00149     return getOperandList()[i];
00150   }
00151 
00152   unsigned getNumOperands() const { return NumUserOperands; }
00153 
00154   /// Returns the descriptor co-allocated with this User instance.
00155   ArrayRef<const uint8_t> getDescriptor() const;
00156 
00157   /// Returns the descriptor co-allocated with this User instance.
00158   MutableArrayRef<uint8_t> getDescriptor();
00159 
00160   /// Set the number of operands on a GlobalVariable.
00161   ///
00162   /// GlobalVariable always allocates space for a single operands, but
00163   /// doesn't always use it.
00164   ///
00165   /// FIXME: As that the number of operands is used to find the start of
00166   /// the allocated memory in operator delete, we need to always think we have
00167   /// 1 operand before delete.
00168   void setGlobalVariableNumOperands(unsigned NumOps) {
00169     assert(NumOps <= 1 && "GlobalVariable can only have 0 or 1 operands");
00170     NumUserOperands = NumOps;
00171   }
00172 
00173   /// \brief Subclasses with hung off uses need to manage the operand count
00174   /// themselves.  In these instances, the operand count isn't used to find the
00175   /// OperandList, so there's no issue in having the operand count change.
00176   void setNumHungOffUseOperands(unsigned NumOps) {
00177     assert(HasHungOffUses && "Must have hung off uses to use this method");
00178     assert(NumOps < (1u << NumUserOperandsBits) && "Too many operands");
00179     NumUserOperands = NumOps;
00180   }
00181 
00182   // ---------------------------------------------------------------------------
00183   // Operand Iterator interface...
00184   //
00185   typedef Use*       op_iterator;
00186   typedef const Use* const_op_iterator;
00187   typedef iterator_range<op_iterator> op_range;
00188   typedef iterator_range<const_op_iterator> const_op_range;
00189 
00190   op_iterator       op_begin()       { return getOperandList(); }
00191   const_op_iterator op_begin() const { return getOperandList(); }
00192   op_iterator       op_end()         {
00193     return getOperandList() + NumUserOperands;
00194   }
00195   const_op_iterator op_end()   const {
00196     return getOperandList() + NumUserOperands;
00197   }
00198   op_range operands() {
00199     return op_range(op_begin(), op_end());
00200   }
00201   const_op_range operands() const {
00202     return const_op_range(op_begin(), op_end());
00203   }
00204 
00205   /// \brief Iterator for directly iterating over the operand Values.
00206   struct value_op_iterator
00207       : iterator_adaptor_base<value_op_iterator, op_iterator,
00208                               std::random_access_iterator_tag, Value *,
00209                               ptrdiff_t, Value *, Value *> {
00210     explicit value_op_iterator(Use *U = nullptr) : iterator_adaptor_base(U) {}
00211 
00212     Value *operator*() const { return *I; }
00213     Value *operator->() const { return operator*(); }
00214   };
00215 
00216   value_op_iterator value_op_begin() {
00217     return value_op_iterator(op_begin());
00218   }
00219   value_op_iterator value_op_end() {
00220     return value_op_iterator(op_end());
00221   }
00222   iterator_range<value_op_iterator> operand_values() {
00223     return make_range(value_op_begin(), value_op_end());
00224   }
00225 
00226   /// \brief Drop all references to operands.
00227   ///
00228   /// This function is in charge of "letting go" of all objects that this User
00229   /// refers to.  This allows one to 'delete' a whole class at a time, even
00230   /// though there may be circular references...  First all references are
00231   /// dropped, and all use counts go to zero.  Then everything is deleted for
00232   /// real.  Note that no operations are valid on an object that has "dropped
00233   /// all references", except operator delete.
00234   void dropAllReferences() {
00235     for (Use &U : operands())
00236       U.set(nullptr);
00237   }
00238 
00239   /// \brief Replace uses of one Value with another.
00240   ///
00241   /// Replaces all references to the "From" definition with references to the
00242   /// "To" definition.
00243   void replaceUsesOfWith(Value *From, Value *To);
00244 
00245   // Methods for support type inquiry through isa, cast, and dyn_cast:
00246   static inline bool classof(const Value *V) {
00247     return isa<Instruction>(V) || isa<Constant>(V);
00248   }
00249 };
00250 // Either Use objects, or a Use pointer can be prepended to User.
00251 static_assert(AlignOf<Use>::Alignment >= AlignOf<User>::Alignment,
00252               "Alignment is insufficient after objects prepended to User");
00253 static_assert(AlignOf<Use *>::Alignment >= AlignOf<User>::Alignment,
00254               "Alignment is insufficient after objects prepended to User");
00255 
00256 template<> struct simplify_type<User::op_iterator> {
00257   typedef Value* SimpleType;
00258   static SimpleType getSimplifiedValue(User::op_iterator &Val) {
00259     return Val->get();
00260   }
00261 };
00262 template<> struct simplify_type<User::const_op_iterator> {
00263   typedef /*const*/ Value* SimpleType;
00264   static SimpleType getSimplifiedValue(User::const_op_iterator &Val) {
00265     return Val->get();
00266   }
00267 };
00268 
00269 } // End llvm namespace
00270 
00271 #endif