#ifndef LLVM_IR_USER_H
#define LLVM_IR_USER_H
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/iterator.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/IR/Value.h"
#include "llvm/Support/AlignOf.h"
#include "llvm/Support/ErrorHandling.h"
namespace llvm {
template <class>
struct OperandTraits;
class User : public Value {
User(const User &) = delete;
template <unsigned>
friend struct HungoffOperandTraits;
virtual void anchor();
LLVM_ATTRIBUTE_ALWAYS_INLINE inline static void *
allocateFixedOperandUser(size_t, unsigned, unsigned);
protected:
void *operator new(size_t Size);
void *operator new(size_t Size, unsigned Us);
void *operator new(size_t Size, unsigned Us, unsigned DescBytes);
User(Type *ty, unsigned vty, Use *, unsigned NumOps)
: Value(ty, vty) {
assert(NumOps < (1u << NumUserOperandsBits) && "Too many operands");
NumUserOperands = NumOps;
assert((!HasHungOffUses || !getOperandList()) &&
"Error in initializing hung off uses for User");
}
void allocHungoffUses(unsigned N, bool IsPhi = false);
void growHungoffUses(unsigned N, bool IsPhi = false);
public:
~User() override {
}
void operator delete(void *Usr);
void operator delete(void*, unsigned) {
llvm_unreachable("Constructor throws?");
}
void operator delete(void*, unsigned, bool) {
llvm_unreachable("Constructor throws?");
}
protected:
template <int Idx, typename U> static Use &OpFrom(const U *that) {
return Idx < 0
? OperandTraits<U>::op_end(const_cast<U*>(that))[Idx]
: OperandTraits<U>::op_begin(const_cast<U*>(that))[Idx];
}
template <int Idx> Use &Op() {
return OpFrom<Idx>(this);
}
template <int Idx> const Use &Op() const {
return OpFrom<Idx>(this);
}
private:
Use *&getHungOffOperands() { return *(reinterpret_cast<Use **>(this) - 1); }
Use *getIntrusiveOperands() {
return reinterpret_cast<Use *>(this) - NumUserOperands;
}
void setOperandList(Use *NewList) {
assert(HasHungOffUses &&
"Setting operand list only required for hung off uses");
getHungOffOperands() = NewList;
}
public:
Use *getOperandList() {
return HasHungOffUses ? getHungOffOperands() : getIntrusiveOperands();
}
const Use *getOperandList() const {
return const_cast<User *>(this)->getOperandList();
}
Value *getOperand(unsigned i) const {
assert(i < NumUserOperands && "getOperand() out of range!");
return getOperandList()[i];
}
void setOperand(unsigned i, Value *Val) {
assert(i < NumUserOperands && "setOperand() out of range!");
assert((!isa<Constant>((const Value*)this) ||
isa<GlobalValue>((const Value*)this)) &&
"Cannot mutate a constant with setOperand!");
getOperandList()[i] = Val;
}
const Use &getOperandUse(unsigned i) const {
assert(i < NumUserOperands && "getOperandUse() out of range!");
return getOperandList()[i];
}
Use &getOperandUse(unsigned i) {
assert(i < NumUserOperands && "getOperandUse() out of range!");
return getOperandList()[i];
}
unsigned getNumOperands() const { return NumUserOperands; }
ArrayRef<const uint8_t> getDescriptor() const;
MutableArrayRef<uint8_t> getDescriptor();
void setGlobalVariableNumOperands(unsigned NumOps) {
assert(NumOps <= 1 && "GlobalVariable can only have 0 or 1 operands");
NumUserOperands = NumOps;
}
void setNumHungOffUseOperands(unsigned NumOps) {
assert(HasHungOffUses && "Must have hung off uses to use this method");
assert(NumOps < (1u << NumUserOperandsBits) && "Too many operands");
NumUserOperands = NumOps;
}
typedef Use* op_iterator;
typedef const Use* const_op_iterator;
typedef iterator_range<op_iterator> op_range;
typedef iterator_range<const_op_iterator> const_op_range;
op_iterator op_begin() { return getOperandList(); }
const_op_iterator op_begin() const { return getOperandList(); }
op_iterator op_end() {
return getOperandList() + NumUserOperands;
}
const_op_iterator op_end() const {
return getOperandList() + NumUserOperands;
}
op_range operands() {
return op_range(op_begin(), op_end());
}
const_op_range operands() const {
return const_op_range(op_begin(), op_end());
}
struct value_op_iterator
: iterator_adaptor_base<value_op_iterator, op_iterator,
std::random_access_iterator_tag, Value *,
ptrdiff_t, Value *, Value *> {
explicit value_op_iterator(Use *U = nullptr) : iterator_adaptor_base(U) {}
Value *operator*() const { return *I; }
Value *operator->() const { return operator*(); }
};
value_op_iterator value_op_begin() {
return value_op_iterator(op_begin());
}
value_op_iterator value_op_end() {
return value_op_iterator(op_end());
}
iterator_range<value_op_iterator> operand_values() {
return make_range(value_op_begin(), value_op_end());
}
void dropAllReferences() {
for (Use &U : operands())
U.set(nullptr);
}
void replaceUsesOfWith(Value *From, Value *To);
static inline bool classof(const Value *V) {
return isa<Instruction>(V) || isa<Constant>(V);
}
};
static_assert(AlignOf<Use>::Alignment >= AlignOf<User>::Alignment,
"Alignment is insufficient after objects prepended to User");
static_assert(AlignOf<Use *>::Alignment >= AlignOf<User>::Alignment,
"Alignment is insufficient after objects prepended to User");
template<> struct simplify_type<User::op_iterator> {
typedef Value* SimpleType;
static SimpleType getSimplifiedValue(User::op_iterator &Val) {
return Val->get();
}
};
template<> struct simplify_type<User::const_op_iterator> {
typedef Value* SimpleType;
static SimpleType getSimplifiedValue(User::const_op_iterator &Val) {
return Val->get();
}
};
}
#endif