On 04/16/14 09:27, Daniel Marjamäki wrote:
Hello!
I am new to GCC.
I want to add a warning to GCC when bit comparison is always true/false.
Example:
if ((x&4)==0) {} // <- no warning
if ((x&4)==4) {} // <- no warning
if ((x&4)==5) {} // <- warn!
When this warning is triggered, the most common cause is that somebody
made a mistake when using bitmasks.
I attach a proof-of-concept patch. I would like comments.
The patch needs some cleanup before it's applied.. I would like it to
handle at least != also and not just ==. And I would like it to be
less strict about where integer constants are located.
I wonder where I should put this code. Is gcc/c/c-typeck.c a good file
to put this in? Should I put it in somewhere else?
What warning flags should be used to enable this? Is some
-Wcondition-bitop a good idea? Can this be added by -Wall?
I wrote this check for Cppcheck years ago. In my experience this
warning has a good signal/noise ratio.
I'd actually do this down at the gimple level. You'll have an SSA graph
you can use to identify the masking and verify its producing a single
bit result. You'll also have canonicalized comparisons, so there'll be
fewer things to test. Depending on exactly where you put the
optimization, you may also see more constants on the RHS due to propagation.
A completely different approach would be to have VRP identify objects
which have values that are only powers of 2. Once such a value is in
the lattice, you can identify and warn/optimize when they're compared
against values which aren't powers of 2.
Jeff
Best regards,
Daniel Marjamäki