Message ID | 20210126220432.58265-1-luc.vanoostenryck@gmail.com (mailing list archive) |
---|---|
Headers | show |
Series | simplify and canonicalize signed compares | expand |
On Tue, Jan 26, 2021 at 7:45 PM Luc Van Oostenryck <luc.vanoostenryck@gmail.com> wrote: > > This series fixes and improves the simplification and the > canonicalization of signed compares. Hmm. Sorry for not replying earlier, but I just checked the most common simplification of signed compares, and it didn't work. This: _Bool test(int a) { return a >=0 && a < 16; } should simplify to be the same as _Bool test(int a) { return (unsigned)a < 16; } but it doesn't. It generates the silly - but straightforward - "two comparisons and a 'and' of the result". In fact, the recent canonicalizations means that the compare against zero is actually pessimised, and ">= 0" becomes "> 0xffffffff", which is often a much more expensive operation. This came up because I was looking at some kernel code that did exactly that "check that a signed value is within proper bounds", and the zero check is a very common bound. Linus
On Sat, Apr 17, 2021 at 10:16:31AM -0700, Linus Torvalds wrote: > On Tue, Jan 26, 2021 at 7:45 PM Luc Van Oostenryck > <luc.vanoostenryck@gmail.com> wrote: > > > > This series fixes and improves the simplification and the > > canonicalization of signed compares. > > Hmm. Sorry for not replying earlier, but I just checked the most > common simplification of signed compares, and it didn't work. > > This: > > _Bool test(int a) > { > return a >=0 && a < 16; > } > > should simplify to be the same as > > _Bool test(int a) > { > return (unsigned)a < 16; > } > > but it doesn't. It generates the silly - but straightforward - "two > comparisons and a 'and' of the result". Yes, I've a draft for this but I still needs to add some tests and such. > In fact, the recent canonicalizations means that the compare against > zero is actually pessimised, and ">= 0" becomes "> 0xffffffff", which > is often a much more expensive operation. Yes, I'm aware of the problem, more or less. When I did the canonicalization, I wondered what was better: * canonicalize toward 0 * canonicalize toward the smallest I choose the later because at the moment it was somehow advantageous because it reduced by 2 some patterns (it allows to eliminate all >= and all <; when doing it toward 0 you can eliminate one set for positive and the other one for negatives values so you need both sets). The compare with -1 instead of with 0 didn't looked much problematic and relatively rare. But in the following days I realized it was in fact quite annoying. So, yes, it's something I'll change very soon. -- Luc