public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/55081] New: [4.8 regression?] Non-optimized static array elements initialization
@ 2012-10-26 13:14 zhroma at ispras dot ru
  2012-10-26 14:49 ` [Bug c++/55081] " jakub at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: zhroma at ispras dot ru @ 2012-10-26 13:14 UTC (permalink / raw)
  To: gcc-bugs


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

             Bug #: 55081
           Summary: [4.8 regression?] Non-optimized static array elements
                    initialization
    Classification: Unclassified
           Product: gcc
           Version: 4.8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: zhroma@ispras.ru


Created attachment 28536
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=28536
Preprocessed minimized testcase.

In some cases g++ 4.8 revision 192141 and later generate initialization block
for local static array with constant elements, while earlier g++ versions 
insert constants into assembly data section (gcc-base is rev192140, gcc-peak is
rev192141, tested on 64bit Linux):
$ cat test.cpp 
struct R {
        int field;
};
long* foo() {
        R r;
        static long array[] = {
                sizeof(char),
                (reinterpret_cast<long>(&(r.field)) -
reinterpret_cast<long>(&r))+1,
        };
        return array;
}
$ gcc-base/bin/g++ -O2 test.cpp -S -o 1.s
$ gcc-peak/bin/g++ -O2 test.cpp -S -o 2.s 
$ diff -u 1.s 2.s
--- 1.s
+++ 2.s
@@ -6,8 +6,27 @@
 _Z3foov:
 .LFB0:
     .cfi_startproc
+    cmpb    $0, _ZGVZ3foovE5array(%rip)
+    je    .L11
     movl    $_ZZ3foovE5array, %eax
     ret
+    .p2align 4,,10
+    .p2align 3
+.L11:
+    subq    $8, %rsp
+    .cfi_def_cfa_offset 16
+    movl    $_ZGVZ3foovE5array, %edi
+    call    __cxa_guard_acquire
+    testl    %eax, %eax
+    je    .L3
+    movl    $_ZGVZ3foovE5array, %edi
+    movq    $1, _ZZ3foovE5array(%rip)
+    call    __cxa_guard_release
+.L3:
+    movl    $_ZZ3foovE5array, %eax
+    addq    $8, %rsp
+    .cfi_def_cfa_offset 8
+    ret
     .cfi_endproc
 .LFE0:
     .size    _Z3foov, .-_Z3foov
@@ -16,7 +35,9 @@
     .type    _ZZ3foovE5array, @object
     .size    _ZZ3foovE5array, 16
 _ZZ3foovE5array:
+    .zero    8
     .quad    1
-    .quad    1
+    .local    _ZGVZ3foovE5array
+    .comm    _ZGVZ3foovE5array,8,8
     .ident    "GCC: (GNU) 4.8.0 20121005 (experimental)"
     .section    .note.GNU-stack,"",@progbits

So, the value of array[0] (sizeof(char) equals 1) is generated on the first
function call instead of emitting it to assemlby data section directly. If I
remove second constant element

static long array[] = {
                sizeof(char),
        };

.. or reimplement it in the following way

static long array[] = {
                sizeof(char),
                __builtin_offsetof(R, field)+1,
        };

the problem disappears.

As I understand, the patch rev192141 goal is new warning. Maybe it should not
affect codegen so much?

Additional information.
The problem described above lead to Webkit build failure.
There is the following step while generating assembly for Webkit JavaScriptCore
low-level interpreter: it generates dummy executable containing a function with
static array:

static const unsigned extractorTable[308992] = {
unsigned(-1639711386),
(reinterpret_cast<ptrdiff_t>(&(reinterpret_cast<ArrayProfile*>
(0x4000)-unsigned(267773781),
sizeof(ValueProfile),
// and so on...
};

And later this dummy executable file (its data section) is parsed to find all
these sizeof-and-offset values. This certainly seems strange, but when Webkit
is cross-compiled it helps to find offsets without running anything on target.
After gcc revision 192141 that executable-parsing script fails to get all
sizeof(...) values - they are zeros in gcc-generated assembly data section.


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

* [Bug c++/55081] [4.8 regression?] Non-optimized static array elements initialization
  2012-10-26 13:14 [Bug c++/55081] New: [4.8 regression?] Non-optimized static array elements initialization zhroma at ispras dot ru
@ 2012-10-26 14:49 ` jakub at gcc dot gnu.org
  2012-10-26 15:17 ` amonakov at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: jakub at gcc dot gnu.org @ 2012-10-26 14:49 UTC (permalink / raw)
  To: gcc-bugs


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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org,
                   |                            |jason at gcc dot gnu.org
   Target Milestone|---                         |4.8.0

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> 2012-10-26 14:49:20 UTC ---
Works fine with -std=c++11.
In store_init we have:
  /* In C++0x constant expression is a semantic, not syntactic, property.
     In C++98, make sure that what we thought was a constant expression at
     template definition time is still constant.  */
  if ((cxx_dialect >= cxx0x
       || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
      && (decl_maybe_constant_var_p (decl)
          || TREE_STATIC (decl)))

but here DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) isn't set and
cxx_dialect < cxx0x for -std=c++98.  Not sure what we should do here, either
just call maybe_constant_value or maybe_constant_init otherwise to fold sizeof,
or, if it is really not appropriate for C++98 as an optimization,
fold_sizeof_expr_r recursively plus fold.  Jason, what do you think?


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

* [Bug c++/55081] [4.8 regression?] Non-optimized static array elements initialization
  2012-10-26 13:14 [Bug c++/55081] New: [4.8 regression?] Non-optimized static array elements initialization zhroma at ispras dot ru
  2012-10-26 14:49 ` [Bug c++/55081] " jakub at gcc dot gnu.org
@ 2012-10-26 15:17 ` amonakov at gcc dot gnu.org
  2012-10-26 15:34 ` jason at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: amonakov at gcc dot gnu.org @ 2012-10-26 15:17 UTC (permalink / raw)
  To: gcc-bugs


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

Alexander Monakov <amonakov at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |amonakov at gcc dot gnu.org

--- Comment #2 from Alexander Monakov <amonakov at gcc dot gnu.org> 2012-10-26 15:17:20 UTC ---
Not sure if that's relevant, but the original testcase uses 0x4000 in place of
non-compile-time-constant (&r) like this:

struct R {
        int field;
};
long* foo() {
        static long array[] = {
                sizeof(char),
          (reinterpret_cast<long>(&(reinterpret_cast<struct
R*>(0x4000)->field)) - 0x4000),              
        };
        return array;
}


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

* [Bug c++/55081] [4.8 regression?] Non-optimized static array elements initialization
  2012-10-26 13:14 [Bug c++/55081] New: [4.8 regression?] Non-optimized static array elements initialization zhroma at ispras dot ru
  2012-10-26 14:49 ` [Bug c++/55081] " jakub at gcc dot gnu.org
  2012-10-26 15:17 ` amonakov at gcc dot gnu.org
@ 2012-10-26 15:34 ` jason at gcc dot gnu.org
  2012-10-26 15:59 ` jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: jason at gcc dot gnu.org @ 2012-10-26 15:34 UTC (permalink / raw)
  To: gcc-bugs


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

--- Comment #3 from Jason Merrill <jason at gcc dot gnu.org> 2012-10-26 15:33:45 UTC ---
(In reply to comment #1)
> but here DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) isn't set and
> cxx_dialect < cxx0x for -std=c++98.  Not sure what we should do here, either
> just call maybe_constant_value or maybe_constant_init otherwise to fold sizeof,
> or, if it is really not appropriate for C++98 as an optimization,
> fold_sizeof_expr_r recursively plus fold.  Jason, what do you think?

I think let's just remove the cxx_dialect check here.


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

* [Bug c++/55081] [4.8 regression?] Non-optimized static array elements initialization
  2012-10-26 13:14 [Bug c++/55081] New: [4.8 regression?] Non-optimized static array elements initialization zhroma at ispras dot ru
                   ` (2 preceding siblings ...)
  2012-10-26 15:34 ` jason at gcc dot gnu.org
@ 2012-10-26 15:59 ` jakub at gcc dot gnu.org
  2012-10-26 20:30 ` jakub at gcc dot gnu.org
  2012-10-26 20:32 ` [Bug c++/55081] [4.8 Regression] " jakub at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: jakub at gcc dot gnu.org @ 2012-10-26 15:59 UTC (permalink / raw)
  To: gcc-bugs


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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |ASSIGNED
   Last reconfirmed|                            |2012-10-26
         AssignedTo|unassigned at gcc dot       |jakub at gcc dot gnu.org
                   |gnu.org                     |
     Ever Confirmed|0                           |1

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> 2012-10-26 15:57:55 UTC ---
Created attachment 28538
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=28538
gcc48-pr55081.patch

Untested fix.


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

* [Bug c++/55081] [4.8 regression?] Non-optimized static array elements initialization
  2012-10-26 13:14 [Bug c++/55081] New: [4.8 regression?] Non-optimized static array elements initialization zhroma at ispras dot ru
                   ` (3 preceding siblings ...)
  2012-10-26 15:59 ` jakub at gcc dot gnu.org
@ 2012-10-26 20:30 ` jakub at gcc dot gnu.org
  2012-10-26 20:32 ` [Bug c++/55081] [4.8 Regression] " jakub at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: jakub at gcc dot gnu.org @ 2012-10-26 20:30 UTC (permalink / raw)
  To: gcc-bugs


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

--- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> 2012-10-26 20:30:39 UTC ---
Author: jakub
Date: Fri Oct 26 20:30:35 2012
New Revision: 192862

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=192862
Log:
    PR c++/55081
    * typeck2.c (store_init_value): Call fold_non_dependent_expr
    and maybe_constant_init even for C++98.

    * g++.dg/opt/pr55081.C: New test.

Added:
    trunk/gcc/testsuite/g++.dg/opt/pr55081.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/typeck2.c
    trunk/gcc/testsuite/ChangeLog


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

* [Bug c++/55081] [4.8 Regression] Non-optimized static array elements initialization
  2012-10-26 13:14 [Bug c++/55081] New: [4.8 regression?] Non-optimized static array elements initialization zhroma at ispras dot ru
                   ` (4 preceding siblings ...)
  2012-10-26 20:30 ` jakub at gcc dot gnu.org
@ 2012-10-26 20:32 ` jakub at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: jakub at gcc dot gnu.org @ 2012-10-26 20:32 UTC (permalink / raw)
  To: gcc-bugs


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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED
            Summary|[4.8 regression?]           |[4.8 Regression]
                   |Non-optimized static array  |Non-optimized static array
                   |elements initialization     |elements initialization

--- Comment #6 from Jakub Jelinek <jakub at gcc dot gnu.org> 2012-10-26 20:31:43 UTC ---
Fixed.


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

end of thread, other threads:[~2012-10-26 20:32 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-10-26 13:14 [Bug c++/55081] New: [4.8 regression?] Non-optimized static array elements initialization zhroma at ispras dot ru
2012-10-26 14:49 ` [Bug c++/55081] " jakub at gcc dot gnu.org
2012-10-26 15:17 ` amonakov at gcc dot gnu.org
2012-10-26 15:34 ` jason at gcc dot gnu.org
2012-10-26 15:59 ` jakub at gcc dot gnu.org
2012-10-26 20:30 ` jakub at gcc dot gnu.org
2012-10-26 20:32 ` [Bug c++/55081] [4.8 Regression] " jakub at gcc dot gnu.org

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