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