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
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-10-17 08:31:36 +00:00
|
|
|
// We adhere to the following convention: The type of a tagged pointer
|
|
|
|
// to T is T volatile*. This means that functions that superpose a tag
|
|
|
|
// on a pointer will be supplied a T* (or T const*) and will return a
|
|
|
|
// tagged one: T volatile*. Untagging functions do it the other way
|
|
|
|
// 'round. While this scheme does not prevent dereferencing of tagged
|
|
|
|
// pointers, proper type annotations do catch most inappropriate uses.
|
|
|
|
|
2008-05-10 08:32:32 +00:00
|
|
|
/// 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>
|
2008-10-16 15:33:02 +00:00
|
|
|
inline volatile T *addTag(const T *P, TAG Tag) {
|
2008-10-17 08:31:36 +00:00
|
|
|
return reinterpret_cast<T*>(ptrdiff_t(P) | Tag);
|
2008-05-10 08:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// stripTag - remove tag bits from a pointer,
|
|
|
|
/// making it dereferencable
|
|
|
|
template <ptrdiff_t MASK, typename T>
|
2008-10-16 15:33:02 +00:00
|
|
|
inline T *stripTag(const volatile T *P) {
|
2008-05-10 08:32:32 +00:00
|
|
|
return reinterpret_cast<T*>(ptrdiff_t(P) & ~MASK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// extractTag - extract tag bits from a pointer
|
|
|
|
template <typename TAG, TAG MASK, typename T>
|
2008-10-16 15:33:02 +00:00
|
|
|
inline TAG extractTag(const volatile T *P) {
|
2008-05-10 08:32:32 +00:00
|
|
|
return TAG(ptrdiff_t(P) & MASK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// transferTag - transfer tag bits from a pointer,
|
|
|
|
/// to an untagged pointer
|
|
|
|
template <ptrdiff_t MASK, typename T>
|
2008-10-16 15:33:02 +00:00
|
|
|
inline volatile T *transferTag(const volatile T *From, const T *To) {
|
2008-05-10 08:32:32 +00:00
|
|
|
return reinterpret_cast<T*>((ptrdiff_t(From) & MASK) | ptrdiff_t(To));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
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;
|
2008-10-16 15:33:02 +00:00
|
|
|
Use *Next, *volatile*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:13:20 +00:00
|
|
|
Prev = transferTag<fullStopTag>(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: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() {
|
2008-09-19 15:13:20 +00:00
|
|
|
Use **StrippedPrev = stripTag<fullStopTag>(Prev);
|
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> > {};
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2003-10-16 16:53:04 +00:00
|
|
|
#endif
|