https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108626
Bug ID: 108626
Summary: GCC doesn't combine string literals for const
char*const and const char b[]
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: marat at slonopotamus dot org
Target Milestone: ---
Given the following code
----
#include <stdio.h>
static const char* const a = "bla";
static const char b[] = "bla";
int main() {
puts(a);
puts(b);
}
----
GCC 12.2 with -O2 produces
----
.LC0:
.string "bla"
main:
sub rsp, 8
mov edi, OFFSET FLAT:.LC0
call puts
mov edi, OFFSET FLAT:_ZL1b
call puts
xor eax, eax
add rsp, 8
ret
_ZL1b:
.string "bla"
----
On the other hand, clang 15.0 with -O1 and higher produces
----
main: # @main
push rbx
lea rbx, [rip + .L.str]
mov rdi, rbx
call puts@PLT
mov rdi, rbx
call puts@PLT
xor eax, eax
pop rbx
ret
.L.str:
.asciz "bla"
----
For some reason, GCC doesn't want to combine readonly data for these two string
literals.
Godbolt playground: https://godbolt.org/z/WaeazezE6