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/ErrorHandling.h"
00026 
00027 namespace llvm {
00028 
00029 /// \brief Compile-time customization of User operands.
00030 ///
00031 /// Customizes operand-related allocators and accessors.
00032 template <class>
00033 struct OperandTraits;
00034 
00035 class User : public Value {
00036   User(const User &) = delete;
00037   void *operator new(size_t) = delete;
00038   template <unsigned>
00039   friend struct HungoffOperandTraits;
00040   virtual void anchor();
00041 protected:
00042   /// \brief This is a pointer to the array of Uses for this User.
00043   ///
00044   /// For nodes of fixed arity (e.g. a binary operator) this array will live
00045   /// prefixed to some derived class instance.  For nodes of resizable variable
00046   /// arity (e.g. PHINodes, SwitchInst etc.), this memory will be dynamically
00047   /// allocated and should be destroyed by the classes' virtual dtor.
00048   Use *OperandList;
00049 
00050   void *operator new(size_t s, unsigned Us);
00051   User(Type *ty, unsigned vty, Use *OpList, unsigned NumOps)
00052       : Value(ty, vty), OperandList(OpList) {
00053     NumOperands = NumOps;
00054   }
00055   Use *allocHungoffUses(unsigned) const;
00056   void dropHungoffUses() {
00057     Use::zap(OperandList, OperandList + NumOperands, true);
00058     OperandList = nullptr;
00059     // Reset NumOperands so User::operator delete() does the right thing.
00060     NumOperands = 0;
00061   }
00062 public:
00063   ~User() override { Use::zap(OperandList, OperandList + NumOperands); }
00064   /// \brief Free memory allocated for User and Use objects.
00065   void operator delete(void *Usr);
00066   /// \brief Placement delete - required by std, but never called.
00067   void operator delete(void*, unsigned) {
00068     llvm_unreachable("Constructor throws?");
00069   }
00070   /// \brief Placement delete - required by std, but never called.
00071   void operator delete(void*, unsigned, bool) {
00072     llvm_unreachable("Constructor throws?");
00073   }
00074 protected:
00075   template <int Idx, typename U> static Use &OpFrom(const U *that) {
00076     return Idx < 0
00077       ? OperandTraits<U>::op_end(const_cast<U*>(that))[Idx]
00078       : OperandTraits<U>::op_begin(const_cast<U*>(that))[Idx];
00079   }
00080   template <int Idx> Use &Op() {
00081     return OpFrom<Idx>(this);
00082   }
00083   template <int Idx> const Use &Op() const {
00084     return OpFrom<Idx>(this);
00085   }
00086 public:
00087   Value *getOperand(unsigned i) const {
00088     assert(i < NumOperands && "getOperand() out of range!");
00089     return OperandList[i];
00090   }
00091   void setOperand(unsigned i, Value *Val) {
00092     assert(i < NumOperands && "setOperand() out of range!");
00093     assert((!isa<Constant>((const Value*)this) ||
00094             isa<GlobalValue>((const Value*)this)) &&
00095            "Cannot mutate a constant with setOperand!");
00096     OperandList[i] = Val;
00097   }
00098   const Use &getOperandUse(unsigned i) const {
00099     assert(i < NumOperands && "getOperandUse() out of range!");
00100     return OperandList[i];
00101   }
00102   Use &getOperandUse(unsigned i) {
00103     assert(i < NumOperands && "getOperandUse() out of range!");
00104     return OperandList[i];
00105   }
00106 
00107   unsigned getNumOperands() const { return NumOperands; }
00108 
00109   // ---------------------------------------------------------------------------
00110   // Operand Iterator interface...
00111   //
00112   typedef Use*       op_iterator;
00113   typedef const Use* const_op_iterator;
00114   typedef iterator_range<op_iterator> op_range;
00115   typedef iterator_range<const_op_iterator> const_op_range;
00116 
00117   inline op_iterator       op_begin()       { return OperandList; }
00118   inline const_op_iterator op_begin() const { return OperandList; }
00119   inline op_iterator       op_end()         { return OperandList+NumOperands; }
00120   inline const_op_iterator op_end()   const { return OperandList+NumOperands; }
00121   inline op_range operands() {
00122     return op_range(op_begin(), op_end());
00123   }
00124   inline const_op_range operands() const {
00125     return const_op_range(op_begin(), op_end());
00126   }
00127 
00128   /// \brief Iterator for directly iterating over the operand Values.
00129   struct value_op_iterator
00130       : iterator_adaptor_base<value_op_iterator, op_iterator,
00131                               std::random_access_iterator_tag, Value *,
00132                               ptrdiff_t, Value *, Value *> {
00133     explicit value_op_iterator(Use *U = nullptr) : iterator_adaptor_base(U) {}
00134 
00135     Value *operator*() const { return *I; }
00136     Value *operator->() const { return operator*(); }
00137   };
00138 
00139   inline value_op_iterator value_op_begin() {
00140     return value_op_iterator(op_begin());
00141   }
00142   inline value_op_iterator value_op_end() {
00143     return value_op_iterator(op_end());
00144   }
00145   inline iterator_range<value_op_iterator> operand_values() {
00146     return iterator_range<value_op_iterator>(value_op_begin(), value_op_end());
00147   }
00148 
00149   /// \brief Drop all references to operands.
00150   ///
00151   /// This function is in charge of "letting go" of all objects that this User
00152   /// refers to.  This allows one to 'delete' a whole class at a time, even
00153   /// though there may be circular references...  First all references are
00154   /// dropped, and all use counts go to zero.  Then everything is deleted for
00155   /// real.  Note that no operations are valid on an object that has "dropped
00156   /// all references", except operator delete.
00157   void dropAllReferences() {
00158     for (Use &U : operands())
00159       U.set(nullptr);
00160   }
00161 
00162   /// \brief Replace uses of one Value with another.
00163   ///
00164   /// Replaces all references to the "From" definition with references to the
00165   /// "To" definition.
00166   void replaceUsesOfWith(Value *From, Value *To);
00167 
00168   // Methods for support type inquiry through isa, cast, and dyn_cast:
00169   static inline bool classof(const Value *V) {
00170     return isa<Instruction>(V) || isa<Constant>(V);
00171   }
00172 };
00173 
00174 template<> struct simplify_type<User::op_iterator> {
00175   typedef Value* SimpleType;
00176   static SimpleType getSimplifiedValue(User::op_iterator &Val) {
00177     return Val->get();
00178   }
00179 };
00180 template<> struct simplify_type<User::const_op_iterator> {
00181   typedef /*const*/ Value* SimpleType;
00182   static SimpleType getSimplifiedValue(User::const_op_iterator &Val) {
00183     return Val->get();
00184   }
00185 };
00186 
00187 } // End llvm namespace
00188 
00189 #endif