LLVM 22.0.0git
PointerLikeTypeTraits.h
Go to the documentation of this file.
1//===- llvm/Support/PointerLikeTypeTraits.h - Pointer Traits ----*- 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// This file defines the PointerLikeTypeTraits class. This allows data
10// structures to reason about pointers and other things that are pointer sized.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_SUPPORT_POINTERLIKETYPETRAITS_H
15#define LLVM_SUPPORT_POINTERLIKETYPETRAITS_H
16
19#include <cassert>
20
21namespace llvm {
22
23/// A traits type that is used to handle pointer types and things that are just
24/// wrappers for pointers as a uniform entity.
25template <typename T> struct PointerLikeTypeTraits;
26
27namespace detail {
28// Provide a trait to check if T is pointer-like.
29template <typename T, typename U = void> struct HasPointerLikeTypeTraits {
30 static const bool value = false;
31};
32
33// sizeof(T) is valid only for a complete T.
34template <typename T>
36 T, decltype((sizeof(PointerLikeTypeTraits<T>) + sizeof(T)), void())> {
37 static const bool value = true;
38};
39
40template <typename T> struct IsPointerLike {
42};
43
44template <typename T> struct IsPointerLike<T *> {
45 static const bool value = true;
46};
47} // namespace detail
48
49// Provide PointerLikeTypeTraits for non-cvr pointers.
50template <typename T> struct PointerLikeTypeTraits<T *> {
51 static inline void *getAsVoidPointer(T *P) { return P; }
52 static inline T *getFromVoidPointer(void *P) { return static_cast<T *>(P); }
53
54 static constexpr int NumLowBitsAvailable = CTLog2<alignof(T)>();
55};
56
57template <> struct PointerLikeTypeTraits<void *> {
58 static inline void *getAsVoidPointer(void *P) { return P; }
59 static inline void *getFromVoidPointer(void *P) { return P; }
60
61 /// Note, we assume here that void* is related to raw malloc'ed memory and
62 /// that malloc returns objects at least 4-byte aligned. However, this may be
63 /// wrong, or pointers may be from something other than malloc. In this case,
64 /// you should specify a real typed pointer or avoid this template.
65 ///
66 /// All clients should use assertions to do a run-time check to ensure that
67 /// this is actually true.
68 static constexpr int NumLowBitsAvailable = 2;
69};
70
71// Provide PointerLikeTypeTraits for const things.
72template <typename T> struct PointerLikeTypeTraits<const T> {
74
75 static inline const void *getAsVoidPointer(const T P) {
76 return NonConst::getAsVoidPointer(P);
77 }
78 static inline const T getFromVoidPointer(const void *P) {
79 return NonConst::getFromVoidPointer(const_cast<void *>(P));
80 }
81 static constexpr int NumLowBitsAvailable = NonConst::NumLowBitsAvailable;
82};
83
84// Provide PointerLikeTypeTraits for const pointers.
85template <typename T> struct PointerLikeTypeTraits<const T *> {
87
88 static inline const void *getAsVoidPointer(const T *P) {
89 return NonConst::getAsVoidPointer(const_cast<T *>(P));
90 }
91 static inline const T *getFromVoidPointer(const void *P) {
92 return NonConst::getFromVoidPointer(const_cast<void *>(P));
93 }
94 static constexpr int NumLowBitsAvailable = NonConst::NumLowBitsAvailable;
95};
96
97// Provide PointerLikeTypeTraits for uintptr_t.
98template <> struct PointerLikeTypeTraits<uintptr_t> {
99 static inline void *getAsVoidPointer(uintptr_t P) {
100 return reinterpret_cast<void *>(P);
101 }
102 static inline uintptr_t getFromVoidPointer(void *P) {
103 return reinterpret_cast<uintptr_t>(P);
104 }
105 // No bits are available!
106 static constexpr int NumLowBitsAvailable = 0;
107};
108
109/// Provide suitable custom traits struct for function pointers.
110///
111/// Function pointers can't be directly given these traits as functions can't
112/// have their alignment computed with `alignof` and we need different casting.
113///
114/// To rely on higher alignment for a specialized use, you can provide a
115/// customized form of this template explicitly with higher alignment, and
116/// potentially use alignment attributes on functions to satisfy that.
117template <int Alignment, typename FunctionPointerT>
119 static constexpr int NumLowBitsAvailable = CTLog2<Alignment>();
120 static inline void *getAsVoidPointer(FunctionPointerT P) {
121 assert((reinterpret_cast<uintptr_t>(P) &
122 ~((uintptr_t)-1 << NumLowBitsAvailable)) == 0 &&
123 "Alignment not satisfied for an actual function pointer!");
124 return reinterpret_cast<void *>(P);
125 }
126 static inline FunctionPointerT getFromVoidPointer(void *P) {
127 return reinterpret_cast<FunctionPointerT>(P);
128 }
129};
130
131/// Provide a default specialization for function pointers that assumes 4-byte
132/// alignment.
133///
134/// We assume here that functions used with this are always at least 4-byte
135/// aligned. This means that, for example, thumb functions won't work or systems
136/// with weird unaligned function pointers won't work. But all practical systems
137/// we support satisfy this requirement.
138template <typename ReturnT, typename... ParamTs>
139struct PointerLikeTypeTraits<ReturnT (*)(ParamTs...)>
140 : FunctionPointerLikeTypeTraits<4, ReturnT (*)(ParamTs...)> {};
141
142} // end namespace llvm
143
144#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
#define T
#define P(N)
This is an optimization pass for GlobalISel generic memory operations.
constexpr size_t CTLog2()
Compile time Log2.
Definition MathExtras.h:326
Provide suitable custom traits struct for function pointers.
static FunctionPointerT getFromVoidPointer(void *P)
static void * getAsVoidPointer(FunctionPointerT P)
static const void * getAsVoidPointer(const T P)
static const T getFromVoidPointer(const void *P)
static const T * getFromVoidPointer(const void *P)
static const void * getAsVoidPointer(const T *P)
static constexpr int NumLowBitsAvailable
Note, we assume here that void* is related to raw malloc'ed memory and that malloc returns objects at...
A traits type that is used to handle pointer types and things that are just wrappers for pointers as ...