Like the patch to phiopt (r15-4033-g1f619fe25925a5f7), this adds rewriting
of VCE to gimple_with_undefined_signed_overflow/rewrite_to_defined_overflow.
I have not seen a case yet for needing this rewrite but this step is needed
to use gimple_with_undefined_signed_overflow/rewrite_to_defined_overflow from
phiopt.

Bootstrappd and tested on x86_64-linux-gnu.

gcc/ChangeLog:

        PR tree-optimization/116939
        * gimple-fold.cc (gimple_with_undefined_signed_overflow): Return true
        for VCE with integral types.
        (rewrite_to_defined_overflow): Handle VCE rewriting to a cast.

Signed-off-by: Andrew Pinski <quic_apin...@quicinc.com>
---
 gcc/gimple-fold.cc | 30 ++++++++++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)

diff --git a/gcc/gimple-fold.cc b/gcc/gimple-fold.cc
index c060ef81a42..a6a7fcbb8c1 100644
--- a/gcc/gimple-fold.cc
+++ b/gcc/gimple-fold.cc
@@ -10602,6 +10602,14 @@ gimple_with_undefined_signed_overflow (gimple *stmt)
   if (!INTEGRAL_TYPE_P (lhs_type)
       && !POINTER_TYPE_P (lhs_type))
     return false;
+  tree rhs = gimple_assign_rhs1 (stmt);
+  /* VCE from integral types to another integral types but with
+     different precisions need to be changed into casts
+     to be well defined. */
+  if (gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR
+      && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (rhs, 0)))
+      && is_gimple_val (TREE_OPERAND (rhs, 0)))
+    return true;
   if (!TYPE_OVERFLOW_UNDEFINED (lhs_type))
     return false;
   if (!arith_code_with_undefined_signed_overflow
@@ -10630,10 +10638,28 @@ rewrite_to_defined_overflow (gimple_stmt_iterator 
*gsi, gimple *stmt,
               "overflow ");
       print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
     }
-
+  gimple_seq stmts = NULL;
+  /* VCE from integral types to another integral types but with
+     different precisions need to be changed into casts
+     to be well defined. */
+  if (gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR)
+    {
+      tree rhs = gimple_assign_rhs1 (stmt);
+      tree new_rhs = TREE_OPERAND (rhs, 0);
+      gcc_assert (is_gimple_val (new_rhs));
+      gimple_assign_set_rhs_code (stmt, NOP_EXPR);
+      gimple_assign_set_rhs1 (stmt, new_rhs);
+      if (in_place)
+         update_stmt (stmt);
+      else
+       {
+         gimple_set_modified (stmt, true);
+         gimple_seq_add_stmt (&stmts, stmt);
+       }
+      return stmts;
+    }
   tree lhs = gimple_assign_lhs (stmt);
   tree type = unsigned_type_for (TREE_TYPE (lhs));
-  gimple_seq stmts = NULL;
   if (gimple_assign_rhs_code (stmt) == ABS_EXPR)
     gimple_assign_set_rhs_code (stmt, ABSU_EXPR);
   else
-- 
2.34.1

Reply via email to