public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "slyfox at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug target/110790] New: [14 Regression] gcc -m32 generates invalid bit test code on gmp-6.2.1
Date: Mon, 24 Jul 2023 10:02:26 +0000	[thread overview]
Message-ID: <bug-110790-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110790

            Bug ID: 110790
           Summary: [14 Regression] gcc -m32 generates invalid bit test
                    code on gmp-6.2.1
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: slyfox at gcc dot gnu.org
  Target Milestone: ---

Initially observed test failures on gmp-6.2.1 when building with gcc
r14-2736-gd07504725973cc

Extracted reproducer:

// $ cat /tmp/a.c
typedef unsigned long int mp_limb_t;
typedef const mp_limb_t * mp_srcptr;

__attribute__((noipa))
int
refmpn_tstbit_bad (mp_srcptr ptr, unsigned long bit)
{
  return (((ptr)[(bit)/(32 - 0)] & (((mp_limb_t) 1L) << ((bit)%(32 - 0)))) !=
0);
}

__attribute__((noipa, optimize(0)))
int
refmpn_tstbit_good (mp_srcptr ptr, unsigned long bit)
{
  return (((ptr)[(bit)/(32 - 0)] & (((mp_limb_t) 1L) << ((bit)%(32 - 0)))) !=
0);
}

__attribute__((noipa))
int
refmpn_tstbit (mp_srcptr ptr, unsigned long bit)
{
  if (refmpn_tstbit_bad (ptr, bit) != refmpn_tstbit_good (ptr, bit)) {
      __builtin_trap();
  }
  return refmpn_tstbit_bad (ptr, bit);
}

int main(){
    unsigned long num[] = { 0x3801ff9f, 0x0, 0x0, 0x0 };
    refmpn_tstbit(num, 0);
}

$ gcc -m32 -fomit-frame-pointer /tmp/a.c -o /tmp/a -O2 && /tmp/a
Illegal instruction (core dumped)
$ gcc -m32 -fomit-frame-pointer /tmp/a.c -o /tmp/a && /tmp/a
# ok

It looks like refmpn_tstbit_bad() does something odd with bit shifts:

Dump of assembler code for function refmpn_tstbit_bad:
   0x08049200 <+0>:     mov    0x8(%esp),%eax ; bit
   0x08049204 <+4>:     mov    0x4(%esp),%edx ; ptr
   0x08049208 <+8>:     mov    %eax,%ecx
   0x0804920a <+10>:    shr    $0x5,%ecx          ; ecx = bit / 32 (limbs)
   0x0804920d <+13>:    mov    (%edx,%ecx,4),%edx ; edx = ptr[ecx]
   0x08049210 <+16>:    bt     %eax,%edx          ; might be ok if 'bit' does
not overflow?
   0x08049213 <+19>:    setae  %al
   0x08049216 <+22>:    movzbl %al,%eax
   0x08049219 <+25>:    ret

I can't see any problems with this code on the data we pass here.

$ gcc -v
Using built-in specs.
COLLECT_GCC=/<<NIX>>/xgcc-14.0.0/bin/gcc
COLLECT_LTO_WRAPPER=/<<NIX>>/xgcc-14.0.0/libexec/gcc/i686-unknown-linux-gnu/14.0.0/lto-wrapper
Target: i686-unknown-linux-gnu
Configured with: ../source/configure --prefix=/<<NIX>>/xgcc-14.0.0
--with-gmp-include=/<<NIX>>/gmp-6.2.1-dev/include
--with-gmp-lib=/<<NIX>>/gmp-6.2.1/lib
--with-mpfr-include=/<<NIX>>/mpfr-4.2.0-dev/include
--with-mpfr-lib=/<<NIX>>/mpfr-4.2.0/lib --with-mpc=/<<NIX>>/libmpc-1.3.1
--with-native-system-header-dir=/<<NIX>>/bootstrap-stage0-glibc-bootstrapFiles/include
--with-build-sysroot=/ --program-prefix= --disable-lto --disable-libstdcxx-pch
--without-included-gettext --with-system-zlib --enable-checking=release
--enable-static --enable-languages=c,c++ --disable-multilib --enable-plugin
--disable-libcc1 --with-isl=/<<NIX>>/isl-0.20 --disable-bootstrap
--with-arch=i686 --with-native-system-header-dir=/include
--with-build-sysroot=/<<NIX>>/bootstrap-stage0-glibc-bootstrapFiles
--build=i686-unknown-linux-gnu --host=i686-unknown-linux-gnu
--target=i686-unknown-linux-gnu
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 14.0.0 99999999 (experimental) (GCC)

refmpn_tstbit_bad() gets compile

             reply	other threads:[~2023-07-24 10:02 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-24 10:02 slyfox at gcc dot gnu.org [this message]
2023-07-24 10:12 ` [Bug target/110790] " slyfox at gcc dot gnu.org
2023-07-24 11:35 ` cvs-commit at gcc dot gnu.org
2023-07-24 12:17 ` slyfox at gcc dot gnu.org
2023-07-24 13:24 ` rguenth at gcc dot gnu.org
2023-07-25 11:24 ` roger at nextmovesoftware dot com
2023-07-29 16:08 ` cvs-commit at gcc dot gnu.org
2023-07-29 17:14 ` slyfox at gcc dot gnu.org
2023-11-12 19:49 ` pinskia at gcc dot gnu.org
2023-11-12 20:15 ` ubizjak at gmail dot com

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=bug-110790-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@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).