From ea03a6ab4f22bc175bcb199acef6a70c7dc844a2 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Fri, 14 Dec 2018 12:30:49 -0500
Subject: [PATCH 12/21] FIXME: fold-const.c tests that I won't pursue (they may
 need a change to non_lvalue_loc)

---
 gcc/fold-const.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 67 insertions(+), 1 deletion(-)

diff --git a/gcc/fold-const.c b/gcc/fold-const.c
index 1851a3d..b6b8fbb 100644
--- a/gcc/fold-const.c
+++ b/gcc/fold-const.c
@@ -2614,6 +2614,8 @@ non_lvalue_loc (location_t loc, tree x)
   if (in_gimple_form)
     return x;
 
+  STRIP_ANY_LOCATION_WRAPPER (x);
+
   if (! maybe_lvalue_p (x))
     return x;
   return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
@@ -14728,7 +14730,8 @@ static void
 assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
 			     tree constant)
 {
-  ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
+  tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
+  ASSERT_EQ (result, constant);
 }
 
 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
@@ -14751,20 +14754,34 @@ static void
 test_arithmetic_folding ()
 {
   tree type = integer_type_node;
+  location_t loc = BUILTINS_LOCATION;
+
   tree x = create_tmp_var_raw (type, "x");
   tree zero = build_zero_cst (type);
   tree one = build_int_cst (type, 1);
 
+  /* As above, but with location wrappers.  */
+  tree wr_x = maybe_wrap_with_location (x, loc);
+  tree wr_zero = maybe_wrap_with_location (zero, loc);
+  tree wr_one = maybe_wrap_with_location (one, loc);
+
   /* Addition.  */
   /* 1 <-- (0 + 1) */
   assert_binop_folds_to_const (zero, PLUS_EXPR, one,
 			       one);
   assert_binop_folds_to_const (one, PLUS_EXPR, zero,
 			       one);
+  assert_binop_folds_to_const (wr_zero, PLUS_EXPR, wr_one,
+			       one);
+  assert_binop_folds_to_const (wr_one, PLUS_EXPR, wr_zero,
+			       one);
+
 
   /* (nonlvalue)x <-- (x + 0) */
   assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
 				   x);
+  assert_binop_folds_to_nonlvalue (wr_x, PLUS_EXPR, wr_zero,
+				   x);
 
   /* Subtraction.  */
   /* 0 <-- (x - x) */
@@ -14772,15 +14789,63 @@ test_arithmetic_folding ()
 			       zero);
   assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
 				   x);
+  assert_binop_folds_to_const (wr_x, MINUS_EXPR, wr_x,
+			       zero);
+  assert_binop_folds_to_nonlvalue (wr_x, MINUS_EXPR, wr_zero,
+				   x);
 
   /* Multiplication.  */
   /* 0 <-- (x * 0) */
   assert_binop_folds_to_const (x, MULT_EXPR, zero,
 			       zero);
+  assert_binop_folds_to_nonlvalue (wr_x, MULT_EXPR, wr_zero,
+				   zero);
 
   /* (nonlvalue)x <-- (x * 1) */
   assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
 				   x);
+  assert_binop_folds_to_nonlvalue (wr_x, MULT_EXPR, wr_one,
+				   x);
+}
+
+/* Verify that x * -1 is transformed into -x, in the presence of
+   location wrappers.  */
+
+static void
+test_foo ()
+{
+  location_t loc = BUILTINS_LOCATION;
+
+  tree i_1 = build_int_cst (integer_type_node, 1);
+  tree i_m1 = build_int_cst (integer_type_node, -1);
+
+  tree wr_i_1 = maybe_wrap_with_location (i_1, loc);
+  tree wr_i_m1 = maybe_wrap_with_location (i_m1, loc);
+  /* -1 as emitted by parser, before any folding.  */
+  tree minus_wr_i_1
+    = build1_loc (loc, NEGATE_EXPR, integer_type_node, wr_i_1);
+
+  tree res = fold (minus_wr_i_1);
+  ASSERT_EQ (TREE_CODE (res), INTEGER_CST);
+  ASSERT_EQ (res, i_m1);
+
+  tree x = create_tmp_var_raw (integer_type_node, "x");
+  tree wr_x = maybe_wrap_with_location (x, loc);
+
+  res = fold_build2 (MULT_EXPR, integer_type_node, x, i_m1);
+  ASSERT_EQ (TREE_CODE (res), NEGATE_EXPR);
+  ASSERT_EQ (TREE_OPERAND (res, 0), x);
+
+#if 0
+  res = fold_build2 (MULT_EXPR, integer_type_node, wr_x, minus_wr_i_1);
+  ASSERT_EQ (TREE_CODE (res), NEGATE_EXPR);
+  ASSERT_EQ (TREE_OPERAND (res, 0), x);
+#endif
+
+#if 0
+  res = fold_build2 (MULT_EXPR, integer_type_node, x, wr_i_1);
+  ASSERT_EQ (res, x);
+#endif
 }
 
 /* Verify that various binary operations on vectors are folded
@@ -14824,6 +14889,7 @@ void
 fold_const_c_tests ()
 {
   test_arithmetic_folding ();
+  test_foo ();
   test_vector_folding ();
   test_vec_duplicate_folding ();
 }
-- 
1.8.5.3

