public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Matt Turner <mattst88@gmail.com>
To: gcc@gcc.gnu.org
Cc: Uros Bizjak <ubizjak@gmail.com>, Richard Henderson <rth@twiddle.net>
Subject: [alpha] Wrong code produced at -Os, -O2, and -O3
Date: Wed, 07 Apr 2010 18:47:00 -0000	[thread overview]
Message-ID: <v2ob4198de61004071138p91fba9aao9fd48d4f91695170@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1452 bytes --]

Hi Uros and Richard,
I was rewriting the Alpha sched_find_first_bit implementation for the
Linux Kernel, and in the process I think I've come across a gcc bug.

I rewrote the function using cmov instructions, and wrote a small
program to test its correctness and performance. I wrote the function
initially as an external .S file, and once I was reasonably sure it
was correct, converted it to C function with inline assembly.
Compiling both produce the exact same output, as shown.

<rewritten>:
        ldq     t0,0(a0)
        clr     t2
        ldq     t1,8(a0)
        cmoveq  t0,0x40,t2
        cmoveq  t0,t1,t0
        cttz    t0,t3
        addq    t3,t2,v0
        ret

In my test program, I found that when I executed the rewritten
implementation _before_ the reference implementation that it produced
bogus results. This only happens when using the C/inline asm function.
When compiled with the external .S file, the results are correct.

Attached is a tar.gz with my test code. Compile the test program with
`gcc -O -mcpu=... find.c rewritten.S test.c -o test` with optional
-D__REWRITTEN_INLINE and -D__REWRITTEN_FIRST. At -Os, -O2, or -O3 and
-D__REWRITTEN_INLINE and -D__REWRITTEN_FIRST the program will produce
incorrect results and assert(). At -O0 or -O1 or without one or both
of the -D flags, it will produce correct results. I've tested with
gcc-4.3.4 and gcc-4.4.2.

Thanks. Let me know what I can do to help further.

Matt Turner

[-- Attachment #2: sched_find_first_bit.tar.gz --]
[-- Type: application/x-gzip, Size: 1463 bytes --]

             reply	other threads:[~2010-04-07 18:38 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-04-07 18:47 Matt Turner [this message]
2010-04-08  9:06 ` Uros Bizjak
2010-04-08 17:39   ` Matt Turner

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=v2ob4198de61004071138p91fba9aao9fd48d4f91695170@mail.gmail.com \
    --to=mattst88@gmail.com \
    --cc=gcc@gcc.gnu.org \
    --cc=rth@twiddle.net \
    --cc=ubizjak@gmail.com \
    /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).