Find patterns in “x in const_set”.
This attempts to compile
x in [1, 3, 5, 7, 9]
as
ord(x) and 1 = 1
(generally, as ord(x) and MASK = VALUE
, if it manages to find suitable MASK
and VALUE
).
Note however that:
-
I have absolutely no idea what’s going on (regarding FPC internals), so you may notice some obvious mistake in using compiler structures and/or have an overwhelming urge to rewrite
tinnode.simplify
andtinsetandshortcut.build
. -
When
x
is an enum with ≤32 values (or maybe even simply whenconst_set
is a small set),in const_set
could be more universally implemented with aBT
against 32-bit constant, and the compiler even seems to know how to do it. So, ultimately, this optimization stops being an optimization and must not be performed for properly implemented small sets. But for now, compiler often shies away for some reason from generating such aBT
and does severalcmp+je
instead, and disabling small sets with something likeand not is_smallset(right.resultdef)
degrades[1, 3, 5, 7, 9]
and many other examples back to the status quo −_−". -
When restricted to large sets, it seems to trigger only here. ^^