ce13648f9b883314ad80378e1f19744b52360cd2
[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   /// atEnd - return true if this iterator is equal to use_end() on the value.
125   bool atEnd() const { return U == 0; }
126
127   // Iterator traversal: forward iteration only
128   _Self &operator++() {          // Preincrement
129     assert(U && "Cannot increment end iterator!");
130     U = U->getNext();
131     return *this;
132   }
133   _Self operator++(int) {        // Postincrement
134     _Self tmp = *this; ++*this; return tmp;
135   }
136
137   // Retrieve a reference to the current SCC
138   UserTy *operator*() const {
139     assert(U && "Cannot increment end iterator!");
140     return U->getUser();
141   }
142
143   UserTy *operator->() const { return operator*(); }
144
145   Use &getUse() const { return *U; }
146   
147   /// getOperandNo - Return the operand # of this use in its User.  Defined in
148   /// User.h
149   ///
150   unsigned getOperandNo() const;
151 };
152
153
154 template<> struct simplify_type<value_use_iterator<User> > {
155   typedef User* SimpleType;
156   
157   static SimpleType getSimplifiedValue(const value_use_iterator<User> &Val) {
158     return *Val;
159   }
160 };
161
162 template<> struct simplify_type<const value_use_iterator<User> >
163  : public simplify_type<value_use_iterator<User> > {};
164
165 template<> struct simplify_type<value_use_iterator<const User> > {
166   typedef const User* SimpleType;
167   
168   static SimpleType getSimplifiedValue(const 
169                                        value_use_iterator<const User> &Val) {
170     return *Val;
171   }
172 };
173
174 template<> struct simplify_type<const value_use_iterator<const User> >
175   : public simplify_type<value_use_iterator<const User> > {};
176
177 } // End llvm namespace
178
179 #endif