2008-11-28 23:31:44 +00:00
|
|
|
//===- llvm/ADT/PointerIntPair.h - Pair for pointer and int -----*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the PointerIntPair class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_ADT_POINTERINTPAIR_H
|
|
|
|
#define LLVM_ADT_POINTERINTPAIR_H
|
|
|
|
|
2009-03-29 04:32:37 +00:00
|
|
|
#include "llvm/Support/PointerLikeTypeTraits.h"
|
2008-11-28 23:31:44 +00:00
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
namespace llvm {
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-11-29 01:18:05 +00:00
|
|
|
template<typename T>
|
|
|
|
struct DenseMapInfo;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-11-28 23:31:44 +00:00
|
|
|
/// PointerIntPair - This class implements a pair of a pointer and small
|
2008-11-28 23:57:26 +00:00
|
|
|
/// integer. It is designed to represent this in the space required by one
|
2008-11-28 23:31:44 +00:00
|
|
|
/// pointer by bitmangling the integer into the low part of the pointer. This
|
|
|
|
/// can only be done for small integers: typically up to 3 bits, but it depends
|
2009-03-29 04:32:37 +00:00
|
|
|
/// on the number of bits available according to PointerLikeTypeTraits for the
|
|
|
|
/// type.
|
|
|
|
///
|
|
|
|
/// Note that PointerIntPair always puts the Int part in the highest bits
|
2009-03-29 13:26:05 +00:00
|
|
|
/// possible. For example, PointerIntPair<void*, 1, bool> will put the bit for
|
2009-03-29 04:32:37 +00:00
|
|
|
/// the bool into bit #2, not bit #0, which allows the low two bits to be used
|
|
|
|
/// for something else. For example, this allows:
|
2009-03-29 13:26:05 +00:00
|
|
|
/// PointerIntPair<PointerIntPair<void*, 1, bool>, 1, bool>
|
2009-03-29 04:32:37 +00:00
|
|
|
/// ... and the two bools will land in different bits.
|
2008-11-28 23:31:44 +00:00
|
|
|
///
|
2009-03-29 06:02:20 +00:00
|
|
|
template <typename PointerTy, unsigned IntBits, typename IntType=unsigned,
|
|
|
|
typename PtrTraits = PointerLikeTypeTraits<PointerTy> >
|
2008-11-28 23:31:44 +00:00
|
|
|
class PointerIntPair {
|
|
|
|
intptr_t Value;
|
2009-03-29 04:32:37 +00:00
|
|
|
enum {
|
|
|
|
/// PointerBitMask - The bits that come from the pointer.
|
2009-04-26 19:46:41 +00:00
|
|
|
PointerBitMask =
|
2009-04-26 20:12:38 +00:00
|
|
|
~(uintptr_t)(((intptr_t)1 << PtrTraits::NumLowBitsAvailable)-1),
|
2009-04-26 19:46:41 +00:00
|
|
|
|
2009-03-29 04:32:37 +00:00
|
|
|
/// IntShift - The number of low bits that we reserve for other uses, and
|
|
|
|
/// keep zero.
|
2009-04-26 20:12:38 +00:00
|
|
|
IntShift = (uintptr_t)PtrTraits::NumLowBitsAvailable-IntBits,
|
2009-03-29 04:32:37 +00:00
|
|
|
|
|
|
|
/// IntMask - This is the unshifted mask for valid bits of the int type.
|
2009-04-26 20:12:38 +00:00
|
|
|
IntMask = (uintptr_t)(((intptr_t)1 << IntBits)-1),
|
2009-03-29 04:32:37 +00:00
|
|
|
|
|
|
|
// ShiftedIntMask - This is the bits for the integer shifted in place.
|
2009-04-26 20:12:38 +00:00
|
|
|
ShiftedIntMask = (uintptr_t)(IntMask << IntShift)
|
2009-03-29 04:32:37 +00:00
|
|
|
};
|
2008-11-28 23:31:44 +00:00
|
|
|
public:
|
|
|
|
PointerIntPair() : Value(0) {}
|
|
|
|
PointerIntPair(PointerTy Ptr, IntType Int) : Value(0) {
|
2009-03-29 04:32:37 +00:00
|
|
|
assert(IntBits <= PtrTraits::NumLowBitsAvailable &&
|
|
|
|
"PointerIntPair formed with integer size too large for pointer");
|
2008-11-28 23:31:44 +00:00
|
|
|
setPointer(Ptr);
|
|
|
|
setInt(Int);
|
|
|
|
}
|
|
|
|
|
|
|
|
PointerTy getPointer() const {
|
2009-09-17 20:35:18 +00:00
|
|
|
return PtrTraits::getFromVoidPointer(
|
|
|
|
reinterpret_cast<void*>(Value & PointerBitMask));
|
2008-11-28 23:31:44 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-11-28 23:31:44 +00:00
|
|
|
IntType getInt() const {
|
2009-03-29 04:32:37 +00:00
|
|
|
return (IntType)((Value >> IntShift) & IntMask);
|
2008-11-28 23:31:44 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-11-28 23:31:44 +00:00
|
|
|
void setPointer(PointerTy Ptr) {
|
2009-09-17 20:35:18 +00:00
|
|
|
intptr_t PtrVal
|
|
|
|
= reinterpret_cast<intptr_t>(PtrTraits::getAsVoidPointer(Ptr));
|
2009-03-29 04:32:37 +00:00
|
|
|
assert((PtrVal & ((1 << PtrTraits::NumLowBitsAvailable)-1)) == 0 &&
|
2008-11-29 08:03:35 +00:00
|
|
|
"Pointer is not sufficiently aligned");
|
2009-03-29 04:32:37 +00:00
|
|
|
// Preserve all low bits, just update the pointer.
|
|
|
|
Value = PtrVal | (Value & ~PointerBitMask);
|
2008-11-28 23:31:44 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-11-28 23:31:44 +00:00
|
|
|
void setInt(IntType Int) {
|
2008-12-08 17:41:24 +00:00
|
|
|
intptr_t IntVal = Int;
|
|
|
|
assert(IntVal < (1 << IntBits) && "Integer too large for field");
|
2009-03-29 04:32:37 +00:00
|
|
|
|
|
|
|
// Preserve all bits other than the ones we are updating.
|
|
|
|
Value &= ~ShiftedIntMask; // Remove integer field.
|
|
|
|
Value |= IntVal << IntShift; // Set new integer.
|
2008-11-28 23:31:44 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-11-28 23:31:44 +00:00
|
|
|
void *getOpaqueValue() const { return reinterpret_cast<void*>(Value); }
|
|
|
|
void setFromOpaqueValue(void *Val) { Value = reinterpret_cast<intptr_t>(Val);}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2009-03-29 00:18:42 +00:00
|
|
|
static PointerIntPair getFromOpaqueValue(void *V) {
|
|
|
|
PointerIntPair P; P.setFromOpaqueValue(V); return P;
|
|
|
|
}
|
|
|
|
|
2008-11-30 19:10:41 +00:00
|
|
|
bool operator==(const PointerIntPair &RHS) const {return Value == RHS.Value;}
|
|
|
|
bool operator!=(const PointerIntPair &RHS) const {return Value != RHS.Value;}
|
|
|
|
bool operator<(const PointerIntPair &RHS) const {return Value < RHS.Value;}
|
|
|
|
bool operator>(const PointerIntPair &RHS) const {return Value > RHS.Value;}
|
|
|
|
bool operator<=(const PointerIntPair &RHS) const {return Value <= RHS.Value;}
|
|
|
|
bool operator>=(const PointerIntPair &RHS) const {return Value >= RHS.Value;}
|
2008-11-28 23:31:44 +00:00
|
|
|
};
|
|
|
|
|
2009-12-15 07:26:43 +00:00
|
|
|
template <typename T> struct isPodLike;
|
|
|
|
template<typename PointerTy, unsigned IntBits, typename IntType>
|
|
|
|
struct isPodLike<PointerIntPair<PointerTy, IntBits, IntType> > {
|
|
|
|
static const bool value = true;
|
|
|
|
};
|
|
|
|
|
2008-11-29 01:18:05 +00:00
|
|
|
// Provide specialization of DenseMapInfo for PointerIntPair.
|
|
|
|
template<typename PointerTy, unsigned IntBits, typename IntType>
|
|
|
|
struct DenseMapInfo<PointerIntPair<PointerTy, IntBits, IntType> > {
|
|
|
|
typedef PointerIntPair<PointerTy, IntBits, IntType> Ty;
|
|
|
|
static Ty getEmptyKey() {
|
2009-03-29 06:33:22 +00:00
|
|
|
intptr_t Val = -1;
|
|
|
|
Val <<= PointerLikeTypeTraits<PointerTy>::NumLowBitsAvailable;
|
|
|
|
return Ty(reinterpret_cast<PointerTy>(Val), IntType((1 << IntBits)-1));
|
2008-11-29 01:18:05 +00:00
|
|
|
}
|
|
|
|
static Ty getTombstoneKey() {
|
2009-03-29 06:33:22 +00:00
|
|
|
intptr_t Val = -2;
|
|
|
|
Val <<= PointerLikeTypeTraits<PointerTy>::NumLowBitsAvailable;
|
|
|
|
return Ty(reinterpret_cast<PointerTy>(Val), IntType(0));
|
2008-11-29 01:18:05 +00:00
|
|
|
}
|
|
|
|
static unsigned getHashValue(Ty V) {
|
|
|
|
uintptr_t IV = reinterpret_cast<uintptr_t>(V.getOpaqueValue());
|
|
|
|
return unsigned(IV) ^ unsigned(IV >> 9);
|
|
|
|
}
|
|
|
|
static bool isEqual(const Ty &LHS, const Ty &RHS) { return LHS == RHS; }
|
|
|
|
};
|
|
|
|
|
2009-03-29 00:18:42 +00:00
|
|
|
// Teach SmallPtrSet that PointerIntPair is "basically a pointer".
|
2009-03-30 20:28:50 +00:00
|
|
|
template<typename PointerTy, unsigned IntBits, typename IntType,
|
|
|
|
typename PtrTraits>
|
|
|
|
class PointerLikeTypeTraits<PointerIntPair<PointerTy, IntBits, IntType,
|
|
|
|
PtrTraits> > {
|
2009-03-29 00:18:42 +00:00
|
|
|
public:
|
|
|
|
static inline void *
|
|
|
|
getAsVoidPointer(const PointerIntPair<PointerTy, IntBits, IntType> &P) {
|
|
|
|
return P.getOpaqueValue();
|
|
|
|
}
|
|
|
|
static inline PointerIntPair<PointerTy, IntBits, IntType>
|
|
|
|
getFromVoidPointer(void *P) {
|
|
|
|
return PointerIntPair<PointerTy, IntBits, IntType>::getFromOpaqueValue(P);
|
|
|
|
}
|
2009-03-29 04:32:37 +00:00
|
|
|
enum {
|
2009-09-17 20:35:18 +00:00
|
|
|
NumLowBitsAvailable = PtrTraits::NumLowBitsAvailable - IntBits
|
2009-03-29 04:32:37 +00:00
|
|
|
};
|
2009-03-29 00:18:42 +00:00
|
|
|
};
|
|
|
|
|
2008-11-28 23:31:44 +00:00
|
|
|
} // end namespace llvm
|
|
|
|
#endif
|