public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/56250] New: Wrong constant folding on unsigned int
@ 2013-02-08  1:39 ishiura-compiler at ml dot kwansei.ac.jp
  2013-02-08  1:52 ` [Bug c/56250] " pinskia at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: ishiura-compiler at ml dot kwansei.ac.jp @ 2013-02-08  1:39 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56250

             Bug #: 56250
           Summary: Wrong constant folding on unsigned int
    Classification: Unclassified
           Product: gcc
           Version: 4.7.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: ishiura-compiler@ml.kwansei.ac.jp


GCC 4.7.2 and 4.8.0 miscompile the following code where UINT_MAX ==
4294967295.

  $ cat bug.c
  int main(void)
  {
         unsigned x = 2;
         return ((unsigned) 0 - ( x / 2 ) ) / 2;
  }
  $ gcc bug.c
  $ ./a.out
  $ echo $?
  0

Unsigned (0-(x/2))/2 should evaluate to 2147483647 instead of 0.  
The following code with variable x replaced by "(unsigned) 2" is
correctly compiled.

  $ cat nobug.c
  int main(void)
  {
         return ((unsigned) 0 - ( (unsigned) 2 / 2 ) ) / 2;
  }
  $ gcc nobug.c
  $ ./a.out
  $ echo $?
  255

The miscompile seems to occur on many versions of gcc (4.2 - 4.8, and
even 4.0 and 3.1 on i686-apple-darwin, 4.4 - 4.6 on
i686-linux-gnu-gcc, etc.)


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c/56250] Wrong constant folding on unsigned int
  2013-02-08  1:39 [Bug c/56250] New: Wrong constant folding on unsigned int ishiura-compiler at ml dot kwansei.ac.jp
@ 2013-02-08  1:52 ` pinskia at gcc dot gnu.org
  2013-02-08  1:54 ` [Bug middle-end/56250] " pinskia at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2013-02-08  1:52 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56250

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> 2013-02-08 01:51:39 UTC ---
When I do this:
         unsigned x = 2;
         unsigned t = (x/2);
         unsigned t1 = -t;
         unsigned t2 = t2/2;

t2 is 0 which is correct.  I don't see why 2147483647 is correct.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug middle-end/56250] Wrong constant folding on unsigned int
  2013-02-08  1:39 [Bug c/56250] New: Wrong constant folding on unsigned int ishiura-compiler at ml dot kwansei.ac.jp
  2013-02-08  1:52 ` [Bug c/56250] " pinskia at gcc dot gnu.org
@ 2013-02-08  1:54 ` pinskia at gcc dot gnu.org
  2013-02-08  1:59 ` ishiura-compiler at ml dot kwansei.ac.jp
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2013-02-08  1:54 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56250

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
          Component|c                           |middle-end

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> 2013-02-08 01:54:23 UTC ---
Actually I get the correct results with the split out one.  The problem is
division is being merged together over the negative which is not valid for
wrapping cases.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug middle-end/56250] Wrong constant folding on unsigned int
  2013-02-08  1:39 [Bug c/56250] New: Wrong constant folding on unsigned int ishiura-compiler at ml dot kwansei.ac.jp
  2013-02-08  1:52 ` [Bug c/56250] " pinskia at gcc dot gnu.org
  2013-02-08  1:54 ` [Bug middle-end/56250] " pinskia at gcc dot gnu.org
@ 2013-02-08  1:59 ` ishiura-compiler at ml dot kwansei.ac.jp
  2013-02-08  8:31 ` jakub at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: ishiura-compiler at ml dot kwansei.ac.jp @ 2013-02-08  1:59 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56250

--- Comment #3 from Ishiura Lab Compiler Team <ishiura-compiler at ml dot kwansei.ac.jp> 2013-02-08 01:59:18 UTC ---
(In reply to comment #1)
> When I do this:
>          unsigned x = 2;
>          unsigned t = (x/2);
>          unsigned t1 = -t;
>          unsigned t2 = t2/2;
> 
> t2 is 0 which is correct.  I don't see why 2147483647 is correct.

The last line should be
    unsigned t2 = t1/2;


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug middle-end/56250] Wrong constant folding on unsigned int
  2013-02-08  1:39 [Bug c/56250] New: Wrong constant folding on unsigned int ishiura-compiler at ml dot kwansei.ac.jp
                   ` (2 preceding siblings ...)
  2013-02-08  1:59 ` ishiura-compiler at ml dot kwansei.ac.jp
@ 2013-02-08  8:31 ` jakub at gcc dot gnu.org
  2013-02-08 11:24 ` [Bug tree-optimization/56250] " jakub at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu.org @ 2013-02-08  8:31 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56250

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |ASSIGNED
   Last reconfirmed|                            |2013-02-08
                 CC|                            |jakub at gcc dot gnu.org
         AssignedTo|unassigned at gcc dot       |jakub at gcc dot gnu.org
                   |gnu.org                     |
     Ever Confirmed|0                           |1

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> 2013-02-08 08:31:03 UTC ---
Bug is in extract_muldiv_1.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug tree-optimization/56250] Wrong constant folding on unsigned int
  2013-02-08  1:39 [Bug c/56250] New: Wrong constant folding on unsigned int ishiura-compiler at ml dot kwansei.ac.jp
                   ` (3 preceding siblings ...)
  2013-02-08  8:31 ` jakub at gcc dot gnu.org
@ 2013-02-08 11:24 ` jakub at gcc dot gnu.org
  2013-02-08 15:07 ` jakub at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu.org @ 2013-02-08 11:24 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56250

--- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> 2013-02-08 11:24:02 UTC ---
Created attachment 29394
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=29394
gcc48-pr56250.patch

Untested fix.

I believe only division/modulus is problematic, not multiplication, and I think
-fwrapv signed types are fine too.  For *_DIV_EXPR/*_MOD_EXPR, I think
wide_type is always NULL_TREE and ctype is always equal to type.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug tree-optimization/56250] Wrong constant folding on unsigned int
  2013-02-08  1:39 [Bug c/56250] New: Wrong constant folding on unsigned int ishiura-compiler at ml dot kwansei.ac.jp
                   ` (4 preceding siblings ...)
  2013-02-08 11:24 ` [Bug tree-optimization/56250] " jakub at gcc dot gnu.org
@ 2013-02-08 15:07 ` jakub at gcc dot gnu.org
  2013-02-08 15:24 ` jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu.org @ 2013-02-08 15:07 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56250

--- Comment #6 from Jakub Jelinek <jakub at gcc dot gnu.org> 2013-02-08 15:06:37 UTC ---
Author: jakub
Date: Fri Feb  8 15:06:26 2013
New Revision: 195888

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=195888
Log:
    PR tree-optimization/56250
    * fold-const.c (extract_muldiv_1) <case NEGATE_EXPR>: Don't optimize
    if type is unsigned and code isn't MULT_EXPR.

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

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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug tree-optimization/56250] Wrong constant folding on unsigned int
  2013-02-08  1:39 [Bug c/56250] New: Wrong constant folding on unsigned int ishiura-compiler at ml dot kwansei.ac.jp
                   ` (5 preceding siblings ...)
  2013-02-08 15:07 ` jakub at gcc dot gnu.org
@ 2013-02-08 15:24 ` jakub at gcc dot gnu.org
  2013-02-19 17:26 ` jakub at gcc dot gnu.org
  2013-02-19 17:44 ` jakub at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu.org @ 2013-02-08 15:24 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56250

--- Comment #7 from Jakub Jelinek <jakub at gcc dot gnu.org> 2013-02-08 15:23:43 UTC ---
Fixed on the trunk so far.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug tree-optimization/56250] Wrong constant folding on unsigned int
  2013-02-08  1:39 [Bug c/56250] New: Wrong constant folding on unsigned int ishiura-compiler at ml dot kwansei.ac.jp
                   ` (6 preceding siblings ...)
  2013-02-08 15:24 ` jakub at gcc dot gnu.org
@ 2013-02-19 17:26 ` jakub at gcc dot gnu.org
  2013-02-19 17:44 ` jakub at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu.org @ 2013-02-19 17:26 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56250

--- Comment #8 from Jakub Jelinek <jakub at gcc dot gnu.org> 2013-02-19 17:26:15 UTC ---
Author: jakub
Date: Tue Feb 19 17:26:04 2013
New Revision: 196147

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=196147
Log:
    Backported from mainline
    2013-02-08  Jakub Jelinek  <jakub@redhat.com>

    PR tree-optimization/56250
    * fold-const.c (extract_muldiv_1) <case NEGATE_EXPR>: Don't optimize
    if type is unsigned and code isn't MULT_EXPR.

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

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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug tree-optimization/56250] Wrong constant folding on unsigned int
  2013-02-08  1:39 [Bug c/56250] New: Wrong constant folding on unsigned int ishiura-compiler at ml dot kwansei.ac.jp
                   ` (7 preceding siblings ...)
  2013-02-19 17:26 ` jakub at gcc dot gnu.org
@ 2013-02-19 17:44 ` jakub at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu.org @ 2013-02-19 17:44 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56250

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED

--- Comment #9 from Jakub Jelinek <jakub at gcc dot gnu.org> 2013-02-19 17:43:36 UTC ---
Fixed for 4.7.3+.


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2013-02-19 17:44 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-02-08  1:39 [Bug c/56250] New: Wrong constant folding on unsigned int ishiura-compiler at ml dot kwansei.ac.jp
2013-02-08  1:52 ` [Bug c/56250] " pinskia at gcc dot gnu.org
2013-02-08  1:54 ` [Bug middle-end/56250] " pinskia at gcc dot gnu.org
2013-02-08  1:59 ` ishiura-compiler at ml dot kwansei.ac.jp
2013-02-08  8:31 ` jakub at gcc dot gnu.org
2013-02-08 11:24 ` [Bug tree-optimization/56250] " jakub at gcc dot gnu.org
2013-02-08 15:07 ` jakub at gcc dot gnu.org
2013-02-08 15:24 ` jakub at gcc dot gnu.org
2013-02-19 17:26 ` jakub at gcc dot gnu.org
2013-02-19 17:44 ` jakub at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).