1 //===- llvm/ADT/PointerEmbeddedInt.h ----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_ADT_POINTEREMBEDDEDINT_H
11 #define LLVM_ADT_POINTEREMBEDDEDINT_H
13 #include "llvm/ADT/DenseMapInfo.h"
14 #include "llvm/Support/PointerLikeTypeTraits.h"
19 /// Utility to embed an integer into a pointer-like type. This is specifically
20 /// intended to allow embedding integers where fewer bits are required than
21 /// exist in a pointer, and the integer can participate in abstractions along
22 /// side other pointer-like types. For example it can be placed into a \c
23 /// PointerSumType or \c PointerUnion.
25 /// Note that much like pointers, an integer value of zero has special utility
26 /// due to boolean conversions. For example, a non-null value can be tested for
27 /// in the above abstractions without testing the particular active member.
28 /// Also, the default constructed value zero initializes the integer.
29 template <typename IntT, int Bits = sizeof(IntT) * CHAR_BIT>
30 class PointerEmbeddedInt {
33 static_assert(Bits < sizeof(uintptr_t) * CHAR_BIT,
34 "Cannot embed more bits than we have in a pointer!");
37 // We shift as many zeros into the value as we can while preserving the
38 // number of bits desired for the integer.
39 Shift = sizeof(uintptr_t) * CHAR_BIT - Bits,
41 // We also want to be able to mask out the preserved bits for asserts.
42 Mask = static_cast<uintptr_t>(-1) << Bits
45 friend class PointerLikeTypeTraits<PointerEmbeddedInt>;
47 explicit PointerEmbeddedInt(uintptr_t Value) : Value(Value) {}
50 PointerEmbeddedInt() : Value(0) {}
52 PointerEmbeddedInt(IntT I) : Value(static_cast<uintptr_t>(I) << Shift) {
53 assert((I & Mask) == 0 && "Integer has bits outside those preserved!");
56 PointerEmbeddedInt &operator=(IntT I) {
57 assert((I & Mask) == 0 && "Integer has bits outside those preserved!");
58 Value = static_cast<uintptr_t>(I) << Shift;
61 // Note that this imilict conversion additionally allows all of the basic
62 // comparison operators to work transparently, etc.
63 operator IntT() const { return static_cast<IntT>(Value >> Shift); }
66 // Provide pointer like traits to support use with pointer unions and sum
68 template <typename IntT, int Bits>
69 class PointerLikeTypeTraits<PointerEmbeddedInt<IntT, Bits>> {
70 typedef PointerEmbeddedInt<IntT, Bits> T;
73 static inline void *getAsVoidPointer(const T &P) {
74 return reinterpret_cast<void *>(P.Value);
76 static inline T getFromVoidPointer(void *P) {
77 return T(reinterpret_cast<uintptr_t>(P));
79 static inline T getFromVoidPointer(const void *P) {
80 return T(reinterpret_cast<uintptr_t>(P));
83 enum { NumLowBitsAvailable = T::Shift };
86 // Teach DenseMap how to use PointerEmbeddedInt objects as keys if the Int type
87 // itself can be a key.
88 template <typename IntT, int Bits>
89 struct DenseMapInfo<PointerEmbeddedInt<IntT, Bits>> {
90 typedef PointerEmbeddedInt<IntT, Bits> T;
92 typedef DenseMapInfo<IntT> IntInfo;
94 static inline T getEmptyKey() { return IntInfo::getEmptyKey(); }
95 static inline T getTombstoneKey() { return IntInfo::getTombstoneKey(); }
96 static unsigned getHashValue(const T &Arg) {
97 return IntInfo::getHashValue(Arg);
99 static bool isEqual(const T &LHS, const T &RHS) { return LHS == RHS; }