LLVM 22.0.0git
PointerEmbeddedInt.h
Go to the documentation of this file.
1//===- llvm/ADT/PointerEmbeddedInt.h ----------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_ADT_POINTEREMBEDDEDINT_H
10#define LLVM_ADT_POINTEREMBEDDEDINT_H
11
15#include <cassert>
16#include <climits>
17#include <cstdint>
18#include <type_traits>
19
20namespace llvm {
21
22/// Utility to embed an integer into a pointer-like type. This is specifically
23/// intended to allow embedding integers where fewer bits are required than
24/// exist in a pointer, and the integer can participate in abstractions along
25/// side other pointer-like types. For example it can be placed into a \c
26/// PointerSumType or \c PointerUnion.
27///
28/// Note that much like pointers, an integer value of zero has special utility
29/// due to boolean conversions. For example, a non-null value can be tested for
30/// in the above abstractions without testing the particular active member.
31/// Also, the default constructed value zero initializes the integer.
32template <typename IntT, int Bits = sizeof(IntT) * CHAR_BIT>
33class PointerEmbeddedInt {
34 uintptr_t Value = 0;
35
36 // Note: This '<' is correct; using '<=' would result in some shifts
37 // overflowing their storage types.
38 static_assert(Bits < sizeof(uintptr_t) * CHAR_BIT,
39 "Cannot embed more bits than we have in a pointer!");
40
41 enum : uintptr_t {
42 // We shift as many zeros into the value as we can while preserving the
43 // number of bits desired for the integer.
44 Shift = sizeof(uintptr_t) * CHAR_BIT - Bits,
45 };
46
47 struct RawValueTag {
48 explicit RawValueTag() = default;
49 };
50
51 friend struct PointerLikeTypeTraits<PointerEmbeddedInt>;
52
53 explicit PointerEmbeddedInt(uintptr_t Value, RawValueTag) : Value(Value) {}
54
55public:
56 PointerEmbeddedInt() = default;
57
58 PointerEmbeddedInt(IntT I) { *this = I; }
59
60 PointerEmbeddedInt &operator=(IntT I) {
61 assert((std::is_signed<IntT>::value ? isInt<Bits>(I) : isUInt<Bits>(I)) &&
62 "Integer has bits outside those preserved!");
63 Value = static_cast<uintptr_t>(I) << Shift;
64 return *this;
65 }
66
67 // Note that this implicit conversion additionally allows all of the basic
68 // comparison operators to work transparently, etc.
69 operator IntT() const {
70 if (std::is_signed<IntT>::value)
71 return static_cast<IntT>(static_cast<intptr_t>(Value) >> Shift);
72 return static_cast<IntT>(Value >> Shift);
73 }
74};
75
76// Provide pointer like traits to support use with pointer unions and sum
77// types.
78template <typename IntT, int Bits>
81
82 static inline void *getAsVoidPointer(const T &P) {
83 return reinterpret_cast<void *>(P.Value);
84 }
85
86 static inline T getFromVoidPointer(void *P) {
87 return T(reinterpret_cast<uintptr_t>(P), typename T::RawValueTag());
88 }
89
90 static inline T getFromVoidPointer(const void *P) {
91 return T(reinterpret_cast<uintptr_t>(P), typename T::RawValueTag());
92 }
93
94 static constexpr int NumLowBitsAvailable = T::Shift;
95};
96
97// Teach DenseMap how to use PointerEmbeddedInt objects as keys if the Int type
98// itself can be a key.
99template <typename IntT, int Bits>
100struct DenseMapInfo<PointerEmbeddedInt<IntT, Bits>> {
103
104 static inline T getEmptyKey() { return IntInfo::getEmptyKey(); }
105 static inline T getTombstoneKey() { return IntInfo::getTombstoneKey(); }
106
107 static unsigned getHashValue(const T &Arg) {
108 return IntInfo::getHashValue(Arg);
109 }
110
111 static bool isEqual(const T &LHS, const T &RHS) { return LHS == RHS; }
112};
113
114} // end namespace llvm
115
116#endif // LLVM_ADT_POINTEREMBEDDEDINT_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines DenseMapInfo traits for DenseMap.
#define I(x, y, z)
Definition MD5.cpp:58
#define T
#define P(N)
Value * RHS
Value * LHS
Utility to embed an integer into a pointer-like type.
PointerEmbeddedInt & operator=(IntT I)
This is an optimization pass for GlobalISel generic memory operations.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:174
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:198
static bool isEqual(const T &LHS, const T &RHS)
An information struct used to provide DenseMap with the various necessary components for a given valu...
A traits type that is used to handle pointer types and things that are just wrappers for pointers as ...