LLVM  3.7.0
User.h
Go to the documentation of this file.
1 //===-- llvm/User.h - User class definition ---------------------*- 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 class defines the interface that one who uses a Value must implement.
11 // Each instance of the Value class keeps track of what User's have handles
12 // to it.
13 //
14 // * Instructions are the largest class of Users.
15 // * Constants may be users of other constants (think arrays and stuff)
16 //
17 //===----------------------------------------------------------------------===//
18 
19 #ifndef LLVM_IR_USER_H
20 #define LLVM_IR_USER_H
21 
22 #include "llvm/ADT/iterator.h"
24 #include "llvm/IR/Value.h"
25 #include "llvm/Support/AlignOf.h"
27 
28 namespace llvm {
29 
30 /// \brief Compile-time customization of User operands.
31 ///
32 /// Customizes operand-related allocators and accessors.
33 template <class>
35 
36 class User : public Value {
37  User(const User &) = delete;
38  template <unsigned>
39  friend struct HungoffOperandTraits;
40  virtual void anchor();
41 
42 protected:
43  /// Allocate a User with an operand pointer co-allocated.
44  ///
45  /// This is used for subclasses which need to allocate a variable number
46  /// of operands, ie, 'hung off uses'.
47  void *operator new(size_t Size);
48 
49  /// Allocate a User with the operands co-allocated.
50  ///
51  /// This is used for subclasses which have a fixed number of operands.
52  void *operator new(size_t Size, unsigned Us);
53 
54  User(Type *ty, unsigned vty, Use *OpList, unsigned NumOps)
55  : Value(ty, vty) {
56  assert(NumOps < (1u << NumUserOperandsBits) && "Too many operands");
57  NumUserOperands = NumOps;
58  // If we have hung off uses, then the operand list should initially be
59  // null.
60  assert((!HasHungOffUses || !getOperandList()) &&
61  "Error in initializing hung off uses for User");
62  }
63 
64  /// \brief Allocate the array of Uses, followed by a pointer
65  /// (with bottom bit set) to the User.
66  /// \param IsPhi identifies callers which are phi nodes and which need
67  /// N BasicBlock* allocated along with N
68  void allocHungoffUses(unsigned N, bool IsPhi = false);
69 
70  /// \brief Grow the number of hung off uses. Note that allocHungoffUses
71  /// should be called if there are no uses.
72  void growHungoffUses(unsigned N, bool IsPhi = false);
73 
74 public:
75  ~User() override {
76  }
77  /// \brief Free memory allocated for User and Use objects.
78  void operator delete(void *Usr);
79  /// \brief Placement delete - required by std, but never called.
80  void operator delete(void*, unsigned) {
81  llvm_unreachable("Constructor throws?");
82  }
83  /// \brief Placement delete - required by std, but never called.
84  void operator delete(void*, unsigned, bool) {
85  llvm_unreachable("Constructor throws?");
86  }
87 protected:
88  template <int Idx, typename U> static Use &OpFrom(const U *that) {
89  return Idx < 0
90  ? OperandTraits<U>::op_end(const_cast<U*>(that))[Idx]
91  : OperandTraits<U>::op_begin(const_cast<U*>(that))[Idx];
92  }
93  template <int Idx> Use &Op() {
94  return OpFrom<Idx>(this);
95  }
96  template <int Idx> const Use &Op() const {
97  return OpFrom<Idx>(this);
98  }
99 private:
100  Use *&getHungOffOperands() { return *(reinterpret_cast<Use **>(this) - 1); }
101 
102  Use *getIntrusiveOperands() {
103  return reinterpret_cast<Use *>(this) - NumUserOperands;
104  }
105 
106  void setOperandList(Use *NewList) {
107  assert(HasHungOffUses &&
108  "Setting operand list only required for hung off uses");
109  getHungOffOperands() = NewList;
110  }
111 public:
113  return HasHungOffUses ? getHungOffOperands() : getIntrusiveOperands();
114  }
115  const Use *getOperandList() const {
116  return const_cast<User *>(this)->getOperandList();
117  }
118  Value *getOperand(unsigned i) const {
119  assert(i < NumUserOperands && "getOperand() out of range!");
120  return getOperandList()[i];
121  }
122  void setOperand(unsigned i, Value *Val) {
123  assert(i < NumUserOperands && "setOperand() out of range!");
124  assert((!isa<Constant>((const Value*)this) ||
125  isa<GlobalValue>((const Value*)this)) &&
126  "Cannot mutate a constant with setOperand!");
127  getOperandList()[i] = Val;
128  }
129  const Use &getOperandUse(unsigned i) const {
130  assert(i < NumUserOperands && "getOperandUse() out of range!");
131  return getOperandList()[i];
132  }
133  Use &getOperandUse(unsigned i) {
134  assert(i < NumUserOperands && "getOperandUse() out of range!");
135  return getOperandList()[i];
136  }
137 
138  unsigned getNumOperands() const { return NumUserOperands; }
139 
140  /// Set the number of operands on a GlobalVariable.
141  ///
142  /// GlobalVariable always allocates space for a single operands, but
143  /// doesn't always use it.
144  ///
145  /// FIXME: As that the number of operands is used to find the start of
146  /// the allocated memory in operator delete, we need to always think we have
147  /// 1 operand before delete.
148  void setGlobalVariableNumOperands(unsigned NumOps) {
149  assert(NumOps <= 1 && "GlobalVariable can only have 0 or 1 operands");
150  NumUserOperands = NumOps;
151  }
152 
153  /// Set the number of operands on a Function.
154  ///
155  /// Function always allocates space for a single operands, but
156  /// doesn't always use it.
157  ///
158  /// FIXME: As that the number of operands is used to find the start of
159  /// the allocated memory in operator delete, we need to always think we have
160  /// 1 operand before delete.
161  void setFunctionNumOperands(unsigned NumOps) {
162  assert(NumOps <= 1 && "Function can only have 0 or 1 operands");
163  NumUserOperands = NumOps;
164  }
165 
166  /// \brief Subclasses with hung off uses need to manage the operand count
167  /// themselves. In these instances, the operand count isn't used to find the
168  /// OperandList, so there's no issue in having the operand count change.
169  void setNumHungOffUseOperands(unsigned NumOps) {
170  assert(HasHungOffUses && "Must have hung off uses to use this method");
171  assert(NumOps < (1u << NumUserOperandsBits) && "Too many operands");
172  NumUserOperands = NumOps;
173  }
174 
175  // ---------------------------------------------------------------------------
176  // Operand Iterator interface...
177  //
178  typedef Use* op_iterator;
179  typedef const Use* const_op_iterator;
182 
186  return getOperandList() + NumUserOperands;
187  }
189  return getOperandList() + NumUserOperands;
190  }
192  return op_range(op_begin(), op_end());
193  }
195  return const_op_range(op_begin(), op_end());
196  }
197 
198  /// \brief Iterator for directly iterating over the operand Values.
200  : iterator_adaptor_base<value_op_iterator, op_iterator,
201  std::random_access_iterator_tag, Value *,
202  ptrdiff_t, Value *, Value *> {
203  explicit value_op_iterator(Use *U = nullptr) : iterator_adaptor_base(U) {}
204 
205  Value *operator*() const { return *I; }
206  Value *operator->() const { return operator*(); }
207  };
208 
210  return value_op_iterator(op_begin());
211  }
213  return value_op_iterator(op_end());
214  }
217  }
218 
219  /// \brief Drop all references to operands.
220  ///
221  /// This function is in charge of "letting go" of all objects that this User
222  /// refers to. This allows one to 'delete' a whole class at a time, even
223  /// though there may be circular references... First all references are
224  /// dropped, and all use counts go to zero. Then everything is deleted for
225  /// real. Note that no operations are valid on an object that has "dropped
226  /// all references", except operator delete.
228  for (Use &U : operands())
229  U.set(nullptr);
230  }
231 
232  /// \brief Replace uses of one Value with another.
233  ///
234  /// Replaces all references to the "From" definition with references to the
235  /// "To" definition.
236  void replaceUsesOfWith(Value *From, Value *To);
237 
238  // Methods for support type inquiry through isa, cast, and dyn_cast:
239  static inline bool classof(const Value *V) {
240  return isa<Instruction>(V) || isa<Constant>(V);
241  }
242 };
243 // Either Use objects, or a Use pointer can be prepended to User.
244 static_assert(AlignOf<Use>::Alignment >= AlignOf<User>::Alignment,
245  "Alignment is insufficient after objects prepended to User");
246 static_assert(AlignOf<Use *>::Alignment >= AlignOf<User>::Alignment,
247  "Alignment is insufficient after objects prepended to User");
248 
249 template<> struct simplify_type<User::op_iterator> {
250  typedef Value* SimpleType;
252  return Val->get();
253  }
254 };
255 template<> struct simplify_type<User::const_op_iterator> {
256  typedef /*const*/ Value* SimpleType;
258  return Val->get();
259  }
260 };
261 
262 } // End llvm namespace
263 
264 #endif
static Use & OpFrom(const U *that)
Definition: User.h:88
const Use & getOperandUse(unsigned i) const
Definition: User.h:129
void setFunctionNumOperands(unsigned NumOps)
Set the number of operands on a Function.
Definition: User.h:161
void dropAllReferences()
Drop all references to operands.
Definition: User.h:227
unsigned getNumOperands() const
Definition: User.h:138
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
value_op_iterator value_op_begin()
Definition: User.h:209
Use & getOperandUse(unsigned i)
Definition: User.h:133
value_op_iterator value_op_end()
Definition: User.h:212
iterator_range< op_iterator > op_range
Definition: User.h:180
const Use & Op() const
Definition: User.h:96
bool HasHungOffUses
Definition: Value.h:112
const_op_range operands() const
Definition: User.h:194
const Use * getOperandList() const
Definition: User.h:115
op_iterator op_begin()
Definition: User.h:183
static bool classof(const Value *V)
Definition: User.h:239
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
A Use represents the edge between a Value definition and its users.
Definition: Use.h:69
void growHungoffUses(unsigned N, bool IsPhi=false)
Grow the number of hung off uses.
Definition: User.cpp:63
const_op_iterator op_end() const
Definition: User.h:188
const_op_iterator op_begin() const
Definition: User.h:184
Use * getOperandList()
Definition: User.h:112
~User() override
Definition: User.h:75
void replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Definition: User.cpp:24
Value * operator->() const
Definition: User.h:206
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
CRTP base class for adapting an iterator to a different type.
Definition: iterator.h:145
op_iterator op_end()
Definition: User.h:185
Value * getOperand(unsigned i) const
Definition: User.h:118
op_range operands()
Definition: User.h:191
void setGlobalVariableNumOperands(unsigned NumOps)
Set the number of operands on a GlobalVariable.
Definition: User.h:148
static SimpleType getSimplifiedValue(User::const_op_iterator &Val)
Definition: User.h:257
Iterator for directly iterating over the operand Values.
Definition: User.h:199
unsigned NumUserOperands
Definition: Value.h:108
void setOperand(unsigned i, Value *Val)
Definition: User.h:122
A range adaptor for a pair of iterators.
iterator_range< const_op_iterator > const_op_range
Definition: User.h:181
#define N
User(Type *ty, unsigned vty, Use *OpList, unsigned NumOps)
Definition: User.h:54
iterator_range< value_op_iterator > operand_values()
Definition: User.h:215
Compile-time customization of User operands.
Definition: User.h:34
Value * operator*() const
Definition: User.h:205
Use * op_iterator
Definition: User.h:178
LLVM Value Representation.
Definition: Value.h:69
HungoffOperandTraits - determine the allocation regime of the Use array when it is not a prefix to th...
Definition: OperandTraits.h:93
void allocHungoffUses(unsigned N, bool IsPhi=false)
Allocate the array of Uses, followed by a pointer (with bottom bit set) to the User.
Definition: User.cpp:43
Use & Op()
Definition: User.h:93
void setNumHungOffUseOperands(unsigned NumOps)
Subclasses with hung off uses need to manage the operand count themselves.
Definition: User.h:169
static SimpleType getSimplifiedValue(User::op_iterator &Val)
Definition: User.h:251
value_op_iterator(Use *U=nullptr)
Definition: User.h:203
const Use * const_op_iterator
Definition: User.h:179