widening_mul: Fix up signed multiplication overflow check handling [PR98727]
authorJakub Jelinek <jakub@redhat.com>
Mon, 18 Jan 2021 18:13:44 +0000 (19:13 +0100)
committerJakub Jelinek <jakub@redhat.com>
Mon, 18 Jan 2021 18:13:44 +0000 (19:13 +0100)
I forgot one line, which means that if the second operand of the multiplication
isn't constant, it would be just the same as the first one.

2021-01-18  Jakub Jelinek  <jakub@redhat.com>

PR tree-optimization/98727
* tree-ssa-math-opts.c (match_arith_overflow): Fix up computation of
second .MUL_OVERFLOW operand for signed multiplication with overflow
checking if the second operand of multiplication is not constant.

* gcc.c-torture/execute/pr98727.c: New test.

gcc/testsuite/gcc.c-torture/execute/pr98727.c [new file with mode: 0644]
gcc/tree-ssa-math-opts.c

diff --git a/gcc/testsuite/gcc.c-torture/execute/pr98727.c b/gcc/testsuite/gcc.c-torture/execute/pr98727.c
new file mode 100644 (file)
index 0000000..7ecdef8
--- /dev/null
@@ -0,0 +1,20 @@
+/* PR tree-optimization/98727 */
+
+__attribute__((noipa)) long int
+foo (long int x, long int y)
+{
+  long int z = (unsigned long) x * y;
+  if (x != z / y)
+    return -1;
+  return z;
+}
+
+int
+main ()
+{
+  if (foo (4, 24) != 96
+      || foo (124, 126) != 124L * 126
+      || foo (__LONG_MAX__ / 16, 17) != -1)
+    __builtin_abort ();
+  return 0;
+}
index 5175ab524dc90fd20e94f05bbb2422da1e397a0b..d6201d3cb943e145720c18fbf3aadd853fd87b44 100644 (file)
@@ -4170,6 +4170,7 @@ match_arith_overflow (gimple_stmt_iterator *gsi, gimple *stmt,
        rhs2 = fold_convert (type, rhs2);
       else
        {
+         g = SSA_NAME_DEF_STMT (rhs2);
          if (gimple_assign_cast_p (g)
              && useless_type_conversion_p (type,
                                            TREE_TYPE (gimple_assign_rhs1 (g)))