public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Peter Osterlund <peter.osterlund@mailbox.swipnet.se>
To: David Korn <dkorn@pixelpower.com>
Cc: "'Denis Chertykov'" <denisc@overta.ru>, gcc@gcc.gnu.org
Subject: Re: Bug in loop optimize (invalid postinc to preinc transformation)
Date: Wed, 27 Dec 2000 15:42:00 -0000	[thread overview]
Message-ID: <m2snn9qvov.fsf@ppro.localdomain> (raw)
In-Reply-To: <718D38CAB6E0D011B2C90060970C28A5642545@EXCHANGESERVER>

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1741 bytes --]

David Korn <dkorn@pixelpower.com> writes:

> >-----Original Message-----
> >From: Denis Chertykov [ mailto:denisc@overta.ru ]
> >Sent: 26 December 2000 20:06
> 
> >"Alexander Popov" <sasho@vip.orbitel.bg> have founded a bug in generic
> >GCC code.
> >I have tried  avr and x86 ports.
> >
> >The XXX.rtl file already have a bug.
> >
> >
> >volatile unsigned char *p;
> >volatile unsigned char i;
> >
> >int main(void) {
> >
> >   do {
> >   	i++;
> >   } while(p++<(unsigned char *)0xffffffff);
> >  /* if 0xffffffff change to 0xfffffffe then no bug */
> >   return 0;
> >}
> 
>   This is undefined behaviour. Since you haven't initialised variable p,
> the compiler is free to implement whatever it likes here. In particular,

No it is not. The p and i variables have static storage, so they are
implicitly initialized to 0. (As expected, even if you explicitly
initialize the variables, the bug doesn't go away.)

Compiling without optimization indicates that the compiler is
transforming (p++ < x) into (++p < (x+1)), even when not optimizing.
This transformation is incorrect because x+1 wraps around. At -O1 the
compiler realizes that testing for <0 will always fail and therefore
the test is removed, resulting in the assembly code below.

(This bug also exists in gcc 2.95 and egcs 1.1.2)

> >.globl main
> >	.type	main,@function
> >main:
> >	movb	i, %al
> >	pushl	%ebp
> >	incl	%eax
> >	movl	%esp, %ebp
> >	incl	p
> >	movb	%al, i
> >	xorl	%eax, %eax
> >	popl	%ebp
> >	ret

-- 
Peter Österlund          Email:     peter.osterlund@mailbox.swipnet.se
Sköndalsvägen 35                    f90-pos@nada.kth.se
S-128 66 Sköndal         Home page: http://home1.swipnet.se/~w-15919
Sweden                   Phone:     +46 8 942647

  reply	other threads:[~2000-12-27 15:42 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-12-27  3:04 Bug in loop optimize (TREE stage) David Korn
2000-12-27 15:42 ` Peter Osterlund [this message]
2000-12-27 19:55   ` Bug in loop optimize (invalid postinc to preinc transformation) Alexandre Oliva
2000-12-27 21:09     ` Torbjorn Granlund
2000-12-27 21:20       ` Alexandre Oliva
2000-12-27 22:24     ` Jamie Lokier
2000-12-31  8:25       ` Andreas Schwab
2000-12-27 22:40 Robert Dewar
2000-12-29 18:36 ` Jamie Lokier
2000-12-28  1:14 Richard Kenner
2000-12-28  5:40 Robert Dewar
2000-12-28 12:21 ` Alexandre Oliva
2000-12-28 14:32   ` Geoff Keating
2000-12-28 15:22     ` Tim Hollebeek
2000-12-28 22:20       ` Dave Korn
2000-12-29  0:52         ` Richard Henderson
2000-12-29  1:51         ` Tim Hollebeek
2000-12-28 20:33 dewar
2000-12-29 18:43 dewar
2000-12-30 15:18 ` Toon Moene
2000-12-31  8:30 dewar
2001-01-03  0:52 ` Jamie Lokier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m2snn9qvov.fsf@ppro.localdomain \
    --to=peter.osterlund@mailbox.swipnet.se \
    --cc=denisc@overta.ru \
    --cc=dkorn@pixelpower.com \
    --cc=gcc@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).