public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug rtl-optimization/36321]  New: Optimization higher or eqaul to -O2 produce wrong code
@ 2008-05-24 15:15 ktietz at gcc dot gnu dot org
  2008-05-24 16:04 ` [Bug rtl-optimization/36321] " ubizjak at gmail dot com
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: ktietz at gcc dot gnu dot org @ 2008-05-24 15:15 UTC (permalink / raw)
  To: gcc-bugs

For the i386 stack probing feature there is a non-standard argument register
%eax used for internal __chkstk call. If the code is translate with gcc with
optimization level one or less, code is fine. For -O2 and higher, the argument
%eax for __chkstk is optimized out.

The following source is a short test-case for this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>

void foo(char *str)
{
  int len = strlen ("ABCDE");
  int len2 = len + strlen (str);
  char *a = (char *) alloca(len);
  char *b = (char *) alloca(len2*3);

  memset (a,'x',len-1); a[len-1]=0;
  memset (b,'y',len2*3-1); b [len2*3-1]=0;
  printf ("%s %s\n", a, b);
}

For optimization -O2 the following assembler is produced (I mark the bad code
by  !):

        .file   ""
        .section .rdata,"dr"
LC0:
        .ascii "%s %s\12\0"
        .text
        .p2align 4,,15
.globl _foo
        .def    _foo;   .scl    2;      .type   32;     .endef
_foo:
        pushq   %rbp
        movq    %rsp, %rbp
        movq    %rsi, -16(%rbp)
        movq    %rdi, -8(%rbp)
        movq    %rbx, -24(%rbp)
        subq    $64, %rsp
        call    _strlen
        subq    $32, %rsp
!       leal    15(%rax,%rax,2), %ecx
        leaq    32(%rsp), %rsi
        movslq  %ecx,%rdi
        call    ___chkstk
        movl    $121, %edx
        leaq    32(%rsp), %rbx
        leal    -1(%rcx), %eax
        movl    $2021161080, (%rsi)
        movb    $0, 4(%rsi)
        movq    %rbx, %rcx
        movslq  %eax,%r8
        call    _memset
        leaq    LC0(%rip), %rcx
        movq    %rbx, %r8
        movq    %rsi, %rdx
        movb    $0, -1(%rbx,%rdi)
        call    _printf
        movq    -24(%rbp), %rbx
        movq    -16(%rbp), %rsi
        movq    -8(%rbp), %rdi
        leave
        ret
        .def    _strlen;        .scl    2;      .type   32;     .endef
        .def    _memset;        .scl    2;      .type   32;     .endef
        .def    _printf;        .scl    2;      .type   32;     .endef

For optimization -O1 correct assembly is produced:
        .file   ""
        .section .rdata,"dr"
LC0:
        .ascii "%s %s\12\0"
        .text
.globl _foo
        .def    _foo;   .scl    2;      .type   32;     .endef
_foo:
        pushq   %rbp
        movq    %rsp, %rbp
        movq    %rbx, -24(%rbp)
        movq    %rsi, -16(%rbp)
        movq    %rdi, -8(%rbp)
        subq    $96, %rsp
        movq    %rcx, %rdi
        movl    $0, %eax
        movq    $-1, %rcx
        repne scasb
        notq    %rcx
        leaq    32(%rsp), %rsi
        leal    12(%rcx,%rcx,2), %ecx
        movslq  %ecx,%rdi
        leaq    30(%rdi), %rax
        andq    $-16, %rax
        call    ___chkstk
        leaq    32(%rsp), %rbx
        movl    $2021161080, (%rsi)
        movb    $0, 4(%rsi)
        subl    $1, %ecx
        movslq  %ecx,%r8
        movl    $121, %edx
        movq    %rbx, %rcx
        call    _memset
        movb    $0, -1(%rbx,%rdi)
        movq    %rbx, %r8
        movq    %rsi, %rdx
        leaq    LC0(%rip), %rcx
        call    _printf
        movq    -24(%rbp), %rbx
        movq    -16(%rbp), %rsi
        movq    -8(%rbp), %rdi
        leave
        ret
        .def    _memset;        .scl    2;      .type   32;     .endef
        .def    _printf;        .scl    2;      .type   32;     .endef


-- 
           Summary: Optimization higher or eqaul to -O2 produce wrong code
           Product: gcc
           Version: 4.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: ktietz at gcc dot gnu dot org
GCC target triplet: 86_64-pc-mingw32


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36321


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

* [Bug rtl-optimization/36321] Optimization higher or eqaul to -O2 produce wrong code
  2008-05-24 15:15 [Bug rtl-optimization/36321] New: Optimization higher or eqaul to -O2 produce wrong code ktietz at gcc dot gnu dot org
@ 2008-05-24 16:04 ` ubizjak at gmail dot com
  2008-05-24 16:37 ` ktietz at gcc dot gnu dot org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: ubizjak at gmail dot com @ 2008-05-24 16:04 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from ubizjak at gmail dot com  2008-05-24 16:03 -------
Can you try this patch:

Index: i386.md
===================================================================
--- i386.md     (revision 135849)
+++ i386.md     (working copy)
@@ -19707,7 +19707,7 @@
    (set_attr "length" "5")])

 (define_insn "allocate_stack_worker_64"
-  [(set (match_operand:DI 0 "register_operand" "=a")
+  [(set (match_operand:DI 0 "register_operand" "+a")
        (unspec_volatile:DI [(match_dup 0)] UNSPECV_STACK_PROBE))
    (set (reg:DI SP_REG) (minus:DI (reg:DI SP_REG) (match_dup 0)))
    (clobber (reg:DI R10_REG))


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36321


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

* [Bug rtl-optimization/36321] Optimization higher or eqaul to -O2 produce wrong code
  2008-05-24 15:15 [Bug rtl-optimization/36321] New: Optimization higher or eqaul to -O2 produce wrong code ktietz at gcc dot gnu dot org
  2008-05-24 16:04 ` [Bug rtl-optimization/36321] " ubizjak at gmail dot com
@ 2008-05-24 16:37 ` ktietz at gcc dot gnu dot org
  2008-05-24 17:02 ` ktietz at gcc dot gnu dot org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: ktietz at gcc dot gnu dot org @ 2008-05-24 16:37 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from ktietz at gcc dot gnu dot org  2008-05-24 16:36 -------
(In reply to comment #1)
> Can you try this patch:
> 
> Index: i386.md
> ===================================================================
> --- i386.md     (revision 135849)
> +++ i386.md     (working copy)
> @@ -19707,7 +19707,7 @@
>     (set_attr "length" "5")])
> 
>  (define_insn "allocate_stack_worker_64"
> -  [(set (match_operand:DI 0 "register_operand" "=a")
> +  [(set (match_operand:DI 0 "register_operand" "+a")
>         (unspec_volatile:DI [(match_dup 0)] UNSPECV_STACK_PROBE))
>     (set (reg:DI SP_REG) (minus:DI (reg:DI SP_REG) (match_dup 0)))
>     (clobber (reg:DI R10_REG))
> 

I tried it. There is no difference.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36321


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

* [Bug rtl-optimization/36321] Optimization higher or eqaul to -O2 produce wrong code
  2008-05-24 15:15 [Bug rtl-optimization/36321] New: Optimization higher or eqaul to -O2 produce wrong code ktietz at gcc dot gnu dot org
  2008-05-24 16:04 ` [Bug rtl-optimization/36321] " ubizjak at gmail dot com
  2008-05-24 16:37 ` ktietz at gcc dot gnu dot org
@ 2008-05-24 17:02 ` ktietz at gcc dot gnu dot org
  2008-05-24 17:55 ` ubizjak at gmail dot com
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: ktietz at gcc dot gnu dot org @ 2008-05-24 17:02 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from ktietz at gcc dot gnu dot org  2008-05-24 17:01 -------
(In reply to comment #1)

Sorry, I missed to install the new compiler :}

Yes, it works, assembler looks now fine.
Thanks, Kai

        .file   ""
        .section .rdata,"dr"
LC0:
        .ascii "%s %s\12\0"
        .text
        .p2align 4,,15
.globl _foo
        .def    _foo;   .scl    2;      .type   32;     .endef
_foo:
        pushq   %rbp
        movq    %rsp, %rbp
        movq    %rsi, -16(%rbp)
        movq    %rdi, -8(%rbp)
        movq    %rbx, -24(%rbp)
        subq    $64, %rsp
        call    _strlen
        leal    15(%rax,%rax,2), %ecx
        subq    $32, %rsp
        leaq    32(%rsp), %rsi
        movslq  %ecx,%rdi
        leaq    30(%rdi), %rdx
        andq    $-16, %rdx
        movq    %rdx, %rax
        call    ___chkstk
        movl    $121, %edx
        leaq    32(%rsp), %rbx
        leal    -1(%rcx), %eax
        movl    $2021161080, (%rsi)
        movb    $0, 4(%rsi)
        movq    %rbx, %rcx
        movslq  %eax,%r8
        call    _memset
        leaq    LC0(%rip), %rcx
        movq    %rbx, %r8
        movq    %rsi, %rdx
        movb    $0, -1(%rbx,%rdi)
        call    _printf
        movq    -24(%rbp), %rbx
        movq    -16(%rbp), %rsi
        movq    -8(%rbp), %rdi
        leave
        ret
        .def    _strlen;        .scl    2;      .type   32;     .endef
        .def    _memset;        .scl    2;      .type   32;     .endef
        .def    _printf;        .scl    2;      .type   32;     .endef


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36321


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

* [Bug rtl-optimization/36321] Optimization higher or eqaul to -O2 produce wrong code
  2008-05-24 15:15 [Bug rtl-optimization/36321] New: Optimization higher or eqaul to -O2 produce wrong code ktietz at gcc dot gnu dot org
                   ` (2 preceding siblings ...)
  2008-05-24 17:02 ` ktietz at gcc dot gnu dot org
@ 2008-05-24 17:55 ` ubizjak at gmail dot com
  2008-05-25  6:54 ` [Bug rtl-optimization/36321] Optimization higher or equal " nightstrike at gmail dot com
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: ubizjak at gmail dot com @ 2008-05-24 17:55 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from ubizjak at gmail dot com  2008-05-24 17:54 -------
(In reply to comment #3)

> Sorry, I missed to install the new compiler :}
> Yes, it works, assembler looks now fine.

Can you please regtest the patch on your target?

BTW: Is this a regression for 4.3 and 4.4?


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36321


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

* [Bug rtl-optimization/36321] Optimization higher or equal to -O2 produce wrong code
  2008-05-24 15:15 [Bug rtl-optimization/36321] New: Optimization higher or eqaul to -O2 produce wrong code ktietz at gcc dot gnu dot org
                   ` (3 preceding siblings ...)
  2008-05-24 17:55 ` ubizjak at gmail dot com
@ 2008-05-25  6:54 ` nightstrike at gmail dot com
  2008-05-26 13:30 ` rguenth at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: nightstrike at gmail dot com @ 2008-05-25  6:54 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from nightstrike at gmail dot com  2008-05-25 06:54 -------
I am regtesting it now.  Note that it will take a very long time to complete
(several days).

This does apply to 4.3 and 4.4


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36321


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

* [Bug rtl-optimization/36321] Optimization higher or equal to -O2 produce wrong code
  2008-05-24 15:15 [Bug rtl-optimization/36321] New: Optimization higher or eqaul to -O2 produce wrong code ktietz at gcc dot gnu dot org
                   ` (4 preceding siblings ...)
  2008-05-25  6:54 ` [Bug rtl-optimization/36321] Optimization higher or equal " nightstrike at gmail dot com
@ 2008-05-26 13:30 ` rguenth at gcc dot gnu dot org
  2008-05-26 13:31 ` [Bug target/36321] " rguenth at gcc dot gnu dot org
  2008-11-14 23:11 ` dj at redhat dot com
  7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2008-05-26 13:30 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from rguenth at gcc dot gnu dot org  2008-05-26 13:29 -------
Subject: Bug 36321

Author: rguenth
Date: Mon May 26 13:28:52 2008
New Revision: 135922

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=135922
Log:
2008-05-26  Kai Tietz  <kai.tietz@onevision.com>

        PR target/36321
        * config/i386/i386.md (allocate_stack_worker_64): Make sure 
        argument operand in rax isn't removed.

        * gcc-c.torture/execute/pr36321.c: New.


Added:
    branches/gcc-4_3-branch/gcc/testsuite/gcc.c-torture/execute/pr36321.c
Modified:
    branches/gcc-4_3-branch/gcc/ChangeLog
    branches/gcc-4_3-branch/gcc/config/i386/i386.md
    branches/gcc-4_3-branch/gcc/testsuite/ChangeLog


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36321


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

* [Bug target/36321] Optimization higher or equal to -O2 produce wrong code
  2008-05-24 15:15 [Bug rtl-optimization/36321] New: Optimization higher or eqaul to -O2 produce wrong code ktietz at gcc dot gnu dot org
                   ` (5 preceding siblings ...)
  2008-05-26 13:30 ` rguenth at gcc dot gnu dot org
@ 2008-05-26 13:31 ` rguenth at gcc dot gnu dot org
  2008-11-14 23:11 ` dj at redhat dot com
  7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2008-05-26 13:31 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from rguenth at gcc dot gnu dot org  2008-05-26 13:30 -------
Fixed.


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
          Component|rtl-optimization            |target
           Keywords|                            |wrong-code
      Known to fail|                            |4.3.0
      Known to work|                            |4.3.1
         Resolution|                            |FIXED
   Target Milestone|---                         |4.3.1


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36321


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

* [Bug target/36321] Optimization higher or equal to -O2 produce wrong code
  2008-05-24 15:15 [Bug rtl-optimization/36321] New: Optimization higher or eqaul to -O2 produce wrong code ktietz at gcc dot gnu dot org
                   ` (6 preceding siblings ...)
  2008-05-26 13:31 ` [Bug target/36321] " rguenth at gcc dot gnu dot org
@ 2008-11-14 23:11 ` dj at redhat dot com
  7 siblings, 0 replies; 9+ messages in thread
From: dj at redhat dot com @ 2008-11-14 23:11 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #8 from dj at redhat dot com  2008-11-14 23:09 -------
The test case segfaults on simulators that don't initialize argv[0] to the name
of the running program.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36321


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

end of thread, other threads:[~2008-11-14 23:11 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-05-24 15:15 [Bug rtl-optimization/36321] New: Optimization higher or eqaul to -O2 produce wrong code ktietz at gcc dot gnu dot org
2008-05-24 16:04 ` [Bug rtl-optimization/36321] " ubizjak at gmail dot com
2008-05-24 16:37 ` ktietz at gcc dot gnu dot org
2008-05-24 17:02 ` ktietz at gcc dot gnu dot org
2008-05-24 17:55 ` ubizjak at gmail dot com
2008-05-25  6:54 ` [Bug rtl-optimization/36321] Optimization higher or equal " nightstrike at gmail dot com
2008-05-26 13:30 ` rguenth at gcc dot gnu dot org
2008-05-26 13:31 ` [Bug target/36321] " rguenth at gcc dot gnu dot org
2008-11-14 23:11 ` dj at redhat 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).