2003-10-16 18:53:04 +02:00
|
|
|
//===-- llvm/Use.h - Definition of the Use class ----------------*- C++ -*-===//
|
2005-04-21 22:19:05 +02:00
|
|
|
//
|
2003-10-20 22:19:47 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:59:42 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 22:19:05 +02:00
|
|
|
//
|
2003-10-20 22:19:47 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-10-16 18:53:04 +02: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.
|
|
|
|
//
|
2009-05-01 01:28:50 +02:00
|
|
|
// Pointer tagging is used to efficiently find the User corresponding
|
|
|
|
// to a Use without having to store a User pointer in every Use. A
|
|
|
|
// User is preceded in memory by all the Uses corresponding to its
|
|
|
|
// operands, and the low bits of one of the fields (Prev) of the Use
|
|
|
|
// class are used to encode offsets to be able to find that User given
|
|
|
|
// a pointer to any Use. For details, see:
|
|
|
|
//
|
|
|
|
// http://www.llvm.org/docs/ProgrammersManual.html#UserLayout
|
|
|
|
//
|
2003-10-16 18:53:04 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_USE_H
|
|
|
|
#define LLVM_USE_H
|
|
|
|
|
2005-02-01 02:22:06 +01:00
|
|
|
#include "llvm/Support/Casting.h"
|
2008-05-29 19:41:17 +02:00
|
|
|
#include "llvm/ADT/iterator.h"
|
2009-01-05 17:05:32 +01:00
|
|
|
#include "llvm/ADT/PointerIntPair.h"
|
2003-11-11 23:41:34 +01:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2003-10-16 18:53:04 +02:00
|
|
|
class Value;
|
|
|
|
class User;
|
2009-03-29 06:32:37 +02:00
|
|
|
class Use;
|
2003-10-16 18:53:04 +02:00
|
|
|
|
2008-05-10 10:32:32 +02:00
|
|
|
/// Tag - generic tag type for (at least 32 bit) pointers
|
|
|
|
enum Tag { noTag, tagOne, tagTwo, tagThree };
|
|
|
|
|
2009-04-03 02:26:01 +02:00
|
|
|
// Use** is only 4-byte aligned.
|
|
|
|
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 };
|
|
|
|
};
|
|
|
|
|
2003-10-16 18:53:04 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Use Class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-10-15 18:56:14 +02:00
|
|
|
/// Use is here to make keeping the "use" list of a Value up-to-date really
|
|
|
|
/// easy.
|
2003-10-16 18:53:04 +02:00
|
|
|
class Use {
|
2008-09-19 17:13:20 +02:00
|
|
|
public:
|
2008-05-14 00:51:52 +02: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 01:30:52 +01:00
|
|
|
|
2008-05-10 10:32:32 +02:00
|
|
|
private:
|
2008-05-14 00:51:52 +02:00
|
|
|
/// Copy ctor - do not implement
|
|
|
|
Use(const Use &U);
|
2008-05-10 10:32:32 +02:00
|
|
|
|
2008-05-14 00:51:52 +02:00
|
|
|
/// Destructor - Only for zap()
|
2008-03-14 23:03:02 +01:00
|
|
|
inline ~Use() {
|
2008-09-19 17:13:20 +02:00
|
|
|
if (Val) removeFromList();
|
2008-03-14 23:03:02 +01:00
|
|
|
}
|
2003-10-16 18:53:04 +02:00
|
|
|
|
2008-10-15 18:56:14 +02: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 10:32:32 +02:00
|
|
|
inline Use() {}
|
|
|
|
enum PrevPtrTag { zeroDigitTag = noTag
|
|
|
|
, oneDigitTag = tagOne
|
|
|
|
, stopTag = tagTwo
|
2008-09-19 17:13:20 +02:00
|
|
|
, fullStopTag = tagThree };
|
|
|
|
|
2008-05-10 10:32:32 +02:00
|
|
|
public:
|
2008-10-15 18:56:14 +02:00
|
|
|
/// Normally Use will just implicitly convert to a Value* that it holds.
|
2008-09-19 17:13:20 +02:00
|
|
|
operator Value*() const { return Val; }
|
2008-10-15 18:56:14 +02:00
|
|
|
|
|
|
|
/// If implicit conversion to Value* doesn't work, the get() method returns
|
|
|
|
/// the Value*.
|
2008-09-19 17:13:20 +02:00
|
|
|
Value *get() const { return Val; }
|
2008-10-15 18:56:14 +02:00
|
|
|
|
|
|
|
/// getUser - This returns the User that contains this Use. For an
|
|
|
|
/// instruction operand, for example, this will return the instruction.
|
2008-05-10 10:32:32 +02:00
|
|
|
User *getUser() const;
|
2003-10-16 18:53:04 +02:00
|
|
|
|
|
|
|
inline void set(Value *Val);
|
|
|
|
|
|
|
|
Value *operator=(Value *RHS) {
|
|
|
|
set(RHS);
|
|
|
|
return RHS;
|
|
|
|
}
|
|
|
|
const Use &operator=(const Use &RHS) {
|
2008-09-19 17:13:20 +02:00
|
|
|
set(RHS.Val);
|
2003-10-16 18:53:04 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2008-09-19 17:13:20 +02:00
|
|
|
Value *operator->() { return Val; }
|
|
|
|
const Value *operator->() const { return Val; }
|
2005-01-29 19:43:28 +01:00
|
|
|
|
2008-09-19 17:13:20 +02:00
|
|
|
Use *getNext() const { return Next; }
|
2008-10-15 18:56:14 +02: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 19:34:49 +01:00
|
|
|
/// getPrefix - Return deletable pointer if appropriate
|
|
|
|
Use *getPrefix();
|
2005-01-29 19:43:28 +01:00
|
|
|
private:
|
2008-10-15 18:56:14 +02:00
|
|
|
const Use* getImpliedUser() const;
|
|
|
|
static Use *initTags(Use *Start, Use *Stop, ptrdiff_t Done = 0);
|
|
|
|
|
2008-09-19 17:13:20 +02:00
|
|
|
Value *Val;
|
2009-01-05 17:05:32 +01:00
|
|
|
Use *Next;
|
2009-01-05 17:28:14 +01:00
|
|
|
PointerIntPair<Use**, 2, PrevPtrTag> Prev;
|
2003-10-16 18:53:04 +02:00
|
|
|
|
2008-05-10 10:32:32 +02:00
|
|
|
void setPrev(Use **NewPrev) {
|
2009-01-05 17:05:32 +01:00
|
|
|
Prev.setPointer(NewPrev);
|
2008-05-10 10:32:32 +02:00
|
|
|
}
|
2005-02-01 02:22:06 +01:00
|
|
|
void addToList(Use **List) {
|
|
|
|
Next = *List;
|
2008-09-19 17:13:20 +02:00
|
|
|
if (Next) Next->setPrev(&Next);
|
2008-05-10 10:32:32 +02:00
|
|
|
setPrev(List);
|
2005-02-01 02:22:06 +01:00
|
|
|
*List = this;
|
|
|
|
}
|
|
|
|
void removeFromList() {
|
2009-01-05 17:05:32 +01:00
|
|
|
Use **StrippedPrev = Prev.getPointer();
|
2008-05-10 10:32:32 +02:00
|
|
|
*StrippedPrev = Next;
|
2008-09-19 17:13:20 +02:00
|
|
|
if (Next) Next->setPrev(StrippedPrev);
|
2005-02-01 02:22:06 +01:00
|
|
|
}
|
2003-10-16 18:53:04 +02:00
|
|
|
|
2005-02-01 02:22:06 +01:00
|
|
|
friend class Value;
|
2008-10-15 18:56:14 +02:00
|
|
|
friend class User;
|
2005-02-01 02:22:06 +01:00
|
|
|
};
|
2003-10-16 18:53:04 +02:00
|
|
|
|
2005-02-01 02:22:06 +01:00
|
|
|
// simplify_type - Allow clients to treat uses just like values when using
|
|
|
|
// casting operators.
|
2003-11-14 07:03:05 +01:00
|
|
|
template<> struct simplify_type<Use> {
|
2003-10-16 18:53:04 +02:00
|
|
|
typedef Value* SimpleType;
|
|
|
|
static SimpleType getSimplifiedValue(const Use &Val) {
|
2003-11-16 21:21:15 +01:00
|
|
|
return static_cast<SimpleType>(Val.get());
|
2003-10-16 18:53:04 +02:00
|
|
|
}
|
|
|
|
};
|
2003-11-14 07:03:05 +01:00
|
|
|
template<> struct simplify_type<const Use> {
|
2003-10-16 18:53:04 +02:00
|
|
|
typedef Value* SimpleType;
|
|
|
|
static SimpleType getSimplifiedValue(const Use &Val) {
|
2003-11-16 21:21:15 +01:00
|
|
|
return static_cast<SimpleType>(Val.get());
|
2003-10-16 18:53:04 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-02-01 02:22:06 +01: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 17:13:20 +02:00
|
|
|
explicit value_use_iterator(Use *u) : U(u) {}
|
2005-02-01 02:22:06 +01:00
|
|
|
friend class Value;
|
|
|
|
public:
|
|
|
|
typedef typename super::reference reference;
|
|
|
|
typedef typename super::pointer pointer;
|
2003-10-16 18:53:04 +02:00
|
|
|
|
2005-02-01 02:22:06 +01:00
|
|
|
value_use_iterator(const _Self &I) : U(I.U) {}
|
|
|
|
value_use_iterator() {}
|
2003-10-16 18:53:04 +02:00
|
|
|
|
2005-04-21 22:19:05 +02:00
|
|
|
bool operator==(const _Self &x) const {
|
2005-02-01 02:22:06 +01:00
|
|
|
return U == x.U;
|
2003-10-16 18:53:04 +02:00
|
|
|
}
|
2005-02-01 02:22:06 +01:00
|
|
|
bool operator!=(const _Self &x) const {
|
|
|
|
return !operator==(x);
|
2003-10-16 18:53:04 +02:00
|
|
|
}
|
2008-03-31 19:09:58 +02:00
|
|
|
|
2007-10-11 06:18:11 +02:00
|
|
|
/// atEnd - return true if this iterator is equal to use_end() on the value.
|
2008-09-19 17:13:20 +02:00
|
|
|
bool atEnd() const { return U == 0; }
|
2003-10-16 18:53:04 +02:00
|
|
|
|
2005-02-01 02:22:06 +01:00
|
|
|
// Iterator traversal: forward iteration only
|
|
|
|
_Self &operator++() { // Preincrement
|
2008-09-19 17:13:20 +02:00
|
|
|
assert(U && "Cannot increment end iterator!");
|
2005-02-01 02:22:06 +01:00
|
|
|
U = U->getNext();
|
2005-04-21 22:19:05 +02:00
|
|
|
return *this;
|
2005-02-01 02:22:06 +01:00
|
|
|
}
|
|
|
|
_Self operator++(int) { // Postincrement
|
2005-04-21 22:19:05 +02:00
|
|
|
_Self tmp = *this; ++*this; return tmp;
|
2003-10-16 18:53:04 +02:00
|
|
|
}
|
|
|
|
|
2008-09-19 17:13:20 +02:00
|
|
|
// Retrieve a pointer to the current User.
|
2006-03-28 00:49:07 +02:00
|
|
|
UserTy *operator*() const {
|
2008-09-19 17:13:20 +02:00
|
|
|
assert(U && "Cannot dereference end iterator!");
|
2005-02-01 02:22:06 +01:00
|
|
|
return U->getUser();
|
|
|
|
}
|
2003-10-16 18:53:04 +02:00
|
|
|
|
2005-02-01 02:22:06 +01:00
|
|
|
UserTy *operator->() const { return operator*(); }
|
2003-10-16 18:53:04 +02:00
|
|
|
|
2005-02-01 02:22:06 +01:00
|
|
|
Use &getUse() const { return *U; }
|
2006-05-05 02:51:42 +02:00
|
|
|
|
2006-05-08 07:59:36 +02:00
|
|
|
/// getOperandNo - Return the operand # of this use in its User. Defined in
|
|
|
|
/// User.h
|
2006-05-05 02:51:42 +02:00
|
|
|
///
|
2006-05-08 07:59:36 +02:00
|
|
|
unsigned getOperandNo() const;
|
2003-10-16 18:53:04 +02:00
|
|
|
};
|
|
|
|
|
2006-03-28 00:49:07 +02: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 06:32:37 +02:00
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2003-10-16 18:53:04 +02:00
|
|
|
#endif
|