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>()...)),
85 template <
typename T,
class =
void>
90 T,
std::enable_if_t<sizeof(T) <= 2 * sizeof(void *)>> : std::true_type {};
101 template <
typename T>
struct AdjustedParamTBase {
102 static_assert(!std::is_reference<T>::value,
103 "references should be handled by template specialization");
105 std::conditional_t<std::is_trivially_copy_constructible<T>::value &&
106 std::is_trivially_move_constructible<T>::value &&
107 IsSizeLessThanThresholdT<T>::value,
114 template <
typename T>
struct AdjustedParamTBase<
T &> {
using type =
T &; };
115 template <
typename T>
struct AdjustedParamTBase<
T &&> {
using type =
T &; };
117 template <
typename T>
118 using AdjustedParamT =
typename AdjustedParamTBase<T>::type;
122 using CallPtrT = ReturnT (*)(
void *CallableAddr,
123 AdjustedParamT<ParamTs>... Params);
124 using MovePtrT = void (*)(
void *LHSCallableAddr,
void *RHSCallableAddr);
125 using DestroyPtrT = void (*)(
void *CallableAddr);
129 struct alignas(8) TrivialCallback {
135 struct alignas(8) NonTrivialCallbacks {
138 DestroyPtrT DestroyPtr;
144 using CallbackPointerUnionT =
145 PointerUnion<TrivialCallback *, NonTrivialCallbacks *>;
149 union StorageUnionT {
152 struct OutOfLineStorageT {
158 sizeof(OutOfLineStorageT) <= InlineStorageSize,
159 "Should always use all of the out-of-line storage for inline storage!");
165 alignas(InlineStorageAlign)
mutable std::byte
166 InlineStorage[InlineStorageSize];
172 PointerIntPair<CallbackPointerUnionT, 1, bool> CallbackAndInlineFlag;
174 bool isInlineStorage()
const {
return CallbackAndInlineFlag.getInt(); }
176 bool isTrivialCallback()
const {
177 return isa<TrivialCallback *>(CallbackAndInlineFlag.getPointer());
180 CallPtrT getTrivialCallback()
const {
181 return cast<TrivialCallback *>(CallbackAndInlineFlag.getPointer())->CallPtr;
185 return cast<NonTrivialCallbacks *>(CallbackAndInlineFlag.getPointer());
189 return isTrivialCallback() ? getTrivialCallback()
204 return StorageUnion.OutOfLineStorage.StoragePtr;
208 return StorageUnion.OutOfLineStorage.Size;
211 return StorageUnion.OutOfLineStorage.Alignment;
215 StorageUnion.OutOfLineStorage = {
Ptr,
Size, Alignment};
218 template <
typename CalledAsT>
220 AdjustedParamT<ParamTs>... Params) {
221 auto &Func = *
reinterpret_cast<CalledAsT *
>(CallableAddr);
222 return Func(std::forward<ParamTs>(Params)...);
225 template <
typename CallableT>
226 static void MoveImpl(
void *LHSCallableAddr,
void *RHSCallableAddr)
noexcept {
227 new (LHSCallableAddr)
228 CallableT(std::move(*
reinterpret_cast<CallableT *
>(RHSCallableAddr)));
231 template <
typename CallableT>
233 reinterpret_cast<CallableT *
>(CallableAddr)->~CallableT();
244 template <
typename CallableT,
typename CalledAs,
typename Enable =
void>
251 template <
typename CallableT,
typename CalledAs>
262 template <
typename CallableT,
typename CalledAsT>
264 bool IsInlineStorage =
true;
268 IsInlineStorage =
false;
271 auto Size =
sizeof(CallableT);
272 auto Alignment =
alignof(CallableT);
278 new (CallableAddr) CallableT(std::move(Callable));
279 CallbackAndInlineFlag.setPointerAndInt(
284 if (!CallbackAndInlineFlag.getPointer())
288 bool IsInlineStorage = isInlineStorage();
290 if (!isTrivialCallback())
294 if (!IsInlineStorage)
301 CallbackAndInlineFlag =
RHS.CallbackAndInlineFlag;
307 if (!isInlineStorage()) {
309 StorageUnion.OutOfLineStorage =
RHS.StorageUnion.OutOfLineStorage;
310 }
else if (isTrivialCallback()) {
316 RHS.getInlineStorage());
321 RHS.CallbackAndInlineFlag = {};
323#if !defined(NDEBUG) && !LLVM_ADDRESS_SANITIZER_BUILD
346 explicit operator bool()
const {
347 return (
bool)CallbackAndInlineFlag.getPointer();
351template <
typename R,
typename...
P>
352template <
typename CallableT,
typename CalledAsT,
typename Enable>
353typename UniqueFunctionBase<R,
P...>::NonTrivialCallbacks UniqueFunctionBase<
354 R,
P...>::CallbacksHolder<CallableT, CalledAsT, Enable>::Callbacks = {
355 &CallImpl<CalledAsT>, &MoveImpl<CallableT>, &DestroyImpl<CallableT>};
357template <
typename R,
typename...
P>
358template <
typename CallableT,
typename CalledAsT>
359typename UniqueFunctionBase<R,
P...>::TrivialCallback
360 UniqueFunctionBase<R,
P...>::CallbacksHolder<
361 CallableT, CalledAsT, EnableIfTrivial<CallableT>>::Callbacks{
362 &CallImpl<CalledAsT>};
366template <
typename R,
typename...
P>
378 template <
typename CallableT>
383 :
Base(
std::forward<CallableT>(Callable),
384 typename
Base::template CalledAs<CallableT>{}) {}
387 return this->getCallPtr()(this->getCalleePtr(), Params...);
391template <
typename R,
typename...
P>
404 template <
typename CallableT>
409 :
Base(
std::forward<CallableT>(Callable),
410 typename
Base::template CalledAs<
const CallableT>{}) {}
413 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
static constexpr size_t InlineStorageAlign
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