On Tue, 24 Nov 2015, Jakub Jelinek wrote:
This is the GIMPLE side of Richard's i?86 uadd/usub overflow testing improvements. If unsigned addition or subtraction result is used both normally and in a GIMPLE_COND/COND_EXPR/tcc_comparison that tests if unsigned overflow happened, the patch replaces it shortly before expansion with {ADD,SUB}_OVERFLOW, so that RTL expansion can generate better code on it.
If I test a+b<a and don't use a+b anywhere else, don't we also want to use the OVERFLOW things so we can expand to test the carry flag? That is, I am not convinced we want to punt on has_single_use for add_overflow. For sub_overflow with a single use of y-z, I guess y-z>y should become z>y, and going through a rewrite with sub_overflow neither helps nor hinders that. Actually, writing z>y is something the user is not unlikely to have done himself, and walking through the uses of y or z should not be hard, so I guess it could make sense to rewrite y-z>y to z>y always in match.pd and only look for the second form in math-opts.
I was thinking more match.pd to transform a+b<a and sccvn to somehow CSE a+b with add_overflow(a,b), but your patch seems to work well with simpler code, that's cool :-)
And it shouldn't be too hard to add a few more later, to detect widening operations that are only used for overflow testing, although the form of such tests is much less universal among users.
-- Marc Glisse