LLVM API Documentation

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 &) LLVM_DELETED_FUNCTION;
00037   void *operator new(size_t) LLVM_DELETED_FUNCTION;
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() {
00064     Use::zap(OperandList, OperandList + NumOperands);
00065   }
00066   /// \brief Free memory allocated for User and Use objects.
00067   void operator delete(void *Usr);
00068   /// \brief Placement delete - required by std, but never called.
00069   void operator delete(void*, unsigned) {
00070     llvm_unreachable("Constructor throws?");
00071   }
00072   /// \brief Placement delete - required by std, but never called.
00073   void operator delete(void*, unsigned, bool) {
00074     llvm_unreachable("Constructor throws?");
00075   }
00076 protected:
00077   template <int Idx, typename U> static Use &OpFrom(const U *that) {
00078     return Idx < 0
00079       ? OperandTraits<U>::op_end(const_cast<U*>(that))[Idx]
00080       : OperandTraits<U>::op_begin(const_cast<U*>(that))[Idx];
00081   }
00082   template <int Idx> Use &Op() {
00083     return OpFrom<Idx>(this);
00084   }
00085   template <int Idx> const Use &Op() const {
00086     return OpFrom<Idx>(this);
00087   }
00088 public:
00089   Value *getOperand(unsigned i) const {
00090     assert(i < NumOperands && "getOperand() out of range!");
00091     return OperandList[i];
00092   }
00093   void setOperand(unsigned i, Value *Val) {
00094     assert(i < NumOperands && "setOperand() out of range!");
00095     assert((!isa<Constant>((const Value*)this) ||
00096             isa<GlobalValue>((const Value*)this)) &&
00097            "Cannot mutate a constant with setOperand!");
00098     OperandList[i] = Val;
00099   }
00100   const Use &getOperandUse(unsigned i) const {
00101     assert(i < NumOperands && "getOperandUse() out of range!");
00102     return OperandList[i];
00103   }
00104   Use &getOperandUse(unsigned i) {
00105     assert(i < NumOperands && "getOperandUse() out of range!");
00106     return OperandList[i];
00107   }
00108 
00109   unsigned getNumOperands() const { return NumOperands; }
00110 
00111   // ---------------------------------------------------------------------------
00112   // Operand Iterator interface...
00113   //
00114   typedef Use*       op_iterator;
00115   typedef const Use* const_op_iterator;
00116   typedef iterator_range<op_iterator> op_range;
00117   typedef iterator_range<const_op_iterator> const_op_range;
00118 
00119   inline op_iterator       op_begin()       { return OperandList; }
00120   inline const_op_iterator op_begin() const { return OperandList; }
00121   inline op_iterator       op_end()         { return OperandList+NumOperands; }
00122   inline const_op_iterator op_end()   const { return OperandList+NumOperands; }
00123   inline op_range operands() {
00124     return op_range(op_begin(), op_end());
00125   }
00126   inline const_op_range operands() const {
00127     return const_op_range(op_begin(), op_end());
00128   }
00129 
00130   /// \brief Iterator for directly iterating over the operand Values.
00131   struct value_op_iterator
00132       : iterator_adaptor_base<value_op_iterator, op_iterator,
00133                               std::random_access_iterator_tag, Value *,
00134                               ptrdiff_t, Value *, Value *> {
00135     explicit value_op_iterator(Use *U = nullptr) : iterator_adaptor_base(U) {}
00136 
00137     Value *operator*() const { return *I; }
00138     Value *operator->() const { return operator*(); }
00139   };
00140 
00141   inline value_op_iterator value_op_begin() {
00142     return value_op_iterator(op_begin());
00143   }
00144   inline value_op_iterator value_op_end() {
00145     return value_op_iterator(op_end());
00146   }
00147   inline iterator_range<value_op_iterator> operand_values() {
00148     return iterator_range<value_op_iterator>(value_op_begin(), value_op_end());
00149   }
00150 
00151   /// \brief Drop all references to operands.
00152   ///
00153   /// This function is in charge of "letting go" of all objects that this User
00154   /// refers to.  This allows one to 'delete' a whole class at a time, even
00155   /// though there may be circular references...  First all references are
00156   /// dropped, and all use counts go to zero.  Then everything is deleted for
00157   /// real.  Note that no operations are valid on an object that has "dropped
00158   /// all references", except operator delete.
00159   void dropAllReferences() {
00160     for (Use &U : operands())
00161       U.set(nullptr);
00162   }
00163 
00164   /// \brief Replace uses of one Value with another.
00165   ///
00166   /// Replaces all references to the "From" definition with references to the
00167   /// "To" definition.
00168   void replaceUsesOfWith(Value *From, Value *To);
00169 
00170   // Methods for support type inquiry through isa, cast, and dyn_cast:
00171   static inline bool classof(const Value *V) {
00172     return isa<Instruction>(V) || isa<Constant>(V);
00173   }
00174 };
00175 
00176 template<> struct simplify_type<User::op_iterator> {
00177   typedef Value* SimpleType;
00178   static SimpleType getSimplifiedValue(User::op_iterator &Val) {
00179     return Val->get();
00180   }
00181 };
00182 template<> struct simplify_type<User::const_op_iterator> {
00183   typedef /*const*/ Value* SimpleType;
00184   static SimpleType getSimplifiedValue(User::const_op_iterator &Val) {
00185     return Val->get();
00186   }
00187 };
00188 
00189 } // End llvm namespace
00190 
00191 #endif