https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128117
--- Comment #4 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Philipp Tomsich <[email protected]>: https://gcc.gnu.org/g:b5e422deba1e461c1f6fc3ad50d3c5bdf178f2c0 commit r17-5726-gb5e422deba1e461c1f6fc3ad50d3c5bdf178f2c0 Author: Philipp Tomsich <[email protected]> Date: Sat Oct 24 16:03:47 2026 +0200 forwprop: Reject mismatched long-multiply ladders [PR128117] Two of the ladder forms of a long multiplication's high part mask one cross product and add the other unmasked. With hilo and hilo' the cross products xh*yl and xl*yh in either order, N the half width and mask the low-half mask, they are xh*yh + (hilo >> N) + (ladder_sum1 >> N) ladder_sum1 = (hilo & mask) + hilo' + (xl*yl >> N) xh*yh + (ladder_sum2 >> N) + (ladder_part_sum >> N) ladder_part_sum = (xl*yl >> N) + hilo ladder_sum2 = (ladder_part_sum & mask) + hilo' The high half of hilo only reaches the result through (hilo >> N) or ladder_part_sum. Neither table row checked that the cross product in that term is hilo and not hilo', so chains with hilo' there were folded as well. That counts the high half of hilo' twice and drops the high half of hilo. Add a check to both rows that the cross product in (hilo >> N) or ladder_part_sum has the same operands as the one the ladder sum masks. The other rows are not affected. Where both high halves are added on their own, long_mul_check_consistency already requires them to be different products. The remaining rows do not add either one on its own, and the carry compares detect the overflow with either cross product. Bootstrapped and regtested on aarch64-linux-gnu and x86_64-linux-gnu. PR tree-optimization/128117 gcc/ChangeLog: * tree-ssa-forwprop.cc (long_mul_check_ladder_sum1): New. (long_mul_check_ladder_sum2): New. (long_mul_table): Use them for the ladder_sum1 and ladder_sum2 high-part rows. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/long-mul-ladder.c: Add full_mul_ladder_comm, mulh_ladder_square and full_mul_ladder_square, and update the fold counts. * gcc.dg/tree-ssa/pr128117.c: New test. Co-authored-by: Konstantinos Eleftheriou <[email protected]>
