https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85330
Bug ID: 85330 Summary: -Os generates bigger code than -O2 due to disabled strlen pass Product: gcc Version: 8.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: msebor at gcc dot gnu.org Target Milestone: --- When the test case below is compiled with -O2, GCC emits object code that's optimally efficient both in space and runtime. But when the same test case is compiled with -Os it results in much bigger object code that will run far slower than the -O2 equivalent. That's because the strlen optimization doesn't run with -Os. At the same time, it's possible to create test cases involving string functions where the opposite is true (often involving long strings). I wonder if enabling the strlen optimization to a limited extent at -Os (to at least track string lengths without other transformations) would give closer to optimal results. $ cat z.c && gcc -O2 -fdump-tree-optimized=/dev/stdout -c z.c && objdump -d z.o && gcc -Os -fdump-tree-optimized=/dev/stdout -c z.c && objdump -d z.o #define S "012345678" int f (void) { char a[sizeof S]; __builtin_strcpy (a, S); return __builtin_strlen (a); } ;; Function f (f, funcdef_no=0, decl_uid=1958, cgraph_uid=0, symbol_order=0) f () { <bb 2> [local count: 1073741825]: return 9; } z.o: file format elf64-x86-64 Disassembly of section .text: 0000000000000000 <f>: 0: b8 09 00 00 00 mov $0x9,%eax 5: c3 retq ;; Function f (f, funcdef_no=0, decl_uid=1958, cgraph_uid=0, symbol_order=0) f () { char a[10]; long unsigned int _1; int _4; <bb 2> [local count: 1073741825]: __builtin_strcpy (&a, "012345678"); _1 = __builtin_strlen (&a); _4 = (int) _1; a ={v} {CLOBBER}; return _4; } z.o: file format elf64-x86-64 Disassembly of section .text: 0000000000000000 <f>: 0: 48 83 ec 18 sub $0x18,%rsp 4: be 00 00 00 00 mov $0x0,%esi 9: 48 8d 54 24 06 lea 0x6(%rsp),%rdx e: 48 89 d7 mov %rdx,%rdi 11: e8 00 00 00 00 callq 16 <f+0x16> 16: 48 83 c9 ff or $0xffffffffffffffff,%rcx 1a: 48 89 c2 mov %rax,%rdx 1d: 31 c0 xor %eax,%eax 1f: 48 89 d7 mov %rdx,%rdi 22: f2 ae repnz scas %es:(%rdi),%al 24: 48 83 c4 18 add $0x18,%rsp 28: 48 89 c8 mov %rcx,%rax 2b: 48 f7 d0 not %rax 2e: 48 ff c8 dec %rax 31: c3 retq