[Bug c++/55081] New: [4.8 regression?] Non-optimized static array elements initialization
zhroma at ispras dot ru
gcc-bugzilla@gcc.gnu.org
Fri Oct 26 13:14:00 GMT 2012
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.
More information about the Gcc-bugs
mailing list