1249259Sdim//===-- llvm/Use.h - Definition of the Use class ----------------*- C++ -*-===//
2249259Sdim//
3249259Sdim//                     The LLVM Compiler Infrastructure
4249259Sdim//
5249259Sdim// This file is distributed under the University of Illinois Open Source
6249259Sdim// License. See LICENSE.TXT for details.
7249259Sdim//
8249259Sdim//===----------------------------------------------------------------------===//
9249259Sdim//
10249259Sdim// This defines the Use class.  The Use class represents the operand of an
11249259Sdim// instruction or some other User instance which refers to a Value.  The Use
12249259Sdim// class keeps the "use list" of the referenced value up to date.
13249259Sdim//
14249259Sdim// Pointer tagging is used to efficiently find the User corresponding
15249259Sdim// to a Use without having to store a User pointer in every Use. A
16249259Sdim// User is preceded in memory by all the Uses corresponding to its
17249259Sdim// operands, and the low bits of one of the fields (Prev) of the Use
18249259Sdim// class are used to encode offsets to be able to find that User given
19249259Sdim// a pointer to any Use. For details, see:
20249259Sdim//
21249259Sdim//   http://www.llvm.org/docs/ProgrammersManual.html#UserLayout
22249259Sdim//
23249259Sdim//===----------------------------------------------------------------------===//
24249259Sdim
25249259Sdim#ifndef LLVM_IR_USE_H
26249259Sdim#define LLVM_IR_USE_H
27249259Sdim
28249259Sdim#include "llvm/ADT/PointerIntPair.h"
29251662Sdim#include "llvm/Support/CBindingWrapping.h"
30249259Sdim#include "llvm/Support/Compiler.h"
31251662Sdim#include "llvm-c/Core.h"
32249259Sdim#include <cstddef>
33249259Sdim#include <iterator>
34249259Sdim
35249259Sdimnamespace llvm {
36249259Sdim
37249259Sdimclass Value;
38249259Sdimclass User;
39249259Sdimclass Use;
40249259Sdimtemplate<typename>
41249259Sdimstruct simplify_type;
42249259Sdim
43249259Sdim// Use** is only 4-byte aligned.
44249259Sdimtemplate<>
45249259Sdimclass PointerLikeTypeTraits<Use**> {
46249259Sdimpublic:
47249259Sdim  static inline void *getAsVoidPointer(Use** P) { return P; }
48249259Sdim  static inline Use **getFromVoidPointer(void *P) {
49249259Sdim    return static_cast<Use**>(P);
50249259Sdim  }
51249259Sdim  enum { NumLowBitsAvailable = 2 };
52249259Sdim};
53249259Sdim
54249259Sdim//===----------------------------------------------------------------------===//
55249259Sdim//                                  Use Class
56249259Sdim//===----------------------------------------------------------------------===//
57249259Sdim
58249259Sdim/// Use is here to make keeping the "use" list of a Value up-to-date really
59249259Sdim/// easy.
60249259Sdimclass Use {
61249259Sdimpublic:
62249259Sdim  /// swap - provide a fast substitute to std::swap<Use>
63249259Sdim  /// that also works with less standard-compliant compilers
64249259Sdim  void swap(Use &RHS);
65249259Sdim
66249259Sdim  // A type for the word following an array of hung-off Uses in memory, which is
67249259Sdim  // a pointer back to their User with the bottom bit set.
68249259Sdim  typedef PointerIntPair<User*, 1, unsigned> UserRef;
69249259Sdim
70249259Sdimprivate:
71249259Sdim  Use(const Use &U) LLVM_DELETED_FUNCTION;
72249259Sdim
73249259Sdim  /// Destructor - Only for zap()
74249259Sdim  ~Use() {
75249259Sdim    if (Val) removeFromList();
76249259Sdim  }
77249259Sdim
78249259Sdim  enum PrevPtrTag { zeroDigitTag
79249259Sdim                  , oneDigitTag
80249259Sdim                  , stopTag
81249259Sdim                  , fullStopTag };
82249259Sdim
83249259Sdim  /// Constructor
84249259Sdim  Use(PrevPtrTag tag) : Val(0) {
85249259Sdim    Prev.setInt(tag);
86249259Sdim  }
87249259Sdim
88249259Sdimpublic:
89249259Sdim  /// Normally Use will just implicitly convert to a Value* that it holds.
90249259Sdim  operator Value*() const { return Val; }
91249259Sdim
92249259Sdim  /// If implicit conversion to Value* doesn't work, the get() method returns
93249259Sdim  /// the Value*.
94249259Sdim  Value *get() const { return Val; }
95249259Sdim
96249259Sdim  /// getUser - This returns the User that contains this Use.  For an
97249259Sdim  /// instruction operand, for example, this will return the instruction.
98249259Sdim  User *getUser() const;
99249259Sdim
100249259Sdim  inline void set(Value *Val);
101249259Sdim
102249259Sdim  Value *operator=(Value *RHS) {
103249259Sdim    set(RHS);
104249259Sdim    return RHS;
105249259Sdim  }
106249259Sdim  const Use &operator=(const Use &RHS) {
107249259Sdim    set(RHS.Val);
108249259Sdim    return *this;
109249259Sdim  }
110249259Sdim
111249259Sdim        Value *operator->()       { return Val; }
112249259Sdim  const Value *operator->() const { return Val; }
113249259Sdim
114249259Sdim  Use *getNext() const { return Next; }
115249259Sdim
116249259Sdim
117249259Sdim  /// initTags - initialize the waymarking tags on an array of Uses, so that
118249259Sdim  /// getUser() can find the User from any of those Uses.
119249259Sdim  static Use *initTags(Use *Start, Use *Stop);
120249259Sdim
121249259Sdim  /// zap - This is used to destroy Use operands when the number of operands of
122249259Sdim  /// a User changes.
123249259Sdim  static void zap(Use *Start, const Use *Stop, bool del = false);
124249259Sdim
125249259Sdimprivate:
126249259Sdim  const Use* getImpliedUser() const;
127249259Sdim
128249259Sdim  Value *Val;
129249259Sdim  Use *Next;
130249259Sdim  PointerIntPair<Use**, 2, PrevPtrTag> Prev;
131249259Sdim
132249259Sdim  void setPrev(Use **NewPrev) {
133249259Sdim    Prev.setPointer(NewPrev);
134249259Sdim  }
135249259Sdim  void addToList(Use **List) {
136249259Sdim    Next = *List;
137249259Sdim    if (Next) Next->setPrev(&Next);
138249259Sdim    setPrev(List);
139249259Sdim    *List = this;
140249259Sdim  }
141249259Sdim  void removeFromList() {
142249259Sdim    Use **StrippedPrev = Prev.getPointer();
143249259Sdim    *StrippedPrev = Next;
144249259Sdim    if (Next) Next->setPrev(StrippedPrev);
145249259Sdim  }
146249259Sdim
147249259Sdim  friend class Value;
148249259Sdim};
149249259Sdim
150249259Sdim// simplify_type - Allow clients to treat uses just like values when using
151249259Sdim// casting operators.
152249259Sdimtemplate<> struct simplify_type<Use> {
153249259Sdim  typedef Value* SimpleType;
154249259Sdim  static SimpleType getSimplifiedValue(Use &Val) {
155249259Sdim    return Val.get();
156249259Sdim  }
157249259Sdim};
158249259Sdimtemplate<> struct simplify_type<const Use> {
159249259Sdim  typedef /*const*/ Value* SimpleType;
160249259Sdim  static SimpleType getSimplifiedValue(const Use &Val) {
161249259Sdim    return Val.get();
162249259Sdim  }
163249259Sdim};
164249259Sdim
165249259Sdim
166249259Sdim
167249259Sdimtemplate<typename UserTy>  // UserTy == 'User' or 'const User'
168249259Sdimclass value_use_iterator : public std::iterator<std::forward_iterator_tag,
169249259Sdim                                                UserTy*, ptrdiff_t> {
170249259Sdim  typedef std::iterator<std::forward_iterator_tag, UserTy*, ptrdiff_t> super;
171249259Sdim  typedef value_use_iterator<UserTy> _Self;
172249259Sdim
173249259Sdim  Use *U;
174249259Sdim  explicit value_use_iterator(Use *u) : U(u) {}
175249259Sdim  friend class Value;
176249259Sdimpublic:
177249259Sdim  typedef typename super::reference reference;
178249259Sdim  typedef typename super::pointer pointer;
179249259Sdim
180249259Sdim  value_use_iterator() {}
181249259Sdim
182249259Sdim  bool operator==(const _Self &x) const {
183249259Sdim    return U == x.U;
184249259Sdim  }
185249259Sdim  bool operator!=(const _Self &x) const {
186249259Sdim    return !operator==(x);
187249259Sdim  }
188249259Sdim
189249259Sdim  /// atEnd - return true if this iterator is equal to use_end() on the value.
190249259Sdim  bool atEnd() const { return U == 0; }
191249259Sdim
192249259Sdim  // Iterator traversal: forward iteration only
193249259Sdim  _Self &operator++() {          // Preincrement
194249259Sdim    assert(U && "Cannot increment end iterator!");
195249259Sdim    U = U->getNext();
196249259Sdim    return *this;
197249259Sdim  }
198249259Sdim  _Self operator++(int) {        // Postincrement
199249259Sdim    _Self tmp = *this; ++*this; return tmp;
200249259Sdim  }
201249259Sdim
202249259Sdim  // Retrieve a pointer to the current User.
203249259Sdim  UserTy *operator*() const {
204249259Sdim    assert(U && "Cannot dereference end iterator!");
205249259Sdim    return U->getUser();
206249259Sdim  }
207249259Sdim
208249259Sdim  UserTy *operator->() const { return operator*(); }
209249259Sdim
210249259Sdim  Use &getUse() const { return *U; }
211249259Sdim
212249259Sdim  /// getOperandNo - Return the operand # of this use in its User.  Defined in
213249259Sdim  /// User.h
214249259Sdim  ///
215249259Sdim  unsigned getOperandNo() const;
216249259Sdim};
217249259Sdim
218251662Sdim// Create wrappers for C Binding types (see CBindingWrapping.h).
219251662SdimDEFINE_SIMPLE_CONVERSION_FUNCTIONS(Use, LLVMUseRef)
220251662Sdim
221249259Sdim} // End llvm namespace
222249259Sdim
223249259Sdim#endif
224