public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/102627] New: wrong code with "-O1"
@ 2021-10-06  8:37 suochenyao at 163 dot com
  2021-10-06 10:13 ` [Bug tree-optimization/102627] " rguenth at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: suochenyao at 163 dot com @ 2021-10-06  8:37 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 102627
           Summary: wrong code with "-O1"
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: suochenyao at 163 dot com
  Target Milestone: ---

*******************************************************************************
OS and Platform:
CentOS Linux release 7.8.2003 (Core).0, x86_64 GNU/Linux
*******************************************************************************
Program:
int printf(const char *, ...);
int a, f, l, m, q, c, d, g;
long b, e;
struct g {
  signed h;
  signed i;
  unsigned j;
  unsigned k;
};
unsigned n;
char o;
int *p = &m;
long(r)(s) { return s && b ?: b; }
long v() {
  l = 0 || r(n & o);
  return q;
}
void w(int, unsigned, struct g x) {
  c ?: a;
  for (; d < 2; d++)
    *p = x.k;
}
struct g y() {
  struct g h = {3, 908, 1, 20};
  for (; g; g++)
    ;
  return h;
}
int main() {
  long t;
  struct g u = y();
  t = e << f;
  w(0, t, u);
  v(0, 4, 4, 4);
  printf("%d\n", m);
}
*******************************************************************************
version:
$ gcc -v
Using built-in specs.
COLLECT_GCC=/data/bin/gcc-dev/bin/gcc
COLLECT_LTO_WRAPPER=/data/bin/gcc-dev/libexec/gcc/x86_64-pc-linux-gnu/12.0.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../configure --prefix=/data/bin/gcc-dev/ --disable-multilib
--enable-languages=c,c++
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 12.0.0 20211006 (experimental) (GCC)

git ver: bb6194e0b44a8262d8de304be3bd3ee65187772a
*******************************************************************************
Command Lines:
$ gcc -O1 -Wall -Wextra -fno-strict-aliasing -fwrapv  a.c -o a1.o
a.c: In function ‘r’:
a.c:13:6: warning: type of ‘s’ defaults to ‘int’ [-Wimplicit-int]
   13 | long(r)(s) { return s && b ?: b; }
      |      ^
a.c:13:29: warning: the omitted middle operand in ‘?:’ will always be ‘true’,
suggest explicit middle operand [-Wparentheses]
   13 | long(r)(s) { return s && b ?: b; }
      |                             ^
$ gcc a.c -o a0.o
a.c: In function ‘r’:
a.c:13:6: warning: type of ‘s’ defaults to ‘int’ [-Wimplicit-int]
   13 | long(r)(s) { return s && b ?: b; }
      |      ^
$ ./a1.o
0
$ ./a0.o
20

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

* [Bug tree-optimization/102627] wrong code with "-O1"
  2021-10-06  8:37 [Bug tree-optimization/102627] New: wrong code with "-O1" suochenyao at 163 dot com
@ 2021-10-06 10:13 ` rguenth at gcc dot gnu.org
  2021-10-06 10:32 ` [Bug rtl-optimization/102627] [11/12 Regression] " jakub at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-10-06 10:13 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
Even with plain -O1 I see the wrong 0 result, -O0 and -O2 are fine.  Testcase
that aborts:

int a, f, l, m, q, c, d, g;
long b, e;
struct g {
  signed h;
  signed i;
  unsigned j;
  unsigned k;
};
unsigned n;
char o;
int *p = &m;
long r(int s) { return s && b ?: b; }
long __attribute__((noipa)) v() {
  l = 0 || r(n & o);
  return q;
}
void w(int, unsigned, struct g x) {
  c ?: a;
  for (; d < 2; d++)
    *p = x.k;
}
struct g __attribute__((noipa)) y() {
  struct g h = {3, 908, 1, 20};
  for (; g; g++)
    ;
  return h;
}
int main() {
  long t;
  struct g u = y();
  t = e << f;
  w(0, t, u);
  v(0, 4, 4, 4);
  if (m != 20)
    __builtin_abort ();
  return 0;
}

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

* [Bug rtl-optimization/102627] [11/12 Regression] wrong code with "-O1"
  2021-10-06  8:37 [Bug tree-optimization/102627] New: wrong code with "-O1" suochenyao at 163 dot com
  2021-10-06 10:13 ` [Bug tree-optimization/102627] " rguenth at gcc dot gnu.org
@ 2021-10-06 10:32 ` jakub at gcc dot gnu.org
  2021-10-06 10:42 ` jakub at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-10-06 10:32 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2021-10-06
   Target Milestone|---                         |11.3
          Component|tree-optimization           |rtl-optimization
           Priority|P3                          |P2
             Status|UNCONFIRMED                 |NEW
                 CC|                            |jakub at gcc dot gnu.org,
                   |                            |vmakarov at gcc dot gnu.org
            Summary|wrong code with "-O1"       |[11/12 Regression] wrong
                   |                            |code with "-O1"
     Ever confirmed|0                           |1

--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Started with r11-8008-g4bbd51afaa4a3c116fb538d912b35e126be80b41 (at least on
the #c1 testcase at -O1, I don't see any aliasing issue nor any other UB in the
source there so e.g. -fno-strict-aliasing or -fwrapv isn't needed) so possibly
a RA issue.

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

* [Bug rtl-optimization/102627] [11/12 Regression] wrong code with "-O1"
  2021-10-06  8:37 [Bug tree-optimization/102627] New: wrong code with "-O1" suochenyao at 163 dot com
  2021-10-06 10:13 ` [Bug tree-optimization/102627] " rguenth at gcc dot gnu.org
  2021-10-06 10:32 ` [Bug rtl-optimization/102627] [11/12 Regression] " jakub at gcc dot gnu.org
@ 2021-10-06 10:42 ` jakub at gcc dot gnu.org
  2021-10-07 19:54 ` vmakarov at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-10-06 10:42 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
The assembly difference r11-8007 to r11-8008 is:
--- pr102627.s  2021-10-06 06:32:46.000000000 -0400
+++ pr102627.s  2021-10-06 06:33:00.000000000 -0400
@@ -77,10 +77,10 @@ main:
        movq    %rdx, %rcx
        movq    %rax, %rdx
        movq    e(%rip), %rax
-       movq    %rcx, 8(%rsp)
+       movl    %ecx, 12(%rsp)
        movzbl  f(%rip), %ecx
        salq    %cl, %rax
-       movq    8(%rsp), %rcx
+       movl    12(%rsp), %ecx
        movq    %rax, %rsi
        movl    $0, %edi
        call    w
I believe y returns the 128-bit struct g return value in %rdx:%rax pair, right
before the above instructions, and the above change means that instead of
spilling the whole 64-bits of %rcx that holds at that point u.j and u.k members
(u.k in the upper 32 bits of %rcx) it spills just 32-bits of %ecx and fills it
back in, effectively setting u.k to 0.  The w call then takes %rdi, %rsi
arguments it doesn't use and the TImode in %rcx:%rdx pair, but with the high 32
bits of the TImode value lost.  The reason for the spill is clear, the shift
instruction needs that register...

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

* [Bug rtl-optimization/102627] [11/12 Regression] wrong code with "-O1"
  2021-10-06  8:37 [Bug tree-optimization/102627] New: wrong code with "-O1" suochenyao at 163 dot com
                   ` (2 preceding siblings ...)
  2021-10-06 10:42 ` jakub at gcc dot gnu.org
@ 2021-10-07 19:54 ` vmakarov at gcc dot gnu.org
  2021-10-08 16:54 ` cvs-commit at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: vmakarov at gcc dot gnu.org @ 2021-10-07 19:54 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Vladimir Makarov <vmakarov at gcc dot gnu.org> ---
(In reply to Jakub Jelinek from comment #3)
> The assembly difference r11-8007 to r11-8008 is:
> --- pr102627.s	2021-10-06 06:32:46.000000000 -0400
> +++ pr102627.s	2021-10-06 06:33:00.000000000 -0400
> @@ -77,10 +77,10 @@ main:
>  	movq	%rdx, %rcx
>  	movq	%rax, %rdx
>  	movq	e(%rip), %rax
> -	movq	%rcx, 8(%rsp)
> +	movl	%ecx, 12(%rsp)
>  	movzbl	f(%rip), %ecx
>  	salq	%cl, %rax
> -	movq	8(%rsp), %rcx
> +	movl	12(%rsp), %ecx
>  	movq	%rax, %rsi
>  	movl	$0, %edi
>  	call	w
> I believe y returns the 128-bit struct g return value in %rdx:%rax pair,
> right before the above instructions, and the above change means that instead
> of spilling the whole 64-bits of %rcx that holds at that point u.j and u.k
> members (u.k in the upper 32 bits of %rcx) it spills just 32-bits of %ecx
> and fills it back in, effectively setting u.k to 0.  The w call then takes
> %rdi, %rsi arguments it doesn't use and the TImode in %rcx:%rdx pair, but
> with the high 32 bits of the TImode value lost.  The reason for the spill is
> clear, the shift instruction needs that register...

Jakub, thank you for the analysis.  I believe the patch in question just
triggered a bug in hard reg live range splitting.

I am working on the PR.  I hope to fix it on this week or at begining of the
next week.

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

* [Bug rtl-optimization/102627] [11/12 Regression] wrong code with "-O1"
  2021-10-06  8:37 [Bug tree-optimization/102627] New: wrong code with "-O1" suochenyao at 163 dot com
                   ` (3 preceding siblings ...)
  2021-10-07 19:54 ` vmakarov at gcc dot gnu.org
@ 2021-10-08 16:54 ` cvs-commit at gcc dot gnu.org
  2021-10-11  8:32 ` [Bug rtl-optimization/102627] [11 " jakub at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-10-08 16:54 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Vladimir Makarov <vmakarov@gcc.gnu.org>:

https://gcc.gnu.org/g:9046e0d46fc285e5c59c87182d48c8de0f7f929c

commit r12-4256-g9046e0d46fc285e5c59c87182d48c8de0f7f929c
Author: Vladimir N. Makarov <vmakarov@redhat.com>
Date:   Fri Oct 8 10:16:09 2021 -0400

    [PR102627] Use at least natural mode during splitting hard reg live range

    In the PR test case SImode was used to split live range of cx on x86-64
    because it was the biggest mode for this hard reg in the function.  But
    all 64-bits of cx contain structure members.  We need always to use at
least
    natural mode of hard reg in splitting to fix this problem.

    gcc/ChangeLog:

            PR rtl-optimization/102627
            * lra-constraints.c (split_reg): Use at least natural mode of hard
reg.

    gcc/testsuite/ChangeLog:

            PR rtl-optimization/102627
            * gcc.target/i386/pr102627.c: New test.

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

* [Bug rtl-optimization/102627] [11 Regression] wrong code with "-O1"
  2021-10-06  8:37 [Bug tree-optimization/102627] New: wrong code with "-O1" suochenyao at 163 dot com
                   ` (4 preceding siblings ...)
  2021-10-08 16:54 ` cvs-commit at gcc dot gnu.org
@ 2021-10-11  8:32 ` jakub at gcc dot gnu.org
  2021-10-14 16:11 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-10-11  8:32 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |vmakarov at gcc dot gnu.org
            Summary|[11/12 Regression] wrong    |[11 Regression] wrong code
                   |code with "-O1"             |with "-O1"
             Status|NEW                         |ASSIGNED

--- Comment #6 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Fixed on the trunk so far.

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

* [Bug rtl-optimization/102627] [11 Regression] wrong code with "-O1"
  2021-10-06  8:37 [Bug tree-optimization/102627] New: wrong code with "-O1" suochenyao at 163 dot com
                   ` (5 preceding siblings ...)
  2021-10-11  8:32 ` [Bug rtl-optimization/102627] [11 " jakub at gcc dot gnu.org
@ 2021-10-14 16:11 ` cvs-commit at gcc dot gnu.org
  2021-10-14 16:15 ` vmakarov at gcc dot gnu.org
  2021-10-22 20:11 ` hjl.tools at gmail dot com
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-10-14 16:11 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-11 branch has been updated by Vladimir Makarov
<vmakarov@gcc.gnu.org>:

https://gcc.gnu.org/g:99d21577f8a00196f3133fe1066de6e3e7d180c1

commit r11-9154-g99d21577f8a00196f3133fe1066de6e3e7d180c1
Author: Vladimir N. Makarov <vmakarov@redhat.com>
Date:   Fri Oct 8 10:16:09 2021 -0400

    [PR102627] Use at least natural mode during splitting hard reg live range

    In the PR test case SImode was used to split live range of cx on x86-64
    because it was the biggest mode for this hard reg in the function.  But
    all 64-bits of cx contain structure members.  We need always to use at
least
    natural mode of hard reg in splitting to fix this problem.

    gcc/ChangeLog:

            PR rtl-optimization/102627
            * lra-constraints.c (split_reg): Use at least natural mode of hard
reg.

    gcc/testsuite/ChangeLog:

            PR rtl-optimization/102627
            * gcc.target/i386/pr102627.c: New test.

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

* [Bug rtl-optimization/102627] [11 Regression] wrong code with "-O1"
  2021-10-06  8:37 [Bug tree-optimization/102627] New: wrong code with "-O1" suochenyao at 163 dot com
                   ` (6 preceding siblings ...)
  2021-10-14 16:11 ` cvs-commit at gcc dot gnu.org
@ 2021-10-14 16:15 ` vmakarov at gcc dot gnu.org
  2021-10-22 20:11 ` hjl.tools at gmail dot com
  8 siblings, 0 replies; 10+ messages in thread
From: vmakarov at gcc dot gnu.org @ 2021-10-14 16:15 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from Vladimir Makarov <vmakarov at gcc dot gnu.org> ---
I've committed the patch to gcc-11 branch too after nobody made complaints
about the patch in the trunk.  I've also successfully tested and bootstrapped
the patch on the branch too.

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

* [Bug rtl-optimization/102627] [11 Regression] wrong code with "-O1"
  2021-10-06  8:37 [Bug tree-optimization/102627] New: wrong code with "-O1" suochenyao at 163 dot com
                   ` (7 preceding siblings ...)
  2021-10-14 16:15 ` vmakarov at gcc dot gnu.org
@ 2021-10-22 20:11 ` hjl.tools at gmail dot com
  8 siblings, 0 replies; 10+ messages in thread
From: hjl.tools at gmail dot com @ 2021-10-22 20:11 UTC (permalink / raw)
  To: gcc-bugs

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

H.J. Lu <hjl.tools at gmail dot com> changed:

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

--- Comment #9 from H.J. Lu <hjl.tools at gmail dot com> ---
Fixed for GCC 12 and 11.3.

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

end of thread, other threads:[~2021-10-22 20:11 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-06  8:37 [Bug tree-optimization/102627] New: wrong code with "-O1" suochenyao at 163 dot com
2021-10-06 10:13 ` [Bug tree-optimization/102627] " rguenth at gcc dot gnu.org
2021-10-06 10:32 ` [Bug rtl-optimization/102627] [11/12 Regression] " jakub at gcc dot gnu.org
2021-10-06 10:42 ` jakub at gcc dot gnu.org
2021-10-07 19:54 ` vmakarov at gcc dot gnu.org
2021-10-08 16:54 ` cvs-commit at gcc dot gnu.org
2021-10-11  8:32 ` [Bug rtl-optimization/102627] [11 " jakub at gcc dot gnu.org
2021-10-14 16:11 ` cvs-commit at gcc dot gnu.org
2021-10-14 16:15 ` vmakarov at gcc dot gnu.org
2021-10-22 20:11 ` hjl.tools at gmail dot com

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).