AK: Simplify as_if<T> when fast_is<T> is unavailable

If `fast_is<T>` was not available but `static_cast<T>` was, we would
call into `is<T>` causing a `dynamic_cast` there, throwing away the
result and return a `static_cast`ed pointer.

We can encourage better codegen by checking for the presence of
`fast_is<T>` ourselves. This does not change anything for when
`fast_is<T>` is present, but if it's unavailable we now reduce
`as_if<T>` to a simple tailcall of `dynamic_cast<T>`.

Old:

```
stp  x20, x19, [sp, #-32]!
stp  x29, x30, [sp, #16]
add  x29, sp, #16
mov  x19, x0
adrp x1, typeinfo for Base@PAGE
add  x1, x1, typeinfo for Base@PAGEOFF
adrp x2, typeinfo for WithoutFastIs@PAGE
add  x2, x2, typeinfo for WithoutFastIs@PAGEOFF
mov  x3, #0
bl   ___dynamic_cast
cmp  x0, #0
csel x0, xzr, x19, eq
ldp  x29, x30, [sp, #16]
ldp  x20, x19, [sp], #32
ret
```

New:

```
adrp x1, typeinfo for Base@PAGE
add  x1, x1, typeinfo for Base@PAGEOFF
adrp x2, typeinfo for WithoutFastIs@PAGE
add  x2, x2, typeinfo for WithoutFastIs@PAGEOFF
mov  x3, #0
b    ___dynamic_cast
```
This commit is contained in:
Jelle Raaijmakers
2026-02-27 11:09:19 +01:00
committed by Tim Flynn
parent 81cb968beb
commit 7eafdcd454
Notes: github-actions[bot] 2026-02-27 16:43:59 +00:00

View File

@@ -38,7 +38,7 @@ ALWAYS_INLINE bool is(NonnullRefPtr<InputType> const& input)
template<typename OutputType, typename InputType>
ALWAYS_INLINE CopyConst<InputType, OutputType>* as_if(InputType& input)
{
if constexpr (requires { static_cast<CopyConst<InputType, OutputType>*>(&input); }) {
if constexpr (requires { input.template fast_is<RemoveCVReference<OutputType>>(); static_cast<CopyConst<InputType, OutputType>*>(&input); }) {
if (!is<OutputType>(input))
return nullptr;
return static_cast<CopyConst<InputType, OutputType>*>(&input);