https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95650
Wilco <wilco at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- Last reconfirmed| |2020-06-12 Ever confirmed|0 |1 CC| |wilco at gcc dot gnu.org Status|UNCONFIRMED |NEW --- Comment #4 from Wilco <wilco at gcc dot gnu.org> --- (In reply to Alex Coplan from comment #3) > I think clang's optimisation is sound here. > > C says that we add two shorts as int and then truncate to short (i.e. reduce > mod 16). > > The question is whether the top bits being set (which the ABI allows) can > influence the result. I don't think it can. > > The observation is that the "top bits being set" are just extra multiples of > 2^16 in the addition, which just disappear when we reduce mod 2^16. That is: > > (x_1 + x_2 + y_1 + y_2) % 2^16 = (x_1 + x_2) % 2^16 > > where x_1,x_2 are arbitrary integers and y_1,y_2 are multiples of 2^16 (the > top bits). Confirmed. It works for signed as well and any operator except right shift and division. Basically the store requires only the bottom 16 bits to be valid, and a backwards dataflow can propagate this to remove unnecessary zero and sign extends.