32#ifndef LLVM_ADT_FUNCTIONEXTRAS_H
33#define LLVM_ADT_FUNCTIONEXTRAS_H
63 std::enable_if_t<std::is_trivially_move_constructible<T>::value &&
64 std::is_trivially_destructible<T>::value>;
65template <
typename CallableT,
typename ThisT>
67 std::enable_if_t<!std::is_same<remove_cvref_t<CallableT>, ThisT>
::value>;
68template <
typename CallableT,
typename Ret,
typename... Params>
71 std::is_same<decltype(std::declval<CallableT>()(std::declval<Params>()...)),
73 std::is_same<
const decltype(std::declval<CallableT>()(
74 std::declval<Params>()...)),
76 std::is_convertible<
decltype(std::declval<CallableT>()(
77 std::declval<Params>()...)),
84 template <
typename T,
class =
void>
89 T,
std::enable_if_t<sizeof(T) <= 2 * sizeof(void *)>> : std::true_type {};
100 template <
typename T>
struct AdjustedParamTBase {
101 static_assert(!std::is_reference<T>::value,
102 "references should be handled by template specialization");
104 std::conditional_t<std::is_trivially_copy_constructible<T>::value &&
105 std::is_trivially_move_constructible<T>::value &&
106 IsSizeLessThanThresholdT<T>::value,
113 template <
typename T>
struct AdjustedParamTBase<
T &> {
using type =
T &; };
114 template <
typename T>
struct AdjustedParamTBase<
T &&> {
using type =
T &; };
116 template <
typename T>
117 using AdjustedParamT =
typename AdjustedParamTBase<T>::type;
121 using CallPtrT = ReturnT (*)(
void *CallableAddr,
122 AdjustedParamT<ParamTs>... Params);
123 using MovePtrT = void (*)(
void *LHSCallableAddr,
void *RHSCallableAddr);
124 using DestroyPtrT = void (*)(
void *CallableAddr);
128 struct alignas(8) TrivialCallback {
134 struct alignas(8) NonTrivialCallbacks {
137 DestroyPtrT DestroyPtr;
143 using CallbackPointerUnionT =
144 PointerUnion<TrivialCallback *, NonTrivialCallbacks *>;
148 union StorageUnionT {
151 struct OutOfLineStorageT {
157 sizeof(OutOfLineStorageT) <= InlineStorageSize,
158 "Should always use all of the out-of-line storage for inline storage!");
164 alignas(
void *)
mutable std::byte InlineStorage[InlineStorageSize];
170 PointerIntPair<CallbackPointerUnionT, 1, bool> CallbackAndInlineFlag;
172 bool isInlineStorage()
const {
return CallbackAndInlineFlag.getInt(); }
174 bool isTrivialCallback()
const {
175 return isa<TrivialCallback *>(CallbackAndInlineFlag.getPointer());
178 CallPtrT getTrivialCallback()
const {
179 return cast<TrivialCallback *>(CallbackAndInlineFlag.getPointer())->CallPtr;
183 return cast<NonTrivialCallbacks *>(CallbackAndInlineFlag.getPointer());
187 return isTrivialCallback() ? getTrivialCallback()
202 return StorageUnion.OutOfLineStorage.StoragePtr;
206 return StorageUnion.OutOfLineStorage.Size;
209 return StorageUnion.OutOfLineStorage.Alignment;
213 StorageUnion.OutOfLineStorage = {
Ptr,
Size, Alignment};
216 template <
typename CalledAsT>
218 AdjustedParamT<ParamTs>... Params) {
219 auto &Func = *
reinterpret_cast<CalledAsT *
>(CallableAddr);
220 return Func(std::forward<ParamTs>(Params)...);
223 template <
typename CallableT>
224 static void MoveImpl(
void *LHSCallableAddr,
void *RHSCallableAddr)
noexcept {
225 new (LHSCallableAddr)
226 CallableT(std::move(*
reinterpret_cast<CallableT *
>(RHSCallableAddr)));
229 template <
typename CallableT>
231 reinterpret_cast<CallableT *
>(CallableAddr)->~CallableT();
242 template <
typename CallableT,
typename CalledAs,
typename Enable =
void>
249 template <
typename CallableT,
typename CalledAs>
260 template <
typename CallableT,
typename CalledAsT>
262 bool IsInlineStorage =
true;
265 alignof(CallableT) >
alignof(
decltype(StorageUnion.InlineStorage))) {
266 IsInlineStorage =
false;
269 auto Size =
sizeof(CallableT);
270 auto Alignment =
alignof(CallableT);
276 new (CallableAddr) CallableT(std::move(Callable));
277 CallbackAndInlineFlag.setPointerAndInt(
282 if (!CallbackAndInlineFlag.getPointer())
286 bool IsInlineStorage = isInlineStorage();
288 if (!isTrivialCallback())
292 if (!IsInlineStorage)
299 CallbackAndInlineFlag =
RHS.CallbackAndInlineFlag;
305 if (!isInlineStorage()) {
307 StorageUnion.OutOfLineStorage =
RHS.StorageUnion.OutOfLineStorage;
308 }
else if (isTrivialCallback()) {
314 RHS.getInlineStorage());
318 RHS.CallbackAndInlineFlag = {};
320#if !defined(NDEBUG) && !LLVM_ADDRESS_SANITIZER_BUILD
343 explicit operator bool()
const {
344 return (
bool)CallbackAndInlineFlag.getPointer();
348template <
typename R,
typename...
P>
349template <
typename CallableT,
typename CalledAsT,
typename Enable>
350typename UniqueFunctionBase<R,
P...>::NonTrivialCallbacks UniqueFunctionBase<
351 R,
P...>::CallbacksHolder<CallableT, CalledAsT, Enable>::Callbacks = {
352 &CallImpl<CalledAsT>, &MoveImpl<CallableT>, &DestroyImpl<CallableT>};
354template <
typename R,
typename...
P>
355template <
typename CallableT,
typename CalledAsT>
356typename UniqueFunctionBase<R,
P...>::TrivialCallback
357 UniqueFunctionBase<R,
P...>::CallbacksHolder<
358 CallableT, CalledAsT, EnableIfTrivial<CallableT>>::Callbacks{
359 &CallImpl<CalledAsT>};
363template <
typename R,
typename...
P>
375 template <
typename CallableT>
380 :
Base(
std::forward<CallableT>(Callable),
381 typename
Base::template CalledAs<CallableT>{}) {}
384 return this->getCallPtr()(this->getCalleePtr(), Params...);
388template <
typename R,
typename...
P>
401 template <
typename CallableT>
406 :
Base(
std::forward<CallableT>(Callable),
407 typename
Base::template CalledAs<
const CallableT>{}) {}
410 return this->getCallPtr()(this->getCalleePtr(), Params...);
Given that RA is a live value
This file defines counterparts of C library allocation functions defined in the namespace 'std'.
This file defines the PointerIntPair class.
This file defines the PointerUnion class, which is a discriminated union of pointer types.
This file contains library features backported from future STL versions.
CallPtrT getCallPtr() const
void * getInlineStorage() const
UniqueFunctionBase()=default
void * getOutOfLineStorage() const
void setOutOfLineStorage(void *Ptr, size_t Size, size_t Alignment)
UniqueFunctionBase & operator=(UniqueFunctionBase &&RHS) noexcept
NonTrivialCallbacks * getNonTrivialCallbacks() const
UniqueFunctionBase(CallableT Callable, CalledAs< CalledAsT >)
static void MoveImpl(void *LHSCallableAddr, void *RHSCallableAddr) noexcept
static void DestroyImpl(void *CallableAddr) noexcept
size_t getOutOfLineStorageSize() const
static ReturnT CallImpl(void *CallableAddr, AdjustedParamT< ParamTs >... Params)
void * getCalleePtr() const
size_t getOutOfLineStorageAlignment() const
UniqueFunctionBase(UniqueFunctionBase &&RHS) noexcept
static constexpr size_t InlineStorageSize
unique_function(std::nullptr_t)
unique_function()=default
unique_function(const unique_function &)=delete
unique_function(unique_function &&)=default
unique_function & operator=(const unique_function &)=delete
unique_function & operator=(unique_function &&)=default
R operator()(P... Params) const
unique_function(CallableT Callable, detail::EnableUnlessSameType< CallableT, unique_function > *=nullptr, detail::EnableIfCallable< const CallableT, R, P... > *=nullptr)
unique_function(unique_function &&)=default
unique_function & operator=(const unique_function &)=delete
unique_function(CallableT Callable, detail::EnableUnlessSameType< CallableT, unique_function > *=nullptr, detail::EnableIfCallable< CallableT, R, P... > *=nullptr)
unique_function & operator=(unique_function &&)=default
R operator()(P... Params)
unique_function()=default
unique_function(std::nullptr_t)
unique_function(const unique_function &)=delete
unique_function is a type-erasing functor similar to std::function.
std::enable_if_t< std::disjunction< std::is_void< Ret >, std::is_same< decltype(std::declval< CallableT >()(std::declval< Params >()...)), Ret >, std::is_same< const decltype(std::declval< CallableT >()(std::declval< Params >()...)), Ret >, std::is_convertible< decltype(std::declval< CallableT >()(std::declval< Params >()...)), Ret > >::value > EnableIfCallable
std::enable_if_t< std::is_trivially_move_constructible< T >::value &&std::is_trivially_destructible< T >::value > EnableIfTrivial
std::enable_if_t<!std::is_same< remove_cvref_t< CallableT >, ThisT >::value > EnableUnlessSameType
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * allocate_buffer(size_t Size, size_t Alignment)
Allocate a buffer of memory with the given size and alignment.
void deallocate_buffer(void *Ptr, size_t Size, size_t Alignment)
Deallocate a buffer of memory with the given size and alignment.
Implement std::hash so that hash_code can be used in STL containers.
static TrivialCallback Callbacks
static NonTrivialCallbacks Callbacks