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"
|
2003-11-11 22:41:34 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2003-10-16 16:53:04 +00:00
|
|
|
class Value;
|
|
|
|
class User;
|
|
|
|
|
|
|
|
|
2008-05-10 08:32:32 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Generic Tagging Functions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// Tag - generic tag type for (at least 32 bit) pointers
|
|
|
|
enum Tag { noTag, tagOne, tagTwo, tagThree };
|
|
|
|
|
|
|
|
/// addTag - insert tag bits into an (untagged) pointer
|
|
|
|
template <typename T, typename TAG>
|
|
|
|
inline T *addTag(const T *P, TAG Tag) {
|
|
|
|
return reinterpret_cast<T*>(ptrdiff_t(P) | Tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// stripTag - remove tag bits from a pointer,
|
|
|
|
/// making it dereferencable
|
|
|
|
template <ptrdiff_t MASK, typename T>
|
|
|
|
inline T *stripTag(const T *P) {
|
|
|
|
return reinterpret_cast<T*>(ptrdiff_t(P) & ~MASK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// extractTag - extract tag bits from a pointer
|
|
|
|
template <typename TAG, TAG MASK, typename T>
|
|
|
|
inline TAG extractTag(const T *P) {
|
|
|
|
return TAG(ptrdiff_t(P) & MASK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// transferTag - transfer tag bits from a pointer,
|
|
|
|
/// to an untagged pointer
|
|
|
|
template <ptrdiff_t MASK, typename T>
|
|
|
|
inline T *transferTag(const T *From, const T *To) {
|
|
|
|
return reinterpret_cast<T*>((ptrdiff_t(From) & MASK) | ptrdiff_t(To));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-16 16:53:04 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Use Class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// Use is here to make keeping the "use" list of a Value up-to-date really easy.
|
|
|
|
//
|
|
|
|
class Use {
|
2008-09-19 15:03:57 +00:00
|
|
|
class UseWaymark;
|
|
|
|
friend class UseWaymark;
|
|
|
|
Value *getValue() const;
|
|
|
|
/// nilUse - returns a 'token' that marks the end of the def/use chain
|
|
|
|
static Use *nilUse(const Value *V) {
|
|
|
|
return addTag((Use*)V, fullStopTagN);
|
|
|
|
}
|
|
|
|
static bool isNil(Use *U) { return extractTag<NextPtrTag, tagMaskN>(U) == fullStopTagN; }
|
|
|
|
void showWaymarks() const;
|
|
|
|
static bool isStop(Use *U) {
|
|
|
|
return isStopTag(extractTag<NextPtrTag, tagMaskN>(U));
|
|
|
|
}
|
|
|
|
public:
|
2008-05-13 22:51:52 +00:00
|
|
|
/// init - specify Value and User
|
|
|
|
/// @deprecated in 2.4, will be removed soon
|
2005-01-29 00:30:52 +00:00
|
|
|
inline void init(Value *V, User *U);
|
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:03:57 +00:00
|
|
|
if (Val1) removeFromList();
|
2008-03-14 22:03:02 +00:00
|
|
|
}
|
2003-10-16 16:53:04 +00:00
|
|
|
|
2008-05-10 08:32:32 +00:00
|
|
|
/// Default ctor - This leaves the Use completely uninitialized. The only thing
|
2005-01-29 00:30:52 +00:00
|
|
|
/// 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:03:57 +00:00
|
|
|
, fullStopTag = tagThree
|
|
|
|
, tagMask = tagThree };
|
|
|
|
|
|
|
|
enum NextPtrTag { zeroDigitTagN = tagTwo
|
|
|
|
, oneDigitTagN = tagOne
|
|
|
|
, stopTagN = noTag
|
|
|
|
, fullStopTagN = tagThree
|
|
|
|
, tagMaskN = tagThree };
|
|
|
|
|
|
|
|
static bool isStopTag(NextPtrTag T) {
|
|
|
|
bool P[] = { true, false, false, true };
|
|
|
|
return P[T];
|
|
|
|
}
|
2008-05-10 08:32:32 +00:00
|
|
|
public:
|
2008-09-19 15:03:57 +00:00
|
|
|
operator Value*() const { return get(); }
|
|
|
|
inline Value *get() const;
|
2008-05-10 08:32:32 +00:00
|
|
|
User *getUser() const;
|
|
|
|
const Use* getImpliedUser() const;
|
|
|
|
static Use *initTags(Use *Start, Use *Stop, ptrdiff_t Done = 0);
|
|
|
|
static void zap(Use *Start, const Use *Stop, bool del = false);
|
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:03:57 +00:00
|
|
|
set(RHS.Val1);
|
2003-10-16 16:53:04 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2008-09-19 15:03:57 +00:00
|
|
|
Value *operator->() { return get(); }
|
|
|
|
const Value *operator->() const { return get(); }
|
2005-01-29 18:43:28 +00:00
|
|
|
|
2008-09-19 15:03:57 +00:00
|
|
|
Use *getNext() const { return extractTag<NextPtrTag, tagMaskN>(Next) == fullStopTagN
|
|
|
|
? 0
|
|
|
|
: stripTag<tagMaskN>(Next); }
|
2005-01-29 18:43:28 +00:00
|
|
|
private:
|
2008-09-19 15:03:57 +00:00
|
|
|
Value *Val1;
|
2008-05-10 08:32:32 +00:00
|
|
|
Use *Next, **Prev;
|
2003-10-16 16:53:04 +00:00
|
|
|
|
2008-05-10 08:32:32 +00:00
|
|
|
void setPrev(Use **NewPrev) {
|
2008-09-19 15:03:57 +00:00
|
|
|
Prev = transferTag<tagMask>(Prev, 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:03:57 +00:00
|
|
|
Use *StrippedNext(getNext());
|
|
|
|
if (StrippedNext) StrippedNext->setPrev(&Next);
|
2008-05-10 08:32:32 +00:00
|
|
|
setPrev(List);
|
2005-02-01 01:22:06 +00:00
|
|
|
*List = this;
|
|
|
|
}
|
|
|
|
void removeFromList() {
|
2008-09-19 15:03:57 +00:00
|
|
|
// __builtin_prefetch(Next);
|
|
|
|
Use **StrippedPrev = stripTag<tagMask>(Prev);
|
|
|
|
Use *StrippedNext(getNext());
|
|
|
|
if (isStop(Next))
|
|
|
|
assert((isStop(*StrippedPrev) || (StrippedNext ? isStop(StrippedNext->Next) : true)) && "joining digits?");
|
2008-05-10 08:32:32 +00:00
|
|
|
*StrippedPrev = Next;
|
2008-09-19 15:03:57 +00:00
|
|
|
if (StrippedNext) StrippedNext->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;
|
|
|
|
};
|
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:03:57 +00:00
|
|
|
explicit value_use_iterator(Use *u) : U(extractTag<Use::NextPtrTag, Use::tagMaskN>(u)
|
|
|
|
== Use::fullStopTagN
|
|
|
|
? 0
|
|
|
|
: stripTag<Use::tagMaskN>(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:03:57 +00:00
|
|
|
bool atEnd() const { return !U; }
|
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:03:57 +00:00
|
|
|
assert(!atEnd() && "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:03:57 +00:00
|
|
|
// Retrieve a reference to the current User
|
2006-03-27 22:49:07 +00:00
|
|
|
UserTy *operator*() const {
|
2008-09-19 15:03:57 +00:00
|
|
|
assert(!atEnd() && "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
|
|
|
};
|
|
|
|
|
2008-09-19 15:03:57 +00:00
|
|
|
Value *Use::get() const {
|
|
|
|
return fullStopTagN == extractTag<NextPtrTag, tagMaskN>(Next)
|
|
|
|
? reinterpret_cast<Value*>(stripTag<tagMaskN>(Next))
|
|
|
|
: (Val1 == getValue() ? Val1 : 0); // should crash if not equal!
|
|
|
|
}
|
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> > {};
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2003-10-16 16:53:04 +00:00
|
|
|
#endif
|