[PATCH] D122079: [ADT] Add Enum matcher

Chris Bieneman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 22 16:34:56 PDT 2022


beanz added a comment.
Herald added a subscriber: StephenFan.

In D122079#3396156 <https://reviews.llvm.org/D122079#3396156>, @kuhar wrote:

> A few thoughts:
>
> 1. Is the requirement for this to be constant folded by the optimizer?

It isn't strictly a requirement. If the code is called with a non-constant value the optimizer will convert it to nice range checks.

> 2. This could be made constexpr if we wanted.

I had another place that I wanted to use this in clang where the input parameter isn't constant.

> 4. The function name suggests this handles enums only but it works with any copybale & comparable type. I think we should either restrict it to enum types or make the name more general.

That is totally reasonable. It's really a compile-time set matcher :).

> 5. Since this function is not constexpr now, how about we added an overload to `is_contained` accepting an initializer list as the second element, e.g.: `is_constained(MyEnum::A, {MyEnum::A, MyEnum::B, MyEnum::C})`?

I realize this is probably a bit overkill, but `isInEnumSet<Numbers, One, Two, Three, Four, Five>(Val)` will actually optimize down to roughly `if( Val >= One && Val <= Five)` (assuming sequential values), and that really only works reliably with the template parameter unrolling.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122079/new/

https://reviews.llvm.org/D122079



More information about the llvm-commits mailing list