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
|
|
|
//===----------------------------------------------------------------------===//
|
2014-03-04 08:51:00 +00:00
|
|
|
/// \file
|
|
|
|
///
|
|
|
|
/// 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.
|
|
|
|
///
|
|
|
|
/// 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 16:53:04 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-10 00:45:19 +00:00
|
|
|
#ifndef LLVM_IR_USE_H
|
|
|
|
#define LLVM_IR_USE_H
|
2003-10-16 16:53:04 +00:00
|
|
|
|
2014-01-07 11:48:04 +00:00
|
|
|
#include "llvm-c/Core.h"
|
2009-01-05 16:05:32 +00:00
|
|
|
#include "llvm/ADT/PointerIntPair.h"
|
2013-05-01 20:59:00 +00:00
|
|
|
#include "llvm/Support/CBindingWrapping.h"
|
2012-09-16 21:50:09 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2011-01-28 03:52:25 +00:00
|
|
|
#include <cstddef>
|
2009-08-27 06:41:46 +00:00
|
|
|
#include <iterator>
|
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;
|
2014-03-04 08:53:41 +00:00
|
|
|
template <typename> struct simplify_type;
|
2003-10-16 16:53:04 +00:00
|
|
|
|
2009-04-03 00:26:01 +00:00
|
|
|
// Use** is only 4-byte aligned.
|
2014-03-04 08:53:41 +00:00
|
|
|
template <> class PointerLikeTypeTraits<Use **> {
|
2009-04-03 00:26:01 +00:00
|
|
|
public:
|
2014-03-04 08:53:41 +00:00
|
|
|
static inline void *getAsVoidPointer(Use **P) { return P; }
|
2009-04-03 00:26:01 +00:00
|
|
|
static inline Use **getFromVoidPointer(void *P) {
|
2014-03-04 08:53:41 +00:00
|
|
|
return static_cast<Use **>(P);
|
2009-04-03 00:26:01 +00:00
|
|
|
}
|
|
|
|
enum { NumLowBitsAvailable = 2 };
|
|
|
|
};
|
|
|
|
|
2014-03-04 08:51:00 +00:00
|
|
|
/// \brief A Use represents the edge between a Value definition and its users.
|
|
|
|
///
|
|
|
|
/// This is notionally a two-dimensional linked list. It supports traversing
|
|
|
|
/// all of the uses for a particular value definition. It also supports jumping
|
|
|
|
/// directly to the used value when we arrive from the User's operands, and
|
|
|
|
/// jumping directly to the User when we arrive from the Value's uses.
|
|
|
|
///
|
|
|
|
/// The pointer to the used Value is explicit, and the pointer to the User is
|
|
|
|
/// implicit. The implicit pointer is found via a waymarking algorithm
|
|
|
|
/// described in the programmer's manual:
|
|
|
|
///
|
2014-04-29 13:21:05 +00:00
|
|
|
/// http://www.llvm.org/docs/ProgrammersManual.html#the-waymarking-algorithm
|
2014-03-04 08:51:00 +00:00
|
|
|
///
|
|
|
|
/// This is essentially the single most memory intensive object in LLVM because
|
|
|
|
/// of the number of uses in the system. At the same time, the constant time
|
|
|
|
/// operations it allows are essential to many optimizations having reasonable
|
|
|
|
/// time complexity.
|
2003-10-16 16:53:04 +00:00
|
|
|
class Use {
|
2008-09-19 15:13:20 +00:00
|
|
|
public:
|
2014-03-04 08:51:00 +00:00
|
|
|
/// \brief Provide a fast substitute to std::swap<Use>
|
2008-05-13 22:51:52 +00:00
|
|
|
/// that also works with less standard-compliant compilers
|
|
|
|
void swap(Use &RHS);
|
2005-01-29 00:30:52 +00:00
|
|
|
|
2011-06-20 14:12:33 +00:00
|
|
|
// A type for the word following an array of hung-off Uses in memory, which is
|
|
|
|
// a pointer back to their User with the bottom bit set.
|
2014-03-04 08:53:41 +00:00
|
|
|
typedef PointerIntPair<User *, 1, unsigned> UserRef;
|
2011-06-20 14:12:33 +00:00
|
|
|
|
2008-05-10 08:32:32 +00:00
|
|
|
private:
|
2012-09-16 21:37:56 +00:00
|
|
|
Use(const Use &U) LLVM_DELETED_FUNCTION;
|
2008-05-10 08:32:32 +00:00
|
|
|
|
2008-05-13 22:51:52 +00:00
|
|
|
/// Destructor - Only for zap()
|
2011-01-16 15:30:52 +00:00
|
|
|
~Use() {
|
2014-03-04 08:53:41 +00:00
|
|
|
if (Val)
|
|
|
|
removeFromList();
|
2008-03-14 22:03:02 +00:00
|
|
|
}
|
2003-10-16 16:53:04 +00:00
|
|
|
|
2014-03-04 08:53:41 +00:00
|
|
|
enum PrevPtrTag { zeroDigitTag, oneDigitTag, stopTag, fullStopTag };
|
2008-09-19 15:13:20 +00:00
|
|
|
|
2011-01-16 15:30:52 +00:00
|
|
|
/// Constructor
|
2014-04-09 06:08:46 +00:00
|
|
|
Use(PrevPtrTag tag) : Val(nullptr) { Prev.setInt(tag); }
|
2011-01-16 15:30:52 +00:00
|
|
|
|
2008-05-10 08:32:32 +00:00
|
|
|
public:
|
2014-03-04 08:53:41 +00:00
|
|
|
operator Value *() const { return Val; }
|
2008-09-19 15:13:20 +00:00
|
|
|
Value *get() const { return Val; }
|
2014-03-04 08:51:00 +00:00
|
|
|
|
|
|
|
/// \brief 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;
|
|
|
|
}
|
|
|
|
|
2014-03-04 08:53:41 +00:00
|
|
|
Value *operator->() { return Val; }
|
2008-09-19 15:13:20 +00:00
|
|
|
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
|
|
|
|
2014-03-04 09:19:43 +00:00
|
|
|
/// \brief Return the operand # of this use in its User.
|
|
|
|
unsigned getOperandNo() const;
|
|
|
|
|
2014-03-04 08:51:00 +00:00
|
|
|
/// \brief Initializes the waymarking tags on an array of Uses.
|
|
|
|
///
|
|
|
|
/// This sets up the array of Uses such that getUser() can find the User from
|
|
|
|
/// any of those Uses.
|
2011-06-23 09:09:15 +00:00
|
|
|
static Use *initTags(Use *Start, Use *Stop);
|
|
|
|
|
2014-03-04 08:51:00 +00:00
|
|
|
/// \brief Destroys Use operands when the number of operands of
|
2008-10-15 16:56:14 +00:00
|
|
|
/// a User changes.
|
|
|
|
static void zap(Use *Start, const Use *Stop, bool del = false);
|
|
|
|
|
2005-01-29 18:43:28 +00:00
|
|
|
private:
|
2014-03-04 08:53:41 +00:00
|
|
|
const Use *getImpliedUser() const;
|
2014-03-04 08:51:00 +00:00
|
|
|
|
2008-09-19 15:13:20 +00:00
|
|
|
Value *Val;
|
2009-01-05 16:05:32 +00:00
|
|
|
Use *Next;
|
2014-03-04 08:53:41 +00:00
|
|
|
PointerIntPair<Use **, 2, PrevPtrTag> Prev;
|
2003-10-16 16:53:04 +00:00
|
|
|
|
2014-03-04 08:53:41 +00:00
|
|
|
void setPrev(Use **NewPrev) { Prev.setPointer(NewPrev); }
|
2005-02-01 01:22:06 +00:00
|
|
|
void addToList(Use **List) {
|
|
|
|
Next = *List;
|
2014-03-04 08:53:41 +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;
|
2014-03-04 08:53:41 +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;
|
|
|
|
};
|
2003-10-16 16:53:04 +00:00
|
|
|
|
2014-03-04 08:51:00 +00:00
|
|
|
/// \brief Allow clients to treat uses just like values when using
|
|
|
|
/// casting operators.
|
2014-03-04 08:53:41 +00:00
|
|
|
template <> struct simplify_type<Use> {
|
|
|
|
typedef Value *SimpleType;
|
|
|
|
static SimpleType getSimplifiedValue(Use &Val) { return Val.get(); }
|
2003-10-16 16:53:04 +00:00
|
|
|
};
|
2014-03-04 08:53:41 +00:00
|
|
|
template <> struct simplify_type<const Use> {
|
|
|
|
typedef /*const*/ Value *SimpleType;
|
|
|
|
static SimpleType getSimplifiedValue(const Use &Val) { return Val.get(); }
|
2003-10-16 16:53:04 +00:00
|
|
|
};
|
|
|
|
|
2013-05-01 20:59:00 +00:00
|
|
|
// Create wrappers for C Binding types (see CBindingWrapping.h).
|
|
|
|
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Use, LLVMUseRef)
|
|
|
|
|
2014-03-04 08:51:00 +00:00
|
|
|
}
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2003-10-16 16:53:04 +00:00
|
|
|
#endif
|