2003-10-16 16:53:04 +00:00
|
|
|
//===-- llvm/Use.h - Definition of the Use class ----------------*- C++ -*-===//
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2003-10-20 20:19:47 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:42 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2003-10-20 20:19:47 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-10-16 16:53:04 +00:00
|
|
|
//
|
|
|
|
// This defines the Use class. The Use class represents the operand of an
|
|
|
|
// instruction or some other User instance which refers to a Value. The Use
|
|
|
|
// class keeps the "use list" of the referenced value up to date.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_USE_H
|
|
|
|
#define LLVM_USE_H
|
|
|
|
|
2005-02-01 01:22:06 +00:00
|
|
|
#include "llvm/Support/Casting.h"
|
2008-05-29 17:41:17 +00:00
|
|
|
#include "llvm/ADT/iterator.h"
|
2009-01-05 16:05:32 +00:00
|
|
|
#include "llvm/ADT/PointerIntPair.h"
|
2003-11-11 22:41:34 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2003-10-16 16:53:04 +00:00
|
|
|
class Value;
|
|
|
|
class User;
|
2009-03-29 04:32:37 +00:00
|
|
|
class Use;
|
2003-10-16 16:53:04 +00:00
|
|
|
|
2008-05-10 08:32:32 +00:00
|
|
|
/// Tag - generic tag type for (at least 32 bit) pointers
|
|
|
|
enum Tag { noTag, tagOne, tagTwo, tagThree };
|
|
|
|
|
2003-10-16 16:53:04 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Use Class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-10-15 16:56:14 +00:00
|
|
|
/// Use is here to make keeping the "use" list of a Value up-to-date really
|
|
|
|
/// easy.
|
2003-10-16 16:53:04 +00:00
|
|
|
class Use {
|
2008-09-19 15:13:20 +00:00
|
|
|
public:
|
2008-05-13 22:51:52 +00:00
|
|
|
/// swap - provide a fast substitute to std::swap<Use>
|
|
|
|
/// that also works with less standard-compliant compilers
|
|
|
|
void swap(Use &RHS);
|
2005-01-29 00:30:52 +00:00
|
|
|
|
2008-05-10 08:32:32 +00:00
|
|
|
private:
|
2008-05-13 22:51:52 +00:00
|
|
|
/// Copy ctor - do not implement
|
|
|
|
Use(const Use &U);
|
2008-05-10 08:32:32 +00:00
|
|
|
|
2008-05-13 22:51:52 +00:00
|
|
|
/// Destructor - Only for zap()
|
2008-03-14 22:03:02 +00:00
|
|
|
inline ~Use() {
|
2008-09-19 15:13:20 +00:00
|
|
|
if (Val) removeFromList();
|
2008-03-14 22:03:02 +00:00
|
|
|
}
|
2003-10-16 16:53:04 +00:00
|
|
|
|
2008-10-15 16:56:14 +00:00
|
|
|
/// Default ctor - This leaves the Use completely uninitialized. The only
|
|
|
|
/// thing that is valid to do with this use is to call the "init" method.
|
2008-05-10 08:32:32 +00:00
|
|
|
inline Use() {}
|
|
|
|
enum PrevPtrTag { zeroDigitTag = noTag
|
|
|
|
, oneDigitTag = tagOne
|
|
|
|
, stopTag = tagTwo
|
2008-09-19 15:13:20 +00:00
|
|
|
, fullStopTag = tagThree };
|
|
|
|
|
2008-05-10 08:32:32 +00:00
|
|
|
public:
|
2008-10-15 16:56:14 +00:00
|
|
|
/// Normally Use will just implicitly convert to a Value* that it holds.
|
2008-09-19 15:13:20 +00:00
|
|
|
operator Value*() const { return Val; }
|
2008-10-15 16:56:14 +00:00
|
|
|
|
|
|
|
/// If implicit conversion to Value* doesn't work, the get() method returns
|
|
|
|
/// the Value*.
|
2008-09-19 15:13:20 +00:00
|
|
|
Value *get() const { return Val; }
|
2008-10-15 16:56:14 +00:00
|
|
|
|
|
|
|
/// getUser - This returns the User that contains this Use. For an
|
|
|
|
/// instruction operand, for example, this will return the instruction.
|
2008-05-10 08:32:32 +00:00
|
|
|
User *getUser() const;
|
2003-10-16 16:53:04 +00:00
|
|
|
|
|
|
|
inline void set(Value *Val);
|
|
|
|
|
|
|
|
Value *operator=(Value *RHS) {
|
|
|
|
set(RHS);
|
|
|
|
return RHS;
|
|
|
|
}
|
|
|
|
const Use &operator=(const Use &RHS) {
|
2008-09-19 15:13:20 +00:00
|
|
|
set(RHS.Val);
|
2003-10-16 16:53:04 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2008-09-19 15:13:20 +00:00
|
|
|
Value *operator->() { return Val; }
|
|
|
|
const Value *operator->() const { return Val; }
|
2005-01-29 18:43:28 +00:00
|
|
|
|
2008-09-19 15:13:20 +00:00
|
|
|
Use *getNext() const { return Next; }
|
2008-10-15 16:56:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
/// zap - This is used to destroy Use operands when the number of operands of
|
|
|
|
/// a User changes.
|
|
|
|
static void zap(Use *Start, const Use *Stop, bool del = false);
|
|
|
|
|
2009-03-12 18:34:49 +00:00
|
|
|
/// getPrefix - Return deletable pointer if appropriate
|
|
|
|
Use *getPrefix();
|
2005-01-29 18:43:28 +00:00
|
|
|
private:
|
2008-10-15 16:56:14 +00:00
|
|
|
const Use* getImpliedUser() const;
|
|
|
|
static Use *initTags(Use *Start, Use *Stop, ptrdiff_t Done = 0);
|
|
|
|
|
2008-09-19 15:13:20 +00:00
|
|
|
Value *Val;
|
2009-01-05 16:05:32 +00:00
|
|
|
Use *Next;
|
2009-01-05 16:28:14 +00:00
|
|
|
PointerIntPair<Use**, 2, PrevPtrTag> Prev;
|
2003-10-16 16:53:04 +00:00
|
|
|
|
2008-05-10 08:32:32 +00:00
|
|
|
void setPrev(Use **NewPrev) {
|
2009-01-05 16:05:32 +00:00
|
|
|
Prev.setPointer(NewPrev);
|
2008-05-10 08:32:32 +00:00
|
|
|
}
|
2005-02-01 01:22:06 +00:00
|
|
|
void addToList(Use **List) {
|
|
|
|
Next = *List;
|
2008-09-19 15:13:20 +00:00
|
|
|
if (Next) Next->setPrev(&Next);
|
2008-05-10 08:32:32 +00:00
|
|
|
setPrev(List);
|
2005-02-01 01:22:06 +00:00
|
|
|
*List = this;
|
|
|
|
}
|
|
|
|
void removeFromList() {
|
2009-01-05 16:05:32 +00:00
|
|
|
Use **StrippedPrev = Prev.getPointer();
|
2008-05-10 08:32:32 +00:00
|
|
|
*StrippedPrev = Next;
|
2008-09-19 15:13:20 +00:00
|
|
|
if (Next) Next->setPrev(StrippedPrev);
|
2005-02-01 01:22:06 +00:00
|
|
|
}
|
2003-10-16 16:53:04 +00:00
|
|
|
|
2005-02-01 01:22:06 +00:00
|
|
|
friend class Value;
|
2008-10-15 16:56:14 +00:00
|
|
|
friend class User;
|
2005-02-01 01:22:06 +00:00
|
|
|
};
|
2003-10-16 16:53:04 +00:00
|
|
|
|
2005-02-01 01:22:06 +00:00
|
|
|
// simplify_type - Allow clients to treat uses just like values when using
|
|
|
|
// casting operators.
|
2003-11-14 06:03:05 +00:00
|
|
|
template<> struct simplify_type<Use> {
|
2003-10-16 16:53:04 +00:00
|
|
|
typedef Value* SimpleType;
|
|
|
|
static SimpleType getSimplifiedValue(const Use &Val) {
|
2003-11-16 20:21:15 +00:00
|
|
|
return static_cast<SimpleType>(Val.get());
|
2003-10-16 16:53:04 +00:00
|
|
|
}
|
|
|
|
};
|
2003-11-14 06:03:05 +00:00
|
|
|
template<> struct simplify_type<const Use> {
|
2003-10-16 16:53:04 +00:00
|
|
|
typedef Value* SimpleType;
|
|
|
|
static SimpleType getSimplifiedValue(const Use &Val) {
|
2003-11-16 20:21:15 +00:00
|
|
|
return static_cast<SimpleType>(Val.get());
|
2003-10-16 16:53:04 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-02-01 01:22:06 +00:00
|
|
|
template<typename UserTy> // UserTy == 'User' or 'const User'
|
|
|
|
class value_use_iterator : public forward_iterator<UserTy*, ptrdiff_t> {
|
|
|
|
typedef forward_iterator<UserTy*, ptrdiff_t> super;
|
|
|
|
typedef value_use_iterator<UserTy> _Self;
|
|
|
|
|
|
|
|
Use *U;
|
2008-09-19 15:13:20 +00:00
|
|
|
explicit value_use_iterator(Use *u) : U(u) {}
|
2005-02-01 01:22:06 +00:00
|
|
|
friend class Value;
|
|
|
|
public:
|
|
|
|
typedef typename super::reference reference;
|
|
|
|
typedef typename super::pointer pointer;
|
2003-10-16 16:53:04 +00:00
|
|
|
|
2005-02-01 01:22:06 +00:00
|
|
|
value_use_iterator(const _Self &I) : U(I.U) {}
|
|
|
|
value_use_iterator() {}
|
2003-10-16 16:53:04 +00:00
|
|
|
|
2005-04-21 20:19:05 +00:00
|
|
|
bool operator==(const _Self &x) const {
|
2005-02-01 01:22:06 +00:00
|
|
|
return U == x.U;
|
2003-10-16 16:53:04 +00:00
|
|
|
}
|
2005-02-01 01:22:06 +00:00
|
|
|
bool operator!=(const _Self &x) const {
|
|
|
|
return !operator==(x);
|
2003-10-16 16:53:04 +00:00
|
|
|
}
|
2008-03-31 17:09:58 +00:00
|
|
|
|
2007-10-11 04:18:11 +00:00
|
|
|
/// atEnd - return true if this iterator is equal to use_end() on the value.
|
2008-09-19 15:13:20 +00:00
|
|
|
bool atEnd() const { return U == 0; }
|
2003-10-16 16:53:04 +00:00
|
|
|
|
2005-02-01 01:22:06 +00:00
|
|
|
// Iterator traversal: forward iteration only
|
|
|
|
_Self &operator++() { // Preincrement
|
2008-09-19 15:13:20 +00:00
|
|
|
assert(U && "Cannot increment end iterator!");
|
2005-02-01 01:22:06 +00:00
|
|
|
U = U->getNext();
|
2005-04-21 20:19:05 +00:00
|
|
|
return *this;
|
2005-02-01 01:22:06 +00:00
|
|
|
}
|
|
|
|
_Self operator++(int) { // Postincrement
|
2005-04-21 20:19:05 +00:00
|
|
|
_Self tmp = *this; ++*this; return tmp;
|
2003-10-16 16:53:04 +00:00
|
|
|
}
|
|
|
|
|
2008-09-19 15:13:20 +00:00
|
|
|
// Retrieve a pointer to the current User.
|
2006-03-27 22:49:07 +00:00
|
|
|
UserTy *operator*() const {
|
2008-09-19 15:13:20 +00:00
|
|
|
assert(U && "Cannot dereference end iterator!");
|
2005-02-01 01:22:06 +00:00
|
|
|
return U->getUser();
|
|
|
|
}
|
2003-10-16 16:53:04 +00:00
|
|
|
|
2005-02-01 01:22:06 +00:00
|
|
|
UserTy *operator->() const { return operator*(); }
|
2003-10-16 16:53:04 +00:00
|
|
|
|
2005-02-01 01:22:06 +00:00
|
|
|
Use &getUse() const { return *U; }
|
2006-05-05 00:51:42 +00:00
|
|
|
|
2006-05-08 05:59:36 +00:00
|
|
|
/// getOperandNo - Return the operand # of this use in its User. Defined in
|
|
|
|
/// User.h
|
2006-05-05 00:51:42 +00:00
|
|
|
///
|
2006-05-08 05:59:36 +00:00
|
|
|
unsigned getOperandNo() const;
|
2003-10-16 16:53:04 +00:00
|
|
|
};
|
|
|
|
|
2006-03-27 22:49:07 +00:00
|
|
|
|
|
|
|
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> > {};
|
2009-03-29 04:32:37 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2003-10-16 16:53:04 +00:00
|
|
|
#endif
|