#ifndef LLVM_USE_H
#define LLVM_USE_H
#include "llvm/Support/Casting.h"
#include "llvm/ADT/PointerIntPair.h"
#include <iterator>
namespace llvm {
class Value;
class User;
class Use;
enum Tag { noTag, tagOne, tagTwo, tagThree };
template<>
class PointerLikeTypeTraits<Use**> {
public:
static inline void *getAsVoidPointer(Use** P) { return P; }
static inline Use **getFromVoidPointer(void *P) {
return static_cast<Use**>(P);
}
enum { NumLowBitsAvailable = 2 };
};
class Use {
public:
void swap(Use &RHS);
private:
Use(const Use &U);
inline ~Use() {
if (Val) removeFromList();
}
inline Use() {}
enum PrevPtrTag { zeroDigitTag = noTag
, oneDigitTag = tagOne
, stopTag = tagTwo
, fullStopTag = tagThree };
public:
operator Value*() const { return Val; }
Value *get() const { return Val; }
User *getUser() const;
inline void set(Value *Val);
Value *operator=(Value *RHS) {
set(RHS);
return RHS;
}
const Use &operator=(const Use &RHS) {
set(RHS.Val);
return *this;
}
Value *operator->() { return Val; }
const Value *operator->() const { return Val; }
Use *getNext() const { return Next; }
static void zap(Use *Start, const Use *Stop, bool del = false);
Use *getPrefix();
private:
const Use* getImpliedUser() const;
static Use *initTags(Use *Start, Use *Stop, ptrdiff_t Done = 0);
Value *Val;
Use *Next;
PointerIntPair<Use**, 2, PrevPtrTag> Prev;
void setPrev(Use **NewPrev) {
Prev.setPointer(NewPrev);
}
void addToList(Use **List) {
Next = *List;
if (Next) Next->setPrev(&Next);
setPrev(List);
*List = this;
}
void removeFromList() {
Use **StrippedPrev = Prev.getPointer();
*StrippedPrev = Next;
if (Next) Next->setPrev(StrippedPrev);
}
friend class Value;
friend class User;
};
template<> struct simplify_type<Use> {
typedef Value* SimpleType;
static SimpleType getSimplifiedValue(const Use &Val) {
return static_cast<SimpleType>(Val.get());
}
};
template<> struct simplify_type<const Use> {
typedef Value* SimpleType;
static SimpleType getSimplifiedValue(const Use &Val) {
return static_cast<SimpleType>(Val.get());
}
};
template<typename UserTy> class value_use_iterator : public std::iterator<std::forward_iterator_tag,
UserTy*, ptrdiff_t> {
typedef std::iterator<std::forward_iterator_tag, UserTy*, ptrdiff_t> super;
typedef value_use_iterator<UserTy> _Self;
Use *U;
explicit value_use_iterator(Use *u) : U(u) {}
friend class Value;
public:
typedef typename super::reference reference;
typedef typename super::pointer pointer;
value_use_iterator(const _Self &I) : U(I.U) {}
value_use_iterator() {}
bool operator==(const _Self &x) const {
return U == x.U;
}
bool operator!=(const _Self &x) const {
return !operator==(x);
}
bool atEnd() const { return U == 0; }
_Self &operator++() { assert(U && "Cannot increment end iterator!");
U = U->getNext();
return *this;
}
_Self operator++(int) { _Self tmp = *this; ++*this; return tmp;
}
UserTy *operator*() const {
assert(U && "Cannot dereference end iterator!");
return U->getUser();
}
UserTy *operator->() const { return operator*(); }
Use &getUse() const { return *U; }
unsigned getOperandNo() const;
};
template<> struct simplify_type<value_use_iterator<User> > {
typedef User* SimpleType;
static SimpleType getSimplifiedValue(const value_use_iterator<User> &Val) {
return *Val;
}
};
template<> struct simplify_type<const value_use_iterator<User> >
: public simplify_type<value_use_iterator<User> > {};
template<> struct simplify_type<value_use_iterator<const User> > {
typedef const User* SimpleType;
static SimpleType getSimplifiedValue(const
value_use_iterator<const User> &Val) {
return *Val;
}
};
template<> struct simplify_type<const value_use_iterator<const User> >
: public simplify_type<value_use_iterator<const User> > {};
}
#endif