1 //===-- llvm/Use.h - Definition of the Use class ----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
19 #include "llvm/Support/Casting.h"
20 #include "llvm/ADT/iterator"
28 //===----------------------------------------------------------------------===//
30 //===----------------------------------------------------------------------===//
32 // Use is here to make keeping the "use" list of a Value up-to-date really easy.
36 inline void init(Value *V, User *U);
38 Use(Value *V, User *U) { init(V, U); }
39 Use(const Use &U) { init(U.Val, U.U); }
41 if (Val) removeFromList();
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) {}
49 operator Value*() const { return Val; }
50 Value *get() const { return Val; }
51 User *getUser() const { return U; }
53 inline void set(Value *Val);
55 Value *operator=(Value *RHS) {
59 const Use &operator=(const Use &RHS) {
64 Value *operator->() { return Val; }
65 const Value *operator->() const { return Val; }
67 Use *getNext() const { return Next; }
73 void addToList(Use **List) {
75 if (Next) Next->Prev = &Next;
79 void removeFromList() {
81 if (Next) Next->Prev = Prev;
87 // simplify_type - Allow clients to treat uses just like values when using
89 template<> struct simplify_type<Use> {
90 typedef Value* SimpleType;
91 static SimpleType getSimplifiedValue(const Use &Val) {
92 return static_cast<SimpleType>(Val.get());
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());
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;
110 explicit value_use_iterator(Use *u) : U(u) {}
113 typedef typename super::reference reference;
114 typedef typename super::pointer pointer;
116 value_use_iterator(const _Self &I) : U(I.U) {}
117 value_use_iterator() {}
119 bool operator==(const _Self &x) const {
122 bool operator!=(const _Self &x) const {
123 return !operator==(x);
126 /// atEnd - return true if this iterator is equal to use_end() on the value.
127 bool atEnd() const { return U == 0; }
129 // Iterator traversal: forward iteration only
130 _Self &operator++() { // Preincrement
131 assert(U && "Cannot increment end iterator!");
135 _Self operator++(int) { // Postincrement
136 _Self tmp = *this; ++*this; return tmp;
139 // Retrieve a reference to the current User
140 UserTy *operator*() const {
141 assert(U && "Cannot increment end iterator!");
145 UserTy *operator->() const { return operator*(); }
147 Use &getUse() const { return *U; }
149 /// getOperandNo - Return the operand # of this use in its User. Defined in
152 unsigned getOperandNo() const;
156 template<> struct simplify_type<value_use_iterator<User> > {
157 typedef User* SimpleType;
159 static SimpleType getSimplifiedValue(const value_use_iterator<User> &Val) {
164 template<> struct simplify_type<const value_use_iterator<User> >
165 : public simplify_type<value_use_iterator<User> > {};
167 template<> struct simplify_type<value_use_iterator<const User> > {
168 typedef const User* SimpleType;
170 static SimpleType getSimplifiedValue(const
171 value_use_iterator<const User> &Val) {
176 template<> struct simplify_type<const value_use_iterator<const User> >
177 : public simplify_type<value_use_iterator<const User> > {};
179 } // End llvm namespace