e050743ffdb0c3737c22a6e28cd16c8e94db2c28
[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 is distributed under the University of Illinois Open Source
6 // 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     if (Val) removeFromList();
42   }
43
44   /// Default ctor - This leaves the Use completely unitialized.  The only thing
45   /// that is valid to do with this use is to call the "init" method.
46   inline Use() : Val(0) {}
47
48
49   operator Value*() const { return Val; }
50   Value *get() const { return Val; }
51   User *getUser() const { return U; }
52
53   inline void set(Value *Val);
54
55   Value *operator=(Value *RHS) {
56     set(RHS);
57     return RHS;
58   }
59   const Use &operator=(const Use &RHS) {
60     set(RHS.Val);
61     return *this;
62   }
63
64         Value *operator->()       { return Val; }
65   const Value *operator->() const { return Val; }
66
67   Use *getNext() const { return Next; }
68 private:
69   Use *Next, **Prev;
70   Value *Val;
71   User *U;
72
73   void addToList(Use **List) {
74     Next = *List;
75     if (Next) Next->Prev = &Next;
76     Prev = List;
77     *List = this;
78   }
79   void removeFromList() {
80     *Prev = Next;
81     if (Next) Next->Prev = Prev;
82   }
83
84   friend class Value;
85 };
86
87 // simplify_type - Allow clients to treat uses just like values when using
88 // casting operators.
89 template<> struct simplify_type<Use> {
90   typedef Value* SimpleType;
91   static SimpleType getSimplifiedValue(const Use &Val) {
92     return static_cast<SimpleType>(Val.get());
93   }
94 };
95 template<> struct simplify_type<const Use> {
96   typedef Value* SimpleType;
97   static SimpleType getSimplifiedValue(const Use &Val) {
98     return static_cast<SimpleType>(Val.get());
99   }
100 };
101
102
103
104 template<typename UserTy>  // UserTy == 'User' or 'const User'
105 class value_use_iterator : public forward_iterator<UserTy*, ptrdiff_t> {
106   typedef forward_iterator<UserTy*, ptrdiff_t> super;
107   typedef value_use_iterator<UserTy> _Self;
108
109   Use *U;
110   explicit value_use_iterator(Use *u) : U(u) {}
111   friend class Value;
112 public:
113   typedef typename super::reference reference;
114   typedef typename super::pointer pointer;
115
116   value_use_iterator(const _Self &I) : U(I.U) {}
117   value_use_iterator() {}
118
119   bool operator==(const _Self &x) const {
120     return U == x.U;
121   }
122   bool operator!=(const _Self &x) const {
123     return !operator==(x);
124   }
125
126   /// atEnd - return true if this iterator is equal to use_end() on the value.
127   bool atEnd() const { return U == 0; }
128
129   // Iterator traversal: forward iteration only
130   _Self &operator++() {          // Preincrement
131     assert(U && "Cannot increment end iterator!");
132     U = U->getNext();
133     return *this;
134   }
135   _Self operator++(int) {        // Postincrement
136     _Self tmp = *this; ++*this; return tmp;
137   }
138
139   // Retrieve a reference to the current User
140   UserTy *operator*() const {
141     assert(U && "Cannot increment end iterator!");
142     return U->getUser();
143   }
144
145   UserTy *operator->() const { return operator*(); }
146
147   Use &getUse() const { return *U; }
148   
149   /// getOperandNo - Return the operand # of this use in its User.  Defined in
150   /// User.h
151   ///
152   unsigned getOperandNo() const;
153 };
154
155
156 template<> struct simplify_type<value_use_iterator<User> > {
157   typedef User* SimpleType;
158   
159   static SimpleType getSimplifiedValue(const value_use_iterator<User> &Val) {
160     return *Val;
161   }
162 };
163
164 template<> struct simplify_type<const value_use_iterator<User> >
165  : public simplify_type<value_use_iterator<User> > {};
166
167 template<> struct simplify_type<value_use_iterator<const User> > {
168   typedef const User* SimpleType;
169   
170   static SimpleType getSimplifiedValue(const 
171                                        value_use_iterator<const User> &Val) {
172     return *Val;
173   }
174 };
175
176 template<> struct simplify_type<const value_use_iterator<const User> >
177   : public simplify_type<value_use_iterator<const User> > {};
178
179 } // End llvm namespace
180
181 #endif