[llvm-dev] Missed optimization of bitwise expressions

Jay Foad via llvm-dev llvm-dev at lists.llvm.org
Thu Nov 11 03:34:04 PST 2021


Hi,

I tried searching for small bitwise expressions using AND OR XOR and
NOT that "opt -O3" fails to optimize to a simpler form. For example:

(A^B)|~A --> ~(A&B)
https://alive2.llvm.org/ce/z/HpWfzp

A|B|(A^B) --> A|B
https://alive2.llvm.org/ce/z/UCC6uM

((A|B)^C)&A --> A&~C (actually I don't understand why this one is OK,
even if B might be poison, but alive2 says it is OK)
https://alive2.llvm.org/ce/z/mkvW6p

I can file bugs for specific examples but I wondered if there was any
interest in a more systematic approach to finding these missed
optimizations? My approach was to write some very hacky Python
(https://gist.github.com/jayfoad/94f4c68fa3a9aa908db79dbd7e9df80d,
please don't read it) to exhaustively generate programs; then take all
the programs with a given truth table, run them all through "opt -O3",
and check that they all got optimized to the same (or at least equally
short) output.

Thanks,
Jay.


More information about the llvm-dev mailing list