Skip to content

Find patterns in “x in const_set”.

Rika requested to merge runewalsh/source:setmask into main

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 and tinsetandshortcut.build.

  • When x is an enum with ≤32 values (or maybe even simply when const_set is a small set), in const_set could be more universally implemented with a BT 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 a BT and does several cmp+je instead, and disabling small sets with something like and 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. ^^

Edited by Rika

Merge request reports