Use.h revision e932e891e55d90398f07957b7333b4ce66ae3829
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// Pointer tagging is used to efficiently find the User corresponding
15// to a Use without having to store a User pointer in every Use. A
16// User is preceded in memory by all the Uses corresponding to its
17// operands, and the low bits of one of the fields (Prev) of the Use
18// class are used to encode offsets to be able to find that User given
19// a pointer to any Use. For details, see:
20//
21//   http://www.llvm.org/docs/ProgrammersManual.html#UserLayout
22//
23//===----------------------------------------------------------------------===//
24
25#ifndef LLVM_IR_USE_H
26#define LLVM_IR_USE_H
27
28#include "llvm/ADT/PointerIntPair.h"
29#include "llvm/Support/CBindingWrapping.h"
30#include "llvm/Support/Compiler.h"
31#include "llvm-c/Core.h"
32#include <cstddef>
33#include <iterator>
34
35namespace llvm {
36
37class Value;
38class User;
39class Use;
40template<typename>
41struct simplify_type;
42
43// Use** is only 4-byte aligned.
44template<>
45class PointerLikeTypeTraits<Use**> {
46public:
47  static inline void *getAsVoidPointer(Use** P) { return P; }
48  static inline Use **getFromVoidPointer(void *P) {
49    return static_cast<Use**>(P);
50  }
51  enum { NumLowBitsAvailable = 2 };
52};
53
54//===----------------------------------------------------------------------===//
55//                                  Use Class
56//===----------------------------------------------------------------------===//
57
58/// Use is here to make keeping the "use" list of a Value up-to-date really
59/// easy.
60class Use {
61public:
62  /// swap - provide a fast substitute to std::swap<Use>
63  /// that also works with less standard-compliant compilers
64  void swap(Use &RHS);
65
66  // A type for the word following an array of hung-off Uses in memory, which is
67  // a pointer back to their User with the bottom bit set.
68  typedef PointerIntPair<User*, 1, unsigned> UserRef;
69
70private:
71  Use(const Use &U) LLVM_DELETED_FUNCTION;
72
73  /// Destructor - Only for zap()
74  ~Use() {
75    if (Val) removeFromList();
76  }
77
78  enum PrevPtrTag { zeroDigitTag
79                  , oneDigitTag
80                  , stopTag
81                  , fullStopTag };
82
83  /// Constructor
84  Use(PrevPtrTag tag) : Val(0) {
85    Prev.setInt(tag);
86  }
87
88public:
89  /// Normally Use will just implicitly convert to a Value* that it holds.
90  operator Value*() const { return Val; }
91
92  /// If implicit conversion to Value* doesn't work, the get() method returns
93  /// the Value*.
94  Value *get() const { return Val; }
95
96  /// getUser - This returns the User that contains this Use.  For an
97  /// instruction operand, for example, this will return the instruction.
98  User *getUser() const;
99
100  inline void set(Value *Val);
101
102  Value *operator=(Value *RHS) {
103    set(RHS);
104    return RHS;
105  }
106  const Use &operator=(const Use &RHS) {
107    set(RHS.Val);
108    return *this;
109  }
110
111        Value *operator->()       { return Val; }
112  const Value *operator->() const { return Val; }
113
114  Use *getNext() const { return Next; }
115
116
117  /// initTags - initialize the waymarking tags on an array of Uses, so that
118  /// getUser() can find the User from any of those Uses.
119  static Use *initTags(Use *Start, Use *Stop);
120
121  /// zap - This is used to destroy Use operands when the number of operands of
122  /// a User changes.
123  static void zap(Use *Start, const Use *Stop, bool del = false);
124
125private:
126  const Use* getImpliedUser() const;
127
128  Value *Val;
129  Use *Next;
130  PointerIntPair<Use**, 2, PrevPtrTag> Prev;
131
132  void setPrev(Use **NewPrev) {
133    Prev.setPointer(NewPrev);
134  }
135  void addToList(Use **List) {
136    Next = *List;
137    if (Next) Next->setPrev(&Next);
138    setPrev(List);
139    *List = this;
140  }
141  void removeFromList() {
142    Use **StrippedPrev = Prev.getPointer();
143    *StrippedPrev = Next;
144    if (Next) Next->setPrev(StrippedPrev);
145  }
146
147  friend class Value;
148};
149
150// simplify_type - Allow clients to treat uses just like values when using
151// casting operators.
152template<> struct simplify_type<Use> {
153  typedef Value* SimpleType;
154  static SimpleType getSimplifiedValue(Use &Val) {
155    return Val.get();
156  }
157};
158template<> struct simplify_type<const Use> {
159  typedef /*const*/ Value* SimpleType;
160  static SimpleType getSimplifiedValue(const Use &Val) {
161    return Val.get();
162  }
163};
164
165
166
167template<typename UserTy>  // UserTy == 'User' or 'const User'
168class value_use_iterator : public std::iterator<std::forward_iterator_tag,
169                                                UserTy*, ptrdiff_t> {
170  typedef std::iterator<std::forward_iterator_tag, UserTy*, ptrdiff_t> super;
171  typedef value_use_iterator<UserTy> _Self;
172
173  Use *U;
174  explicit value_use_iterator(Use *u) : U(u) {}
175  friend class Value;
176public:
177  typedef typename super::reference reference;
178  typedef typename super::pointer pointer;
179
180  value_use_iterator() {}
181
182  bool operator==(const _Self &x) const {
183    return U == x.U;
184  }
185  bool operator!=(const _Self &x) const {
186    return !operator==(x);
187  }
188
189  /// atEnd - return true if this iterator is equal to use_end() on the value.
190  bool atEnd() const { return U == 0; }
191
192  // Iterator traversal: forward iteration only
193  _Self &operator++() {          // Preincrement
194    assert(U && "Cannot increment end iterator!");
195    U = U->getNext();
196    return *this;
197  }
198  _Self operator++(int) {        // Postincrement
199    _Self tmp = *this; ++*this; return tmp;
200  }
201
202  // Retrieve a pointer to the current User.
203  UserTy *operator*() const {
204    assert(U && "Cannot dereference end iterator!");
205    return U->getUser();
206  }
207
208  UserTy *operator->() const { return operator*(); }
209
210  Use &getUse() const { return *U; }
211
212  /// getOperandNo - Return the operand # of this use in its User.  Defined in
213  /// User.h
214  ///
215  unsigned getOperandNo() const;
216};
217
218// Create wrappers for C Binding types (see CBindingWrapping.h).
219DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Use, LLVMUseRef)
220
221} // End llvm namespace
222
223#endif
224