Skip to content

[llvm] add libcall optimizations for C23 stdbit.h routines #79630

@nickdesaulniers

Description

@nickdesaulniers

C23 added a bunch of type generic bitwise functions like:

  • stdc_leading_zeros
  • stdc_leading_ones
  • stdc_trailing_zeros
  • stdc_trailing_ones
  • stdc_first_leading_zero
  • stdc_first_leading_one (smells like ffs but using proper signedness)
  • stdc_first_trailing_zero
  • stdc_first_trailing_one
  • stdc_count_zeros
  • stdc_count_ones
  • stdc_has_single_bit
  • stdc_bit_width
  • stdc_bit_floor
  • stdc_bit_ceil

Refer to section 7.18 of the latest C23 draft (n3096 is what I'm looking at).

All of the above are type generic, so I expect libc implementations to use _Generic to dispatch to the proper variant. There are type specific variants of all of the above with suffixes uc, us, ui, ul, ull for unsigned char, unsigned short, unsigned int, unsigned long, and unsigned long long respectively.

We should recognize calls to all of the above (14 type-generic functions cross 5 specific types == 70 functions) and ensure that calling them with a constant value is recognized (in hosted mode) and folded properly, thereby eliminating the library dispatch.

llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp should recognize these calls in IR and ensure we can elide the libcalls.

cc @efriedma-quic @davidbolvansky

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions