The following makes us properly rewrite ABS_EXPR to avoid
undefined overflow when hoisting it from a not always executed
region.

Bootstrap & regtest on x86_64-unknown-linux-gnu in progress.

Richard.

2019-05-21  Richard Biener  <rguent...@suse.de>

        * gimple-fold.c (arith_code_with_undefined_signed_overflow):
        Add ABS_EXPR.
        (rewrite_to_defined_overflow): Handle rewriting ABS_EXPR
        as ABSU_EXPR.

        * gcc.dg/tree-ssa/ssa-lim-13.c: New testcase.

Index: gcc/gimple-fold.c
===================================================================
--- gcc/gimple-fold.c   (revision 271463)
+++ gcc/gimple-fold.c   (working copy)
@@ -7329,6 +7329,7 @@ arith_code_with_undefined_signed_overflo
 {
   switch (code)
     {
+    case ABS_EXPR:
     case PLUS_EXPR:
     case MINUS_EXPR:
     case MULT_EXPR:
@@ -7361,12 +7362,15 @@ rewrite_to_defined_overflow (gimple *stm
   tree lhs = gimple_assign_lhs (stmt);
   tree type = unsigned_type_for (TREE_TYPE (lhs));
   gimple_seq stmts = NULL;
-  for (unsigned i = 1; i < gimple_num_ops (stmt); ++i)
-    {
-      tree op = gimple_op (stmt, i);
-      op = gimple_convert (&stmts, type, op);
-      gimple_set_op (stmt, i, op);
-    }
+  if (gimple_assign_rhs_code (stmt) == ABS_EXPR)
+    gimple_assign_set_rhs_code (stmt, ABSU_EXPR);
+  else
+    for (unsigned i = 1; i < gimple_num_ops (stmt); ++i)
+      {
+       tree op = gimple_op (stmt, i);
+       op = gimple_convert (&stmts, type, op);
+       gimple_set_op (stmt, i, op);
+      }
   gimple_assign_set_lhs (stmt, make_ssa_name (type, stmt));
   if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
     gimple_assign_set_rhs_code (stmt, PLUS_EXPR);
Index: gcc/testsuite/gcc.dg/tree-ssa/ssa-lim-13.c
===================================================================
--- gcc/testsuite/gcc.dg/tree-ssa/ssa-lim-13.c  (nonexistent)
+++ gcc/testsuite/gcc.dg/tree-ssa/ssa-lim-13.c  (working copy)
@@ -0,0 +1,53 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fgimple -fdump-tree-lim2-details" } */
+
+int __GIMPLE (ssa,startwith("lim"))
+foo (int x, int n)
+{
+  int i;
+  int r;
+  int _1;
+  int _2;
+  int _6;
+
+  __BB(2):
+  goto __BB7;
+
+  __BB(3):
+  if (i_5 == 17)
+    goto __BB8;
+  else
+    goto __BB4;
+
+  __BB(4):
+  _1 = i_5 & 1;
+  if (_1 != 0)
+    goto __BB5;
+  else
+    goto __BB6;
+
+  __BB(5):
+  _2 = __ABS x_8(D);
+  r_9 = _2 / 5;
+  goto __BB6;
+
+  __BB(6):
+  r_3 = __PHI (__BB5: r_9, __BB4: r_4);
+  i_10 = i_5 + 1;
+  goto __BB7;
+
+  __BB(7,loop_header(1)):
+  r_4 = __PHI (__BB2: 1, __BB6: r_3);
+  i_5 = __PHI (__BB2: 0, __BB6: i_10);
+  if (i_5 < n_7(D))
+    goto __BB3;
+  else
+    goto __BB8;
+
+  __BB(8):
+  _6 = __PHI (__BB3: 0, __BB7: r_4);
+  return _6;
+}
+
+/* { dg-final { scan-tree-dump-times "Moving statement" 2 "lim2" } } */
+/* { dg-final { scan-tree-dump "ABSU_EXPR" "lim2" } } */

Reply via email to