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   /// OperandList - This is a pointer to the array of Uses for this User.
00043   /// For nodes of fixed arity (e.g. a binary operator) this array will live
00044   /// prefixed to some derived class instance.  For nodes of resizable variable
00045   /// arity (e.g. PHINodes, SwitchInst etc.), this memory will be dynamically
00046   /// allocated and should be destroyed by the classes' virtual dtor.
00047   Use *OperandList;
00048 
00049   /// NumOperands - The number of values used by this User.
00050   ///
00051   unsigned NumOperands;
00052 
00053   void *operator new(size_t s, unsigned Us);
00054   User(Type *ty, unsigned vty, Use *OpList, unsigned NumOps)
00055     : Value(ty, vty), OperandList(OpList), NumOperands(NumOps) {}
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, Value *, Value *,
00135                               Value *> {
00136     explicit value_op_iterator(Use *U = nullptr) : iterator_adaptor_base(U) {}
00137 
00138     Value *operator*() const { return *I; }
00139     Value *operator->() const { return operator*(); }
00140   };
00141 
00142   inline value_op_iterator value_op_begin() {
00143     return value_op_iterator(op_begin());
00144   }
00145   inline value_op_iterator value_op_end() {
00146     return value_op_iterator(op_end());
00147   }
00148   inline iterator_range<value_op_iterator> operand_values() {
00149     return iterator_range<value_op_iterator>(value_op_begin(), value_op_end());
00150   }
00151 
00152   // dropAllReferences() - This function is in charge of "letting go" of all
00153   // objects that this User refers to.  This allows one to
00154   // 'delete' a whole class at a time, even though there may be circular
00155   // references...  First all references are dropped, and all use counts go to
00156   // zero.  Then everything is deleted for real.  Note that no operations are
00157   // valid on an object that has "dropped all references", except operator
00158   // delete.
00159   //
00160   void dropAllReferences() {
00161     for (Use &U : operands())
00162       U.set(nullptr);
00163   }
00164 
00165   /// replaceUsesOfWith - Replaces all references to the "From" definition with
00166   /// references to the "To" definition.
00167   ///
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