From 81ad578ea1b8fbeafe172152cc27c0f4911502ce Mon Sep 17 00:00:00 2001
From: Richard Guenther <rguenther@suse.de>
Date: Tue, 29 Apr 2008 13:52:53 +0000
Subject: [PATCH] re PR middle-end/36077 (Expressions result is wrong)

2008-04-29  Richard Guenther  <rguenther@suse.de>

	PR middle-end/36077
	* fold-const.c (extract_muldiv_1): In combining division constants
	make sure to never overflow.

	* gcc.c-torture/execute/pr36077.c: New testcase.

From-SVN: r134792
---
 gcc/ChangeLog                                 |  6 ++++++
 gcc/fold-const.c                              |  9 +++++++--
 gcc/testsuite/ChangeLog                       |  5 +++++
 gcc/testsuite/gcc.c-torture/execute/pr36077.c | 13 +++++++++++++
 4 files changed, 31 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr36077.c

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 1c7cba1c5684..3ffe3bc08f3d 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,9 @@
+2008-04-29  Richard Guenther  <rguenther@suse.de>
+
+	PR middle-end/36077
+	* fold-const.c (extract_muldiv_1): In combining division constants
+	make sure to never overflow.
+
 2008-04-29  Nick Clifton  <nickc@redhat.com>
 
 	* doc/tm.texi (RETURN_ADDR_RTX): Fix typo.
diff --git a/gcc/fold-const.c b/gcc/fold-const.c
index a4d5760b8619..aae203777289 100644
--- a/gcc/fold-const.c
+++ b/gcc/fold-const.c
@@ -5953,8 +5953,13 @@ extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
       /* If these are the same operation types, we can associate them
 	 assuming no overflow.  */
       if (tcode == code
-	  && 0 != (t1 = const_binop (MULT_EXPR, fold_convert (ctype, op1),
-				     fold_convert (ctype, c), 0))
+	  && 0 != (t1 = int_const_binop (MULT_EXPR, fold_convert (ctype, op1),
+					 fold_convert (ctype, c), 1))
+	  && 0 != (t1 = force_fit_type_double (ctype, TREE_INT_CST_LOW (t1),
+					       TREE_INT_CST_HIGH (t1),
+					       (TYPE_UNSIGNED (ctype)
+					        && tcode != MULT_EXPR) ? -1 : 1,
+					       TREE_OVERFLOW (t1)))
 	  && !TREE_OVERFLOW (t1))
 	return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
 
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index f6bb5e1aadc4..f2b69e90afca 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2008-04-29  Richard Guenther  <rguenther@suse.de>
+
+	PR middle-end/36077
+	* gcc.c-torture/execute/pr36077.c: New testcase.
+
 2008-04-29  Kai Tietz  <kai.tietz@onevision.com>
 
 	* testsuite/gcc.dg/sync-2.c: Set dg-options "-ansi".
diff --git a/gcc/testsuite/gcc.c-torture/execute/pr36077.c b/gcc/testsuite/gcc.c-torture/execute/pr36077.c
new file mode 100644
index 000000000000..09636a10286f
--- /dev/null
+++ b/gcc/testsuite/gcc.c-torture/execute/pr36077.c
@@ -0,0 +1,13 @@
+extern void abort (void);
+
+unsigned int test (unsigned int x)
+{
+  return x / 0x80000001U / 0x00000002U;
+}
+
+int main()
+{
+  if (test(2) != 0)
+    abort ();
+  return 0;
+}
-- 
GitLab