LLVM API Documentation
00001 //===-- llvm/User.h - User class definition ---------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This class defines the interface that one who 'use's 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 User's. 00015 // * Constants may be users of other constants (think arrays and stuff) 00016 // 00017 //===----------------------------------------------------------------------===// 00018 00019 #ifndef LLVM_USER_H 00020 #define LLVM_USER_H 00021 00022 #include "llvm/Value.h" 00023 #include <vector> 00024 00025 namespace llvm { 00026 00027 class User : public Value { 00028 User(const User &); // Do not implement 00029 protected: 00030 /// OperandList - This is a pointer to the array of Users for this operand. 00031 /// For nodes of fixed arity (e.g. a binary operator) this array will live 00032 /// embedded into the derived class. For nodes of variable arity 00033 /// (e.g. ConstantArrays, CallInst, PHINodes, etc), this memory will be 00034 /// dynamically allocated and should be destroyed by the classes virtual dtor. 00035 Use *OperandList; 00036 00037 /// NumOperands - The number of values used by this User. 00038 /// 00039 unsigned NumOperands; 00040 00041 public: 00042 User(const Type *Ty, unsigned vty, Use *OpList, unsigned NumOps, 00043 const std::string &name = "") 00044 : Value(Ty, vty, name), OperandList(OpList), NumOperands(NumOps) {} 00045 00046 Value *getOperand(unsigned i) const { 00047 assert(i < NumOperands && "getOperand() out of range!"); 00048 return OperandList[i]; 00049 } 00050 void setOperand(unsigned i, Value *Val) { 00051 assert(i < NumOperands && "setOperand() out of range!"); 00052 OperandList[i] = Val; 00053 } 00054 unsigned getNumOperands() const { return NumOperands; } 00055 00056 // --------------------------------------------------------------------------- 00057 // Operand Iterator interface... 00058 // 00059 typedef Use* op_iterator; 00060 typedef const Use* const_op_iterator; 00061 00062 inline op_iterator op_begin() { return OperandList; } 00063 inline const_op_iterator op_begin() const { return OperandList; } 00064 inline op_iterator op_end() { return OperandList+NumOperands; } 00065 inline const_op_iterator op_end() const { return OperandList+NumOperands; } 00066 00067 // dropAllReferences() - This function is in charge of "letting go" of all 00068 // objects that this User refers to. This allows one to 00069 // 'delete' a whole class at a time, even though there may be circular 00070 // references... first all references are dropped, and all use counts go to 00071 // zero. Then everything is delete'd for real. Note that no operations are 00072 // valid on an object that has "dropped all references", except operator 00073 // delete. 00074 // 00075 void dropAllReferences() { 00076 Use *OL = OperandList; 00077 for (unsigned i = 0, e = NumOperands; i != e; ++i) 00078 OL[i].set(0); 00079 } 00080 00081 /// replaceUsesOfWith - Replaces all references to the "From" definition with 00082 /// references to the "To" definition. 00083 /// 00084 void replaceUsesOfWith(Value *From, Value *To); 00085 00086 // Methods for support type inquiry through isa, cast, and dyn_cast: 00087 static inline bool classof(const User *) { return true; } 00088 static inline bool classof(const Value *V) { 00089 return isa<Instruction>(V) || isa<Constant>(V); 00090 } 00091 }; 00092 00093 template<> struct simplify_type<User::op_iterator> { 00094 typedef Value* SimpleType; 00095 00096 static SimpleType getSimplifiedValue(const User::op_iterator &Val) { 00097 return static_cast<SimpleType>(Val->get()); 00098 } 00099 }; 00100 00101 template<> struct simplify_type<const User::op_iterator> 00102 : public simplify_type<User::op_iterator> {}; 00103 00104 template<> struct simplify_type<User::const_op_iterator> { 00105 typedef Value* SimpleType; 00106 00107 static SimpleType getSimplifiedValue(const User::const_op_iterator &Val) { 00108 return static_cast<SimpleType>(Val->get()); 00109 } 00110 }; 00111 00112 template<> struct simplify_type<const User::const_op_iterator> 00113 : public simplify_type<User::const_op_iterator> {}; 00114 00115 } // End llvm namespace 00116 00117 #endif