From 5bf51f2f731c28ea9b45e170fc36a2b0d51ba9b2 Mon Sep 17 00:00:00 2001
From: Jakub Jelinek <jakub@redhat.com>
Date: Wed, 4 Dec 2013 12:25:06 +0100
Subject: [PATCH] re PR rtl-optimization/58726 (wrong code at -Os on
 x86_64-linux-gnu (affecting trunk/4.7/4.6, but not 4.8))

	PR rtl-optimization/58726
	* combine.c (force_to_mode): Fix comment typo.  Don't destructively
	modify x for ROTATE, ROTATERT and IF_THEN_ELSE.

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

From-SVN: r205664
---
 gcc/ChangeLog                                 |  6 +++++
 gcc/combine.c                                 | 27 ++++++++++---------
 gcc/testsuite/ChangeLog                       |  3 +++
 gcc/testsuite/gcc.c-torture/execute/pr58726.c | 26 ++++++++++++++++++
 4 files changed, 50 insertions(+), 12 deletions(-)
 create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr58726.c

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 56b4f477f785..081787c094fd 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,9 @@
+2013-12-04  Jakub Jelinek  <jakub@redhat.com>
+
+	PR rtl-optimization/58726
+	* combine.c (force_to_mode): Fix comment typo.  Don't destructively
+	modify x for ROTATE, ROTATERT and IF_THEN_ELSE.
+
 2013-12-04  Jakub Jelinek  <jakub@redhat.com>
 	    Uros Bizjak  <ubizjak@gmail.com>
 
diff --git a/gcc/combine.c b/gcc/combine.c
index 25cf273f8080..c7eb5e5b0241 100644
--- a/gcc/combine.c
+++ b/gcc/combine.c
@@ -8029,7 +8029,7 @@ force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
   if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
     return x;
 
-  /* We want to perform the operation is its present mode unless we know
+  /* We want to perform the operation in its present mode unless we know
      that the operation is valid in MODE, in which case we do the operation
      in MODE.  */
   op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
@@ -8460,9 +8460,10 @@ force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
 					    gen_int_mode (mask, GET_MODE (x)),
 					    XEXP (x, 1));
 	  if (temp && CONST_INT_P (temp))
-	    SUBST (XEXP (x, 0),
-		   force_to_mode (XEXP (x, 0), GET_MODE (x),
-				  INTVAL (temp), next_select));
+	    x = simplify_gen_binary (code, GET_MODE (x),
+				     force_to_mode (XEXP (x, 0), GET_MODE (x),
+						    INTVAL (temp), next_select),
+				     XEXP (x, 1));
 	}
       break;
 
@@ -8530,14 +8531,16 @@ force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
       /* We have no way of knowing if the IF_THEN_ELSE can itself be
 	 written in a narrower mode.  We play it safe and do not do so.  */
 
-      SUBST (XEXP (x, 1),
-	     gen_lowpart_or_truncate (GET_MODE (x),
-				      force_to_mode (XEXP (x, 1), mode,
-						     mask, next_select)));
-      SUBST (XEXP (x, 2),
-	     gen_lowpart_or_truncate (GET_MODE (x),
-				      force_to_mode (XEXP (x, 2), mode,
-						     mask, next_select)));
+      op0 = gen_lowpart_or_truncate (GET_MODE (x),
+				     force_to_mode (XEXP (x, 1), mode,
+						    mask, next_select));
+      op1 = gen_lowpart_or_truncate (GET_MODE (x),
+				     force_to_mode (XEXP (x, 2), mode,
+						    mask, next_select));
+      if (op0 != XEXP (x, 1) || op1 != XEXP (x, 2))
+	x = simplify_gen_ternary (IF_THEN_ELSE, GET_MODE (x),
+				  GET_MODE (XEXP (x, 0)), XEXP (x, 0),
+				  op0, op1);
       break;
 
     default:
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 32b4ff41e3e2..912b117ac0ee 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,5 +1,8 @@
 2013-12-04  Jakub Jelinek  <jakub@redhat.com>
 
+	PR rtl-optimization/58726
+	* gcc.c-torture/execute/pr58726.c: New test.
+
 	PR target/59163
 	* g++.dg/torture/pr59163.C: New test.
 
diff --git a/gcc/testsuite/gcc.c-torture/execute/pr58726.c b/gcc/testsuite/gcc.c-torture/execute/pr58726.c
new file mode 100644
index 000000000000..9fa8b6953f19
--- /dev/null
+++ b/gcc/testsuite/gcc.c-torture/execute/pr58726.c
@@ -0,0 +1,26 @@
+/* PR rtl-optimization/58726 */
+
+int a, c;
+union { int f1; int f2 : 1; } b;
+
+short
+foo (short p)
+{
+  return p < 0 ? p : a;
+}
+
+int
+main ()
+{
+  if (sizeof (short) * __CHAR_BIT__ != 16
+      || sizeof (int) * __CHAR_BIT__ != 32)
+    return 0;
+  b.f1 = 56374;
+  unsigned short d;
+  int e = b.f2;
+  d = e == 0 ? b.f1 : 0;
+  c = foo (d);
+  if (c != (short) 56374)
+    __builtin_abort ();
+  return 0;
+}
-- 
GitLab