1 //===-- llvm/Use.h - Definition of the Use class ----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
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); }
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) {}
47 operator Value*() const { return Val; }
48 Value *get() const { return Val; }
49 User *getUser() const { return U; }
51 inline void set(Value *Val);
53 Value *operator=(Value *RHS) {
57 const Use &operator=(const Use &RHS) {
62 Value *operator->() { return Val; }
63 const Value *operator->() const { return Val; }
65 Use *getNext() const { return Next; }
71 void addToList(Use **List) {
73 if (Next) Next->Prev = &Next;
77 void removeFromList() {
79 if (Next) Next->Prev = Prev;
85 // simplify_type - Allow clients to treat uses just like values when using
87 template<> struct simplify_type<Use> {
88 typedef Value* SimpleType;
89 static SimpleType getSimplifiedValue(const Use &Val) {
90 return static_cast<SimpleType>(Val.get());
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());
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;
108 value_use_iterator(Use *u) : U(u) {}
111 typedef typename super::reference reference;
112 typedef typename super::pointer pointer;
114 value_use_iterator(const _Self &I) : U(I.U) {}
115 value_use_iterator() {}
117 bool operator==(const _Self &x) const {
120 bool operator!=(const _Self &x) const {
121 return !operator==(x);
124 // Iterator traversal: forward iteration only
125 _Self &operator++() { // Preincrement
126 assert(U && "Cannot increment end iterator!");
130 _Self operator++(int) { // Postincrement
131 _Self tmp = *this; ++*this; return tmp;
134 // Retrieve a reference to the current SCC
135 UserTy *operator*() const {
136 assert(U && "Cannot increment end iterator!");
140 UserTy *operator->() const { return operator*(); }
142 Use &getUse() const { return *U; }
145 } // End llvm namespace