Bug 36548 - [4.3 Regression] remainder gives the wrong result for wrapping case with unsigned types
Summary: [4.3 Regression] remainder gives the wrong result for wrapping case with unsi...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.4.0
: P3 normal
Target Milestone: 4.3.3
Assignee: Richard Biener
URL:
Keywords: wrong-code
: 37125 38817 (view as bug list)
Depends on:
Blocks:
 
Reported: 2008-06-17 01:42 UTC by John Regehr
Modified: 2009-01-12 21:56 UTC (History)
6 users (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Known to work: 4.3.3 4.4.0
Known to fail: 4.3.2
Last reconfirmed: 2008-08-22 10:35:02


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description John Regehr 2008-06-17 01:42:12 UTC
At -O and higher with -fwrapv, svn 136854 improperly compiles func_9 below into object code returning 0.

static unsigned long 
mod_rhs (long int rhs)
{
  return rhs;
}

unsigned g_8 = 0x0815898FL;
int func_9 (void)
{
  return ((0x0274952FL * g_8) % mod_rhs (0x0274952FL));
}
Comment 1 Andrew Pinski 2008-06-17 14:18:40 UTC
      /* We have a special case here if we are doing something like
	 (C * 8) % 4 since we know that's zero.  */
      if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
	   || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
	  && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
	  && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
	return omit_one_operand (type, integer_zero_node, op0);
Comment 2 Andrew Pinski 2008-06-17 14:20:24 UTC
That is only true for where overflow is undefined IIRC.  So if wrapping happens this is not true ...
Comment 3 Richard Biener 2008-06-21 16:20:09 UTC
Hm, but multiplication overflow behaves "sane".
Comment 4 Richard Biener 2008-08-22 10:34:25 UTC
*** Bug 37125 has been marked as a duplicate of this bug. ***
Comment 5 Richard Biener 2008-08-22 10:35:02 UTC
Mine.
Comment 6 Richard Biener 2008-08-22 12:45:32 UTC
Subject: Bug 36548

Author: rguenth
Date: Fri Aug 22 12:43:49 2008
New Revision: 139450

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=139450
Log:
2008-08-22  Richard Guenther  <rguenther@suse.de>

	PR middle-end/36548
	PR middle-end/37125
	* fold-const.c (extract_muldiv_1): Optimize (X * C1) % C2 only
	if the multiplication does not overflow.

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

Added:
    trunk/gcc/testsuite/gcc.c-torture/execute/pr37125.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/fold-const.c
    trunk/gcc/testsuite/ChangeLog

Comment 7 Richard Biener 2008-08-22 14:05:50 UTC
Fixed on the trunk.
Comment 8 Joseph S. Myers 2008-08-27 22:04:37 UTC
4.3.2 is released, changing milestones to 4.3.3.
Comment 9 Richard Biener 2008-08-28 14:18:37 UTC
Fixed.
Comment 10 Richard Biener 2008-08-28 14:19:47 UTC
Subject: Bug 36548

Author: rguenth
Date: Thu Aug 28 14:18:23 2008
New Revision: 139709

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=139709
Log:
2008-08-28  Richard Guenther  <rguenther@suse.de>

	PR middle-end/36548
	PR middle-end/37125
	* fold-const.c (extract_muldiv_1): Optimize (X * C1) % C2 only
	if the multiplication does not overflow.

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

Added:
    branches/gcc-4_3-branch/gcc/testsuite/gcc.c-torture/execute/pr37125.c
Modified:
    branches/gcc-4_3-branch/gcc/ChangeLog
    branches/gcc-4_3-branch/gcc/fold-const.c
    branches/gcc-4_3-branch/gcc/testsuite/ChangeLog

Comment 11 Andrew Pinski 2009-01-12 21:56:13 UTC
*** Bug 38817 has been marked as a duplicate of this bug. ***