Move the definition of value_use_iterator::getOperandNo to User.h where the
[oota-llvm.git] / include / llvm / Use.h
1 //===-- llvm/Use.h - Definition of the Use class ----------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This defines the Use class.  The Use class represents the operand of an
11 // instruction or some other User instance which refers to a Value.  The Use
12 // class keeps the "use list" of the referenced value up to date.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_USE_H
17 #define LLVM_USE_H
18
19 #include "llvm/Support/Casting.h"
20 #include "llvm/ADT/iterator"
21
22 namespace llvm {
23
24 class Value;
25 class User;
26
27
28 //===----------------------------------------------------------------------===//
29 //                                  Use Class
30 //===----------------------------------------------------------------------===//
31
32 // Use is here to make keeping the "use" list of a Value up-to-date really easy.
33 //
34 class Use {
35 public:
36   inline void init(Value *V, User *U);
37
38   Use(Value *V, User *U) { init(V, U); }
39   Use(const Use &U) { init(U.Val, U.U); }
40   inline ~Use();
41
42   /// Default ctor - This leaves the Use completely unitialized.  The only thing
43   /// that is valid to do with this use is to call the "init" method.
44   inline Use() : Val(0) {}
45
46
47   operator Value*() const { return Val; }
48   Value *get() const { return Val; }
49   User *getUser() const { return U; }
50
51   inline void set(Value *Val);
52
53   Value *operator=(Value *RHS) {
54     set(RHS);
55     return RHS;
56   }
57   const Use &operator=(const Use &RHS) {
58     set(RHS.Val);
59     return *this;
60   }
61
62         Value *operator->()       { return Val; }
63   const Value *operator->() const { return Val; }
64
65   Use *getNext() const { return Next; }
66 private:
67   Use *Next, **Prev;
68   Value *Val;
69   User *U;
70
71   void addToList(Use **List) {
72     Next = *List;
73     if (Next) Next->Prev = &Next;
74     Prev = List;
75     *List = this;
76   }
77   void removeFromList() {
78     *Prev = Next;
79     if (Next) Next->Prev = Prev;
80   }
81
82   friend class Value;
83 };
84
85 // simplify_type - Allow clients to treat uses just like values when using
86 // casting operators.
87 template<> struct simplify_type<Use> {
88   typedef Value* SimpleType;
89   static SimpleType getSimplifiedValue(const Use &Val) {
90     return static_cast<SimpleType>(Val.get());
91   }
92 };
93 template<> struct simplify_type<const Use> {
94   typedef Value* SimpleType;
95   static SimpleType getSimplifiedValue(const Use &Val) {
96     return static_cast<SimpleType>(Val.get());
97   }
98 };
99
100
101
102 template<typename UserTy>  // UserTy == 'User' or 'const User'
103 class value_use_iterator : public forward_iterator<UserTy*, ptrdiff_t> {
104   typedef forward_iterator<UserTy*, ptrdiff_t> super;
105   typedef value_use_iterator<UserTy> _Self;
106
107   Use *U;
108   value_use_iterator(Use *u) : U(u) {}
109   friend class Value;
110 public:
111   typedef typename super::reference reference;
112   typedef typename super::pointer pointer;
113
114   value_use_iterator(const _Self &I) : U(I.U) {}
115   value_use_iterator() {}
116
117   bool operator==(const _Self &x) const {
118     return U == x.U;
119   }
120   bool operator!=(const _Self &x) const {
121     return !operator==(x);
122   }
123
124   // Iterator traversal: forward iteration only
125   _Self &operator++() {          // Preincrement
126     assert(U && "Cannot increment end iterator!");
127     U = U->getNext();
128     return *this;
129   }
130   _Self operator++(int) {        // Postincrement
131     _Self tmp = *this; ++*this; return tmp;
132   }
133
134   // Retrieve a reference to the current SCC
135   UserTy *operator*() const {
136     assert(U && "Cannot increment end iterator!");
137     return U->getUser();
138   }
139
140   UserTy *operator->() const { return operator*(); }
141
142   Use &getUse() const { return *U; }
143   
144   /// getOperandNo - Return the operand # of this use in its User.  Defined in
145   /// User.h
146   ///
147   unsigned getOperandNo() const;
148 };
149
150
151 template<> struct simplify_type<value_use_iterator<User> > {
152   typedef User* SimpleType;
153   
154   static SimpleType getSimplifiedValue(const value_use_iterator<User> &Val) {
155     return *Val;
156   }
157 };
158
159 template<> struct simplify_type<const value_use_iterator<User> >
160  : public simplify_type<value_use_iterator<User> > {};
161
162 template<> struct simplify_type<value_use_iterator<const User> > {
163   typedef const User* SimpleType;
164   
165   static SimpleType getSimplifiedValue(const 
166                                        value_use_iterator<const User> &Val) {
167     return *Val;
168   }
169 };
170
171 template<> struct simplify_type<const value_use_iterator<const User> >
172   : public simplify_type<value_use_iterator<const User> > {};
173
174 } // End llvm namespace
175
176 #endif