https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120391
Bug ID: 120391 Summary: Enhancement: deduplicate constexpr char[] arrays with identical or overlapping contents Product: gcc Version: 14.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: nickooo314 at gmail dot com Target Milestone: --- In optimized builds, GCC currently does not deduplicate "constexpr char[]" arrays that have identical or overlapping contents, even when using flags like -Os or -fmerge-all-constants. This results in unnecessary duplication in .rodata, especially for embedded or size-constrained targets. Example (no deduplication): $ cat merge_test.cpp #include <stdio.h> constexpr char c1[] = {'H','e','l','l','o',' ','w','o','r','l','d','!','\0'}; constexpr char c2[] = {'w','o','r','l','d','!','\0'}; int main() { puts(c1); puts(c2); } $ /test/bin/g++ -Os -fmerge-all-constants -o merge_test merge_test.cpp $ objdump -s -j .rodata merge_test merge_test: file format elf64-x86-64 Contents of section .rodata: 402000 01000200 00000000 776f726c 64210a00 ........world!.. 402010 48656c6c 6f20776f 726c6421 0a00 Hello world!.. Even though c2 is a suffix of c1, GCC emits both arrays independently in .rodata, as opposed to string literals which get optimized (in fact even without "-fmerge-all-constants"): constexpr auto c1 = "Hello world!"; constexpr auto c2 = "world!"; $ objdump -s -j .rodata merge_test merge_test: file format elf64-x86-64 Contents of section .rodata: 402000 01000200 48656c6c 6f20776f 726c6421 ....Hello world! 402010 00 Is there any reason GCC doesn't perform this optimization even with "-fmerge-all-constants" for constexpr char arrays?