1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===-- llvm/User.h - User class definition ---------------------*- C++ -*-===// 2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// The LLVM Compiler Infrastructure 4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file is distributed under the University of Illinois Open Source 6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// License. See LICENSE.TXT for details. 7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===// 9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This class defines the interface that one who 'use's a Value must implement. 11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Each instance of the Value class keeps track of what User's have handles 12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// to it. 13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// * Instructions are the largest class of User's. 15894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// * Constants may be users of other constants (think arrays and stuff) 16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===// 18894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 19894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef LLVM_USER_H 20894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define LLVM_USER_H 21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Value.h" 23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace llvm { 25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// OperandTraits - Compile-time customization of 27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// operand-related allocators and accessors 28894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// for use of the User class 29894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate <class> 30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstruct OperandTraits; 31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 32894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass User : public Value { 33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman User(const User &); // Do not implement 34894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void *operator new(size_t); // Do not implement 35894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman template <unsigned> 36894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman friend struct HungoffOperandTraits; 37894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanprotected: 38894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// OperandList - This is a pointer to the array of Uses for this User. 39894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// For nodes of fixed arity (e.g. a binary operator) this array will live 40894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// prefixed to some derived class instance. For nodes of resizable variable 41894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// arity (e.g. PHINodes, SwitchInst etc.), this memory will be dynamically 42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// allocated and should be destroyed by the classes' virtual dtor. 43894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Use *OperandList; 44894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 45894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// NumOperands - The number of values used by this User. 46894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// 47894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned NumOperands; 48894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 49894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void *operator new(size_t s, unsigned Us); 5019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman User(Type *ty, unsigned vty, Use *OpList, unsigned NumOps) 51894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman : Value(ty, vty), OperandList(OpList), NumOperands(NumOps) {} 52894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Use *allocHungoffUses(unsigned) const; 5319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman void dropHungoffUses() { 5419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Use::zap(OperandList, OperandList + NumOperands, true); 5519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman OperandList = 0; 5619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Reset NumOperands so User::operator delete() does the right thing. 5719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman NumOperands = 0; 58894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 59894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanpublic: 60894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ~User() { 6119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Use::zap(OperandList, OperandList + NumOperands); 62894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 63894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// operator delete - free memory allocated for User and Use objects 64894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void operator delete(void *Usr); 65894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// placement delete - required by std, but never called. 66894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void operator delete(void*, unsigned) { 67894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(0 && "Constructor throws?"); 68894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 69894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// placement delete - required by std, but never called. 70894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void operator delete(void*, unsigned, bool) { 71894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(0 && "Constructor throws?"); 72894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 73894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanprotected: 74894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman template <int Idx, typename U> static Use &OpFrom(const U *that) { 75894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return Idx < 0 76894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ? OperandTraits<U>::op_end(const_cast<U*>(that))[Idx] 77894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman : OperandTraits<U>::op_begin(const_cast<U*>(that))[Idx]; 78894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 79894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman template <int Idx> Use &Op() { 80894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return OpFrom<Idx>(this); 81894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 82894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman template <int Idx> const Use &Op() const { 83894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return OpFrom<Idx>(this); 84894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 85894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanpublic: 86894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Value *getOperand(unsigned i) const { 87894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(i < NumOperands && "getOperand() out of range!"); 88894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return OperandList[i]; 89894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 90894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void setOperand(unsigned i, Value *Val) { 91894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(i < NumOperands && "setOperand() out of range!"); 92894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert((!isa<Constant>((const Value*)this) || 93894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman isa<GlobalValue>((const Value*)this)) && 94894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman "Cannot mutate a constant with setOperand!"); 95894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman OperandList[i] = Val; 96894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 97894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const Use &getOperandUse(unsigned i) const { 9819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman assert(i < NumOperands && "getOperandUse() out of range!"); 99894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return OperandList[i]; 100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Use &getOperandUse(unsigned i) { 10219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman assert(i < NumOperands && "getOperandUse() out of range!"); 103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return OperandList[i]; 104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned getNumOperands() const { return NumOperands; } 107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // --------------------------------------------------------------------------- 109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Operand Iterator interface... 110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // 111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman typedef Use* op_iterator; 112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman typedef const Use* const_op_iterator; 113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman inline op_iterator op_begin() { return OperandList; } 115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman inline const_op_iterator op_begin() const { return OperandList; } 116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman inline op_iterator op_end() { return OperandList+NumOperands; } 117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman inline const_op_iterator op_end() const { return OperandList+NumOperands; } 118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // dropAllReferences() - This function is in charge of "letting go" of all 120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // objects that this User refers to. This allows one to 121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // 'delete' a whole class at a time, even though there may be circular 122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // references... First all references are dropped, and all use counts go to 123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // zero. Then everything is deleted for real. Note that no operations are 124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // valid on an object that has "dropped all references", except operator 125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // delete. 126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // 127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void dropAllReferences() { 128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (op_iterator i = op_begin(), e = op_end(); i != e; ++i) 129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman i->set(0); 130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// replaceUsesOfWith - Replaces all references to the "From" definition with 133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// references to the "To" definition. 134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// 135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void replaceUsesOfWith(Value *From, Value *To); 136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Methods for support type inquiry through isa, cast, and dyn_cast: 138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman static inline bool classof(const User *) { return true; } 139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman static inline bool classof(const Value *V) { 140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return isa<Instruction>(V) || isa<Constant>(V); 141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}; 143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate<> struct simplify_type<User::op_iterator> { 145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman typedef Value* SimpleType; 146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman static SimpleType getSimplifiedValue(const User::op_iterator &Val) { 148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return static_cast<SimpleType>(Val->get()); 149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}; 151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate<> struct simplify_type<const User::op_iterator> 153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman : public simplify_type<User::op_iterator> {}; 154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate<> struct simplify_type<User::const_op_iterator> { 156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman typedef Value* SimpleType; 157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman static SimpleType getSimplifiedValue(const User::const_op_iterator &Val) { 159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return static_cast<SimpleType>(Val->get()); 160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}; 162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate<> struct simplify_type<const User::const_op_iterator> 164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman : public simplify_type<User::const_op_iterator> {}; 165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// value_use_iterator::getOperandNo - Requires the definition of the User class. 168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate<typename UserTy> 169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned value_use_iterator<UserTy>::getOperandNo() const { 170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return U - U->getUser()->op_begin(); 171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} // End llvm namespace 174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif 176