On 9/6/26 8:21 PM, Shivam Gupta wrote:
When evaluating integer division of the form (t * u) / v where constants
u and v share a common factor g = gcd(u, v) > 1, the expression can be
simplified to (t * (u / g)) / (v / g).

This patch adds a new match.pd pattern to compute the GCD of INTEGER_CST
operands using wi::gcd on absolute values and reduce both operands
accordingly to reduce signed overflow and generate cheaper
division-by-constant reciprocal sequences.

Regression tested on x86_64-pc-linux-gnu.

        PR tree-optimization/126311

gcc/ChangeLog:

        * match.pd ((t * u) / v): Reduce integer constants u and v by
        their greatest common divisor if gcd(u, v) > 1.

gcc/testsuite/ChangeLog:

        * gcc.dg/tree-ssa/pr126311.c: New test.

Signed-off-by: Shivam Gupta <[email protected]>
So I can see what you're doing and why.  My concern is more about testing.

+
+/* gcd(6, 10) == 2: (t * 6) / 10 -> (t * 3) / 5 */
+int src_pos (int t) {
+  return (t * 6) / 10;
+}
This  case should be reduced AFAICT.
+
+int src_neg (int t) {
+  return (t * -6) / 10;
+}
This one too.

+
+unsigned int src_unsigned (unsigned int t) {
+  return (t * 6u) / 10u;
+}
This one won't reduce as the ranger is going to signal that the multiply may overflow because @0 is has a range [0..UINT_MAX].

+
+/* No common factor: gcd(7, 10) == 1 (should stay / 10) */
+int src_gcd_one (int t) {
+  return (t * 7) / 10;
+}
As the comment, says, no common factor.


So by my count, two reduce, two do not.  Yet we've got these counts in the test:

+
+/* { dg-final { scan-tree-dump-times " / 5" 3 "optimized" } } */
+/* { dg-final { scan-tree-dump-times " / 10" 1 "optimized" } } */
Something seems inconsistent.

jeff


Reply via email to