forwprop: Correctly handle long multiplication ladders with mixed operands
Fixes an error in long multiplication optimizations by ensuring consistent operands are used in ladder sums, preventing incorrect results.
This commit addresses an issue in the forward propagation pass related to the optimization of long multiplication operations. Specifically, it corrects two ladder forms where the high part was masked and added, but mismatched cross-product operands were allowed. The fix ensures that the cross-product operand used in the added term is consistent with the one being masked. This prevents double-counting or dropping parts of the high-half of intermediate products, thus guaranteeing correct results for these optimizations.
In Details
The long_mul_check_ladder_sum1 and long_mul_check_ladder_sum2 functions in tree-ssa-forwprop.cc were updated to reject mismatched operands in long multiplication ladders. Two specific ladder forms incorrectly allowed different cross-products (hilo vs. hilo') to be used in the masked sum and the added term. This commit adds checks to ensure that if (hilo >> N) or ladder_part_sum uses a cross-product, it must be the same as the one masked by ladder_sum1/ladder_sum2. This resolves a bug where the high half of hilo could be dropped or the high half of hilo' counted twice.
- forwprop
- Forward Propagation, a compiler optimization pass that replaces variables with their constant values or expressions where they are used.
- long multiplication
- An arithmetic operation that multiplies two large integers, typically requiring more than the standard word size of the processor. The result is a number that can be twice the size of the operands.
- ladder sum
- In the context of long multiplication optimization, a series of additions and multiplications designed to compute the high-order bits of the product.
- tree-ssa
- GCC's intermediate representation and optimization framework that uses Static Single Assignment (SSA) form.