Add missing newline to debug statement.
[llvm.git] / include / llvm / User.h
blobf8277952ee4ba9fa7cd03e8ee116c4a4fbb9f5fa
1 //===-- llvm/User.h - User class definition ---------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This class defines the interface that one who 'use's a Value must implement.
11 // Each instance of the Value class keeps track of what User's have handles
12 // to it.
14 // * Instructions are the largest class of User's.
15 // * Constants may be users of other constants (think arrays and stuff)
17 //===----------------------------------------------------------------------===//
19 #ifndef LLVM_USER_H
20 #define LLVM_USER_H
22 #include "llvm/Value.h"
24 namespace llvm {
26 /// OperandTraits - Compile-time customization of
27 /// operand-related allocators and accessors
28 /// for use of the User class
29 template <class>
30 struct OperandTraits;
32 class User;
34 /// OperandTraits<User> - specialization to User
35 template <>
36 struct OperandTraits<User> {
37 static inline Use *op_begin(User*);
38 static inline Use *op_end(User*);
39 static inline unsigned operands(const User*);
40 template <class U>
41 struct Layout {
42 typedef U overlay;
46 class User : public Value {
47 User(const User &); // Do not implement
48 void *operator new(size_t); // Do not implement
49 template <unsigned>
50 friend struct HungoffOperandTraits;
51 protected:
52 /// OperandList - This is a pointer to the array of Uses for this User.
53 /// For nodes of fixed arity (e.g. a binary operator) this array will live
54 /// prefixed to some derived class instance. For nodes of resizable variable
55 /// arity (e.g. PHINodes, SwitchInst etc.), this memory will be dynamically
56 /// allocated and should be destroyed by the classes' virtual dtor.
57 Use *OperandList;
59 /// NumOperands - The number of values used by this User.
60 ///
61 unsigned NumOperands;
63 void *operator new(size_t s, unsigned Us);
64 void *operator new(size_t s, unsigned Us, bool Prefix);
65 User(const Type *ty, unsigned vty, Use *OpList, unsigned NumOps)
66 : Value(ty, vty), OperandList(OpList), NumOperands(NumOps) {}
67 Use *allocHungoffUses(unsigned) const;
68 void dropHungoffUses(Use *U) {
69 if (OperandList == U) {
70 OperandList = 0;
71 NumOperands = 0;
73 Use::zap(U, U->getImpliedUser(), true);
75 public:
76 ~User() {
77 if ((intptr_t(OperandList) & 1) == 0)
78 Use::zap(OperandList, OperandList + NumOperands);
80 /// operator delete - free memory allocated for User and Use objects
81 void operator delete(void *Usr);
82 /// placement delete - required by std, but never called.
83 void operator delete(void*, unsigned) {
84 assert(0 && "Constructor throws?");
86 /// placement delete - required by std, but never called.
87 void operator delete(void*, unsigned, bool) {
88 assert(0 && "Constructor throws?");
90 protected:
91 template <int Idx, typename U> static Use &OpFrom(const U *that) {
92 return Idx < 0
93 ? OperandTraits<U>::op_end(const_cast<U*>(that))[Idx]
94 : OperandTraits<U>::op_begin(const_cast<U*>(that))[Idx];
96 template <int Idx> Use &Op() {
97 return OpFrom<Idx>(this);
99 template <int Idx> const Use &Op() const {
100 return OpFrom<Idx>(this);
102 public:
103 Value *getOperand(unsigned i) const {
104 assert(i < NumOperands && "getOperand() out of range!");
105 return OperandList[i];
107 void setOperand(unsigned i, Value *Val) {
108 assert(i < NumOperands && "setOperand() out of range!");
109 assert((!isa<Constant>((const Value*)this) ||
110 isa<GlobalValue>((const Value*)this)) &&
111 "Cannot mutate a constant with setOperand!");
112 OperandList[i] = Val;
114 const Use &getOperandUse(unsigned i) const {
115 assert(i < NumOperands && "getOperand() out of range!");
116 return OperandList[i];
118 Use &getOperandUse(unsigned i) {
119 assert(i < NumOperands && "getOperand() out of range!");
120 return OperandList[i];
123 unsigned getNumOperands() const { return NumOperands; }
125 // ---------------------------------------------------------------------------
126 // Operand Iterator interface...
128 typedef Use* op_iterator;
129 typedef const Use* const_op_iterator;
131 inline op_iterator op_begin() { return OperandList; }
132 inline const_op_iterator op_begin() const { return OperandList; }
133 inline op_iterator op_end() { return OperandList+NumOperands; }
134 inline const_op_iterator op_end() const { return OperandList+NumOperands; }
136 // dropAllReferences() - This function is in charge of "letting go" of all
137 // objects that this User refers to. This allows one to
138 // 'delete' a whole class at a time, even though there may be circular
139 // references... First all references are dropped, and all use counts go to
140 // zero. Then everything is deleted for real. Note that no operations are
141 // valid on an object that has "dropped all references", except operator
142 // delete.
144 void dropAllReferences() {
145 for (op_iterator i = op_begin(), e = op_end(); i != e; ++i)
146 i->set(0);
149 /// replaceUsesOfWith - Replaces all references to the "From" definition with
150 /// references to the "To" definition.
152 void replaceUsesOfWith(Value *From, Value *To);
154 // Methods for support type inquiry through isa, cast, and dyn_cast:
155 static inline bool classof(const User *) { return true; }
156 static inline bool classof(const Value *V) {
157 return isa<Instruction>(V) || isa<Constant>(V);
161 inline Use *OperandTraits<User>::op_begin(User *U) {
162 return U->op_begin();
165 inline Use *OperandTraits<User>::op_end(User *U) {
166 return U->op_end();
169 inline unsigned OperandTraits<User>::operands(const User *U) {
170 return U->getNumOperands();
173 template<> struct simplify_type<User::op_iterator> {
174 typedef Value* SimpleType;
176 static SimpleType getSimplifiedValue(const User::op_iterator &Val) {
177 return static_cast<SimpleType>(Val->get());
181 template<> struct simplify_type<const User::op_iterator>
182 : public simplify_type<User::op_iterator> {};
184 template<> struct simplify_type<User::const_op_iterator> {
185 typedef Value* SimpleType;
187 static SimpleType getSimplifiedValue(const User::const_op_iterator &Val) {
188 return static_cast<SimpleType>(Val->get());
192 template<> struct simplify_type<const User::const_op_iterator>
193 : public simplify_type<User::const_op_iterator> {};
196 // value_use_iterator::getOperandNo - Requires the definition of the User class.
197 template<typename UserTy>
198 unsigned value_use_iterator<UserTy>::getOperandNo() const {
199 return U - U->getUser()->op_begin();
202 } // End llvm namespace
204 #endif