User.h revision 2df178997d17474042e8c3704cc93ab2db6619bf
1800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//===-- llvm/User.h - User class definition ---------------------*- C++ -*-===//
2800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//
3800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//                     The LLVM Compiler Infrastructure
4800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//
5800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// This file is distributed under the University of Illinois Open Source
6800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// License. See LICENSE.TXT for details.
7800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//
8800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//===----------------------------------------------------------------------===//
9800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//
10800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// This class defines the interface that one who uses a Value must implement.
11800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// Each instance of the Value class keeps track of what User's have handles
12800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// to it.
13800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//
14800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//  * Instructions are the largest class of Users.
15800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//  * Constants may be users of other constants (think arrays and stuff)
16800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//
17800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//===----------------------------------------------------------------------===//
18800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
19800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#ifndef LLVM_IR_USER_H
20800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#define LLVM_IR_USER_H
21800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
22800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/ADT/iterator.h"
23800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/ADT/iterator_range.h"
24800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/IR/Use.h"
25800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/IR/Value.h"
26800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/Support/Casting.h"
27800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/Support/Compiler.h"
28800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/Support/ErrorHandling.h"
29800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include <cassert>
30800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include <cstddef>
31800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include <cstdint>
32800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include <iterator>
33800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
34800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanynamespace llvm {
35800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
36800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanytemplate <typename T> class ArrayRef;
37800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanytemplate <typename T> class MutableArrayRef;
38800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
39800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany/// \brief Compile-time customization of User operands.
40800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany///
41800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany/// Customizes operand-related allocators and accessors.
42800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanytemplate <class>
43800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystruct OperandTraits;
44800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
45800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanyclass User : public Value {
46800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  template <unsigned>
47800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  friend struct HungoffOperandTraits;
48800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
49800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  virtual void anchor();
50800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
51800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  LLVM_ATTRIBUTE_ALWAYS_INLINE inline static void *
52800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  allocateFixedOperandUser(size_t, unsigned, unsigned);
53800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
54800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanyprotected:
55800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// Allocate a User with an operand pointer co-allocated.
56800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  ///
577bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany  /// This is used for subclasses which need to allocate a variable number
587bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany  /// of operands, ie, 'hung off uses'.
59800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  void *operator new(size_t Size);
60800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
617bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany  /// Allocate a User with the operands co-allocated.
62800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  ///
63800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// This is used for subclasses which have a fixed number of operands.
64800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  void *operator new(size_t Size, unsigned Us);
65800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
66800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// Allocate a User with the operands co-allocated.  If DescBytes is non-zero
67800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// then allocate an additional DescBytes bytes before the operands. These
68800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// bytes can be accessed by calling getDescriptor.
69800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  ///
70800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// DescBytes needs to be divisible by sizeof(void *).  The allocated
71800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// descriptor, if any, is aligned to sizeof(void *) bytes.
72800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  ///
73800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// This is used for subclasses which have a fixed number of operands.
74800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  void *operator new(size_t Size, unsigned Us, unsigned DescBytes);
75800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
76800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  User(Type *ty, unsigned vty, Use *, unsigned NumOps)
77800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      : Value(ty, vty) {
78800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    assert(NumOps < (1u << NumUserOperandsBits) && "Too many operands");
79800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    NumUserOperands = NumOps;
80800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    // If we have hung off uses, then the operand list should initially be
81800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    // null.
82800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    assert((!HasHungOffUses || !getOperandList()) &&
83800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany           "Error in initializing hung off uses for User");
84800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
85800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
86800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// \brief Allocate the array of Uses, followed by a pointer
87800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// (with bottom bit set) to the User.
88800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// \param IsPhi identifies callers which are phi nodes and which need
89800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// N BasicBlock* allocated along with N
90800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  void allocHungoffUses(unsigned N, bool IsPhi = false);
91800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
92800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// \brief Grow the number of hung off uses.  Note that allocHungoffUses
93800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// should be called if there are no uses.
94800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  void growHungoffUses(unsigned N, bool IsPhi = false);
95800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
96800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanypublic:
97800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  User(const User &) = delete;
98800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  ~User() override = default;
99800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
100800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// \brief Free memory allocated for User and Use objects.
101800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  void operator delete(void *Usr);
102800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// \brief Placement delete - required by std, but never called.
103800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  void operator delete(void*, unsigned) {
104800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    llvm_unreachable("Constructor throws?");
105800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
106800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// \brief Placement delete - required by std, but never called.
107800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  void operator delete(void*, unsigned, bool) {
108800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    llvm_unreachable("Constructor throws?");
109800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
110800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
111800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanyprotected:
112800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  template <int Idx, typename U> static Use &OpFrom(const U *that) {
113800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    return Idx < 0
114800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      ? OperandTraits<U>::op_end(const_cast<U*>(that))[Idx]
115800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      : OperandTraits<U>::op_begin(const_cast<U*>(that))[Idx];
116800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
117800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  template <int Idx> Use &Op() {
118800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    return OpFrom<Idx>(this);
119800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
120800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  template <int Idx> const Use &Op() const {
121800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    return OpFrom<Idx>(this);
122800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
123800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
124800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanyprivate:
125800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Use *&getHungOffOperands() { return *(reinterpret_cast<Use **>(this) - 1); }
126800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
127800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Use *getIntrusiveOperands() {
128800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    return reinterpret_cast<Use *>(this) - NumUserOperands;
129800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
130800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
131800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  void setOperandList(Use *NewList) {
132800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    assert(HasHungOffUses &&
133800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany           "Setting operand list only required for hung off uses");
134800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    getHungOffOperands() = NewList;
135800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
136800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
137800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanypublic:
138800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Use *getOperandList() {
139800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    return HasHungOffUses ? getHungOffOperands() : getIntrusiveOperands();
140800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
141800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  const Use *getOperandList() const {
142800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    return const_cast<User *>(this)->getOperandList();
143800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
144800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
145800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Value *getOperand(unsigned i) const {
14625878042030e85c244b41bfcdfad27c32360e2ecAlexander Potapenko    assert(i < NumUserOperands && "getOperand() out of range!");
147800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    return getOperandList()[i];
148800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
149800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
150800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  void setOperand(unsigned i, Value *Val) {
151800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    assert(i < NumUserOperands && "setOperand() out of range!");
152800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    assert((!isa<Constant>((const Value*)this) ||
153800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany            isa<GlobalValue>((const Value*)this)) &&
154800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany           "Cannot mutate a constant with setOperand!");
155800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    getOperandList()[i] = Val;
156800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
157800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
158a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany  const Use &getOperandUse(unsigned i) const {
159800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    assert(i < NumUserOperands && "getOperandUse() out of range!");
160800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    return getOperandList()[i];
161800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
162800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Use &getOperandUse(unsigned i) {
163800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    assert(i < NumUserOperands && "getOperandUse() out of range!");
164800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    return getOperandList()[i];
165800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
166800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
167800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  unsigned getNumOperands() const { return NumUserOperands; }
168800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
169800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// Returns the descriptor co-allocated with this User instance.
170800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  ArrayRef<const uint8_t> getDescriptor() const;
171800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
172800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// Returns the descriptor co-allocated with this User instance.
173800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  MutableArrayRef<uint8_t> getDescriptor();
174800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
175800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// Set the number of operands on a GlobalVariable.
176800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  ///
177800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// GlobalVariable always allocates space for a single operands, but
178800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// doesn't always use it.
179800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  ///
180800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// FIXME: As that the number of operands is used to find the start of
181800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// the allocated memory in operator delete, we need to always think we have
182800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// 1 operand before delete.
1835a3a9c937198084498a196dae856ac5a5a005bccKostya Serebryany  void setGlobalVariableNumOperands(unsigned NumOps) {
184800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    assert(NumOps <= 1 && "GlobalVariable can only have 0 or 1 operands");
185800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    NumUserOperands = NumOps;
186800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
187800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
188800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// \brief Subclasses with hung off uses need to manage the operand count
189800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// themselves.  In these instances, the operand count isn't used to find the
190800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// OperandList, so there's no issue in having the operand count change.
191800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  void setNumHungOffUseOperands(unsigned NumOps) {
192800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    assert(HasHungOffUses && "Must have hung off uses to use this method");
193800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    assert(NumOps < (1u << NumUserOperandsBits) && "Too many operands");
194800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    NumUserOperands = NumOps;
195800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
196800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
197800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  // ---------------------------------------------------------------------------
198800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  // Operand Iterator interface...
199800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  //
200800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  typedef Use*       op_iterator;
201800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  typedef const Use* const_op_iterator;
202800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  typedef iterator_range<op_iterator> op_range;
203800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  typedef iterator_range<const_op_iterator> const_op_range;
204800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
205800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  op_iterator       op_begin()       { return getOperandList(); }
206800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  const_op_iterator op_begin() const { return getOperandList(); }
207800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  op_iterator       op_end()         {
208800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    return getOperandList() + NumUserOperands;
209800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
21025878042030e85c244b41bfcdfad27c32360e2ecAlexander Potapenko  const_op_iterator op_end()   const {
21125878042030e85c244b41bfcdfad27c32360e2ecAlexander Potapenko    return getOperandList() + NumUserOperands;
21225878042030e85c244b41bfcdfad27c32360e2ecAlexander Potapenko  }
21325878042030e85c244b41bfcdfad27c32360e2ecAlexander Potapenko  op_range operands() {
214800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    return op_range(op_begin(), op_end());
215800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
21618c7f80b3e83ab584bd8572695a3cde8bafd9d3cChris Lattner  const_op_range operands() const {
217800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    return const_op_range(op_begin(), op_end());
218800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
219800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
220800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// \brief Iterator for directly iterating over the operand Values.
221800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  struct value_op_iterator
222800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      : iterator_adaptor_base<value_op_iterator, op_iterator,
223800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                              std::random_access_iterator_tag, Value *,
224800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                              ptrdiff_t, Value *, Value *> {
225800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    explicit value_op_iterator(Use *U = nullptr) : iterator_adaptor_base(U) {}
226800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
227800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    Value *operator*() const { return *I; }
228800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    Value *operator->() const { return operator*(); }
229800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  };
230800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
231800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  value_op_iterator value_op_begin() {
232800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    return value_op_iterator(op_begin());
233800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
234800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  value_op_iterator value_op_end() {
235800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    return value_op_iterator(op_end());
236800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
237800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  iterator_range<value_op_iterator> operand_values() {
238800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    return make_range(value_op_begin(), value_op_end());
239800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
240800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
241800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// \brief Drop all references to operands.
242800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  ///
243800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// This function is in charge of "letting go" of all objects that this User
244800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// refers to.  This allows one to 'delete' a whole class at a time, even
245800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// though there may be circular references...  First all references are
246800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// dropped, and all use counts go to zero.  Then everything is deleted for
247800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// real.  Note that no operations are valid on an object that has "dropped
248800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// all references", except operator delete.
249800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  void dropAllReferences() {
250800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    for (Use &U : operands())
251800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      U.set(nullptr);
252800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
253800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
254800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// \brief Replace uses of one Value with another.
255800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  ///
256800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// Replaces all references to the "From" definition with references to the
257800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  /// "To" definition.
258800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  void replaceUsesOfWith(Value *From, Value *To);
259800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
260800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  // Methods for support type inquiry through isa, cast, and dyn_cast:
261800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  static inline bool classof(const Value *V) {
262800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    return isa<Instruction>(V) || isa<Constant>(V);
263800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
264800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany};
265800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// Either Use objects, or a Use pointer can be prepended to User.
266800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic_assert(alignof(Use) >= alignof(User),
267800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany              "Alignment is insufficient after objects prepended to User");
268800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic_assert(alignof(Use *) >= alignof(User),
269800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany              "Alignment is insufficient after objects prepended to User");
270800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
271800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanytemplate<> struct simplify_type<User::op_iterator> {
272800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  typedef Value* SimpleType;
273800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  static SimpleType getSimplifiedValue(User::op_iterator &Val) {
274800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    return Val->get();
275800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
276800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany};
277800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanytemplate<> struct simplify_type<User::const_op_iterator> {
278800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  typedef /*const*/ Value* SimpleType;
279800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  static SimpleType getSimplifiedValue(User::const_op_iterator &Val) {
280800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    return Val->get();
281800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
282800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany};
283800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
284800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany} // end namespace llvm
285800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
286800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#endif // LLVM_IR_USER_H
287800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany