User account creation filtered due to spam.

Bug 8794 - [3.2 regression] optimization improperly eliminates certain expressions
Summary: [3.2 regression] optimization improperly eliminates certain expressions
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: rtl-optimization (show other bugs)
Version: 3.2.1
: P1 critical
Target Milestone: ---
Assignee: Eric Botcazou
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2002-12-03 06:36 UTC by PaX Team
Modified: 2003-07-25 17:33 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description PaX Team 2002-12-03 06:36:01 UTC
the following code is miscompiled when any optimization (-O, -O2, etc) is enabled, works otherwise. the miscompiled code falsely evaluates the expression on the right hand side of += to nothing (ie. no code emitted for it, presumably because gcc thought that it was a constant 0 which is not true for values of 'addr' that are not aligned to ELF_PAGE_SIZE). example runs:

good (gcc -o a.out a.c):
a.out 0 -> 00000000
a.out 1 -> 00001001
a.out 4095 -> 00001FFF
a.out 4096 -> 00001000

bad (gcc -O2 -o a.out a.c):
a.out 1 -> 00000001
a.out 4095 -> 00000FFF

it's also worth noting that in the real life code where this bug showed up we managed to get the optimized version to produce proper code by adding some extra expressions working on 'addr' before the ominous one (that may explain why it wasn't found already, it apparently needs some 'context' to show up).

------- cut -------
#include <stdio.h>
#include <stdlib.h>

#define ELF_PAGE_SIZE 0x1000UL

int main(int argc, char* argv[])
{
  unsigned long addr = atoi(argv[1]);
  addr += ELF_PAGE_SIZE - (ELF_PAGE_SIZE & (ELF_PAGE_SIZE - (addr & (ELF_PAGE_SIZE-1))));
  printf("addr: %08lX\n", addr);
  return 0;
}

Release:
gcc 3.2.1

Environment:
i386 linux (Athlon), kernel 2.4.20, locally compiled gcc 3.2.1 (--enable-shared --with-system-zlib --enable-nls --without-included-gettext --enable-__cxa_atexit --enable-clocale=gnu)
Comment 1 Volker Reichelt 2002-12-03 08:10:01 UTC
State-Changed-From-To: open->analyzed
State-Changed-Why: Confirmed.
    
    OK with gcc 2.95.x, but fails with gcc 3.0.x, 3.2-branch,
    and mainline. (Checked also on mips-sgi-irix6.5.)
Comment 2 Christian Ehrhardt 2002-12-03 17:24:08 UTC
From: "Christian Ehrhardt" <ehrhardt@mathematik.uni-ulm.de>
To: pageexec@freemail.hu
Cc: gcc-gnats@gcc.gnu.org, gcc-bugs@gcc.gnu.org
Subject: Re: optimization/8794: optimization improperly eliminates certain expressions
Date: Tue, 3 Dec 2002 17:24:08 +0100

 On Tue, Dec 03, 2002 at 02:29:05PM -0000, pageexec@freemail.hu wrote:
 > >Number:         8794
 > >Category:       optimization
 > >Synopsis:       optimization improperly eliminates certain expressions
 > >Release:        gcc 3.2.1
 
 Confirmed on i686. The orignal example works ok on sparc but a slightly
 simplified example produces bad code on sparc with 3.2.1 and 3.3:
 
 ------------- cut ------------------
 unsigned int addr;
 void f (void)
 {
 	addr = (1024 & (1024UL - (addr & 1023UL)));
 }
 ------------- cut ------------------
 
 Is compiled into:
 
 ------------- cut ------------------
 	.file	"8794.c"
 	.section	".text"
 	.align 4
 	.global f
 	.type	f,#function
 	.proc	020
 f:
 	!#PROLOGUE# 0
 	!#PROLOGUE# 1
 	mov	1024, %o1
 	sethi	%hi(addr), %o0
 	retl
 	st	%o1, [%o0+%lo(addr)]
 .LLfe1:
 	.size	f,.LLfe1-f
 	.common	addr,4,4
 	.ident	"GCC: (GNU) 3.2.1 20021111 (prerelease)"
 ------------- cut ------------------
 
 Which is clearly wrong. The necessary instructions are elimiated by
 between 8794.c.15.life and 8794.c.16.combine.
 
       regards   Christian
 
 -- 
 THAT'S ALL FOLKS!

Comment 3 Eric Botcazou 2002-12-05 19:49:40 UTC
From: Eric Botcazou <ebotcazou@libertysurf.fr>
To: reichelt@igpm.rwth-aachen.de
Cc: gcc-bugs@gcc.gnu.org,
 gcc-prs@gcc.gnu.org,
 nobody@gcc.gnu.org,
 pageexec@freemail.hu,
 gcc-gnats@gcc.gnu.org
Subject: Re: optimization/8794: [3.2/3.3 regression] optimization improperly eliminates certain expressions
Date: Thu, 5 Dec 2002 19:49:40 +0100

 > Old Synopsis: optimization improperly eliminates certain expressions
 > New Synopsis: [3.2/3.3 regression] optimization improperly eliminates
 > certain expressions
 >
 > State-Changed-From-To: open->analyzed
 > State-Changed-By: reichelt
 > State-Changed-When: Tue Dec  3 08:10:01 2002
 > State-Changed-Why:
 >     Confirmed.
 >
 >     OK with gcc 2.95.x, but fails with gcc 3.0.x, 3.2-branch,
 >     and mainline. (Checked also on mips-sgi-irix6.5.)
 
 Are you sure for mainline ? I can't reproduce it with:
 gcc version 3.3 20021205 (experimental)
 
 The fix seems to be:
 
 2002-11-20  Jakub Jelinek  <jakub@redhat.com>
 
 =09* combine.c (force_to_mode): Only replace with (not Y) if all bits in
 =09fuller_mask (not just mask) are set in C.
 
 http://gcc.gnu.org/ml/gcc-patches/2002-11/msg01045.html
 
 --=20
 Eric Botcazou

Comment 4 pcarlini 2002-12-05 20:01:33 UTC
From: Paolo Carlini <pcarlini@unitus.it>
To: gcc-gnats@gcc.gnu.org,  gcc-prs@gcc.gnu.org,  pageexec@freemail.hu, 
 gcc-bugs@gcc.gnu.org,  nobody@gcc.gnu.org
Cc: ebotcazou@libertysurf.fr,  reichelt@igpm.rwth-aachen.de
Subject: Re: optimization/8794: [3.2/3.3 regression] optimization improperly
 eliminates certain expressions
Date: Thu, 05 Dec 2002 20:01:33 +0100

 Neither can I, with 3.3 20021204.
 
 Ciao,
 Paolo.
 
 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=8794
 

Comment 5 Eric Botcazou 2002-12-05 20:56:30 UTC
From: Eric Botcazou <ebotcazou@libertysurf.fr>
To: Paolo Carlini <pcarlini@unitus.it>
Cc: reichelt@igpm.rwth-aachen.de,
 gcc-gnats@gcc.gnu.org,
 gcc-prs@gcc.gnu.org,
 pageexec@freemail.hu,
 gcc-bugs@gcc.gnu.org,
 nobody@gcc.gnu.org
Subject: Re: optimization/8794: [3.2/3.3 regression] optimization improperly eliminates certain expressions
Date: Thu, 5 Dec 2002 20:56:30 +0100

 > Neither can I, with 3.3 20021204
 
 Ok. I confirm that Jakub's patch is the fix.
 
 --=20
 Eric Botcazou

Comment 6 Volker Reichelt 2002-12-06 10:41:30 UTC
From: Volker Reichelt <reichelt@igpm.rwth-aachen.de>
To: ebotcazou@libertysurf.fr, pcarlini@unitus.it
Cc: gcc-gnats@gcc.gnu.org, pageexec@freemail.hu, gcc-bugs@gcc.gnu.org
Subject: Re: optimization/8794: [3.2/3.3 regression] optimization improperly eliminates certain expressions
Date: Fri, 6 Dec 2002 10:41:30 +0100

 Sorry for the confusion.
 
 I checked mainline with the 20021118 snapshot where the bug was still
 present. In the snapshot from 20021202 the bug is fixed.
 
 Regards,
 Volker
 
 
Comment 7 Eric Botcazou 2002-12-16 08:46:04 UTC
Responsible-Changed-From-To: unassigned->ebotcazou
Responsible-Changed-Why: Backporting Jakub's patch.
Comment 8 Eric Botcazou 2002-12-18 03:05:02 UTC
State-Changed-From-To: analyzed->closed
State-Changed-Why: http://gcc.gnu.org/ml/gcc-patches/2002-11/msg01045.html