Hi!

As reported by Dimitar, this should have been a multiplication, but wasn't
caught because in the test (~(__SIZE_TYPE__) 0) / 2 is the largest accepted
size and so adding 3 to it also resulted in "overflow".

The following patch adds one subtest to really verify it is a multiplication
and fixes the operation.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2025-01-20  Jakub Jelinek  <ja...@redhat.com>

        PR tree-optimization/118224
        * tree-ssa-dce.cc (is_removable_allocation_p): Multiply a1 by a2
        instead of adding it.

        * gcc.dg/pr118224.c: New test.

--- gcc/tree-ssa-dce.cc.jj      2025-01-08 12:13:30.378063752 +0100
+++ gcc/tree-ssa-dce.cc 2025-01-19 22:27:27.047533500 +0100
@@ -331,7 +331,7 @@ is_removable_allocation_p (gcall *stmt,
        return false;
       if (TREE_CODE (a1) == INTEGER_CST
          && TREE_CODE (a2) == INTEGER_CST
-         && (wi::to_widest (a1) + wi::to_widest (a2)
+         && (wi::to_widest (a1) * wi::to_widest (a2)
              > tree_to_uhwi (TYPE_MAX_VALUE (ptrdiff_type_node))))
        return false;
       return true;
--- gcc/testsuite/gcc.dg/pr118224.c.jj  2025-01-06 10:11:53.398877980 +0100
+++ gcc/testsuite/gcc.dg/pr118224.c     2025-01-19 22:10:36.826394427 +0100
@@ -27,5 +27,7 @@ main ()
 #endif
   if (__builtin_calloc ((~(__SIZE_TYPE__) 0) / 2, 3))
     __builtin_abort ();
+  if (__builtin_calloc ((~(__SIZE_TYPE__) 0) / 16, 64))
+    __builtin_abort ();
   foo (1);
 }

        Jakub

Reply via email to