Thanks! > 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.
Ok, I can move it to the gimple level. However I don't know much about GCC internals so I'd like to know what file/function I should look at. > 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. I don't want to limit this to numbers that are only powers of 2. I want to catch this also: if ((relocation & 0xfffc0003) != 0xffff0000) Actual code: https://lkml.org/lkml/2011/11/17/344 I determine if a condition ((x & 0xc) != 0xf) is always true by checking if this is true: ((0xc & 0xf) != 0xf) I wonder if I can check if a tree is a macro or template parameter somehow. I don't know if there are any false positives with macros/templates but it might be a good idea to skip warnings if the expression is a expanded macro or if any value is a template parameter. Best regards, Daniel Marjamäki 2014-04-16 23:17 GMT+02:00 Jeff Law <l...@redhat.com>: > 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 >> >