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