32#ifndef LLVM_ADT_FUNCTIONEXTRAS_H
33#define LLVM_ADT_FUNCTIONEXTRAS_H
62 std::enable_if_t<llvm::is_trivially_move_constructible<T>::value &&
63 std::is_trivially_destructible<T>::value>;
64template <
typename CallableT,
typename ThisT>
66 std::enable_if_t<!std::is_same<remove_cvref_t<CallableT>, ThisT>
::value>;
67template <
typename CallableT,
typename Ret,
typename... Params>
70 std::is_same<decltype(std::declval<CallableT>()(std::declval<Params>()...)),
72 std::is_same<
const decltype(std::declval<CallableT>()(
73 std::declval<Params>()...)),
75 std::is_convertible<
decltype(std::declval<CallableT>()(
76 std::declval<Params>()...)),
83 template <
typename T,
class =
void>
88 T,
std::enable_if_t<sizeof(T) <= 2 * sizeof(void *)>> : std::true_type {};
99 template <
typename T>
struct AdjustedParamTBase {
100 static_assert(!std::is_reference<T>::value,
101 "references should be handled by template specialization");
102 using type = std::conditional_t<
105 IsSizeLessThanThresholdT<T>::value,
112 template <
typename T>
struct AdjustedParamTBase<
T &> {
using type =
T &; };
113 template <
typename T>
struct AdjustedParamTBase<
T &&> {
using type =
T &; };
115 template <
typename T>
116 using AdjustedParamT =
typename AdjustedParamTBase<T>::type;
120 using CallPtrT = ReturnT (*)(
void *CallableAddr,
121 AdjustedParamT<ParamTs>... Params);
122 using MovePtrT = void (*)(
void *LHSCallableAddr,
void *RHSCallableAddr);
123 using DestroyPtrT = void (*)(
void *CallableAddr);
127 struct alignas(8) TrivialCallback {
133 struct alignas(8) NonTrivialCallbacks {
136 DestroyPtrT DestroyPtr;
142 using CallbackPointerUnionT =
143 PointerUnion<TrivialCallback *, NonTrivialCallbacks *>;
147 union StorageUnionT {
150 struct OutOfLineStorageT {
156 sizeof(OutOfLineStorageT) <= InlineStorageSize,
157 "Should always use all of the out-of-line storage for inline storage!");
163 mutable std::aligned_storage_t<InlineStorageSize,
alignof(
void *)>
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 = {};
341 explicit operator bool()
const {
342 return (
bool)CallbackAndInlineFlag.getPointer();
346template <
typename R,
typename...
P>
347template <
typename CallableT,
typename CalledAsT,
typename Enable>
348typename UniqueFunctionBase<R,
P...>::NonTrivialCallbacks UniqueFunctionBase<
349 R,
P...>::CallbacksHolder<CallableT, CalledAsT, Enable>::Callbacks = {
350 &CallImpl<CalledAsT>, &MoveImpl<CallableT>, &DestroyImpl<CallableT>};
352template <
typename R,
typename...
P>
353template <
typename CallableT,
typename CalledAsT>
354typename UniqueFunctionBase<R,
P...>::TrivialCallback
355 UniqueFunctionBase<R,
P...>::CallbacksHolder<
356 CallableT, CalledAsT, EnableIfTrivial<CallableT>>::Callbacks{
357 &CallImpl<CalledAsT>};
361template <
typename R,
typename...
P>
373 template <
typename CallableT>
378 :
Base(
std::forward<CallableT>(Callable),
379 typename
Base::template CalledAs<CallableT>{}) {}
382 return this->getCallPtr()(this->getCalleePtr(), Params...);
386template <
typename R,
typename...
P>
399 template <
typename CallableT>
404 :
Base(
std::forward<CallableT>(Callable),
405 typename
Base::template CalledAs<
const CallableT>{}) {}
408 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< llvm::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.
static TrivialCallback Callbacks
static NonTrivialCallbacks Callbacks
An implementation of std::is_trivially_copy_constructible since we have users with STLs that don't ye...
An implementation of std::is_trivially_move_constructible since we have users with STLs that don't ye...