https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77276
Bug ID: 77276 Summary: strlen expanded inline with -Os, emits larger code than with -O2 Product: gcc Version: 7.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: target Assignee: unassigned at gcc dot gnu.org Reporter: msebor at gcc dot gnu.org Target Milestone: --- On x86_64 (but not on powerpc64le, for example), a call to __builtin_strlen with a non-constant argument is expanded inline at -Os (as well as -Og and -O1), but not at -O2 even though the size of the code emitted at -O2 is less than that at -Os. $ (set -x && cat xyz.c && for o in s 2; do /build/gcc-trunk-svn/gcc/xgcc -B /build/gcc-trunk-svn/gcc -O$o -Wall -Wextra -c xyz.c && objdump -d xyz.o; done) + cat xyz.c static const char* volatile s = "123"; unsigned f (void) { return __builtin_strlen (s); } + for o in s 2 + /build/gcc-trunk-svn/gcc/xgcc -B /build/gcc-trunk-svn/gcc -Os -Wall -Wextra -c xyz.c + objdump -d xyz.o xyz.o: file format elf64-x86-64 Disassembly of section .text: 0000000000000000 <f>: 0: 48 8b 3d 00 00 00 00 mov 0x0(%rip),%rdi # 7 <f+0x7> 7: 31 c0 xor %eax,%eax 9: 48 83 c9 ff or $0xffffffffffffffff,%rcx d: f2 ae repnz scas %es:(%rdi),%al f: 48 89 c8 mov %rcx,%rax 12: 48 f7 d0 not %rax 15: 48 ff c8 dec %rax 18: c3 retq + for o in s 2 + /build/gcc-trunk-svn/gcc/xgcc -B /build/gcc-trunk-svn/gcc -O2 -Wall -Wextra -c xyz.c + objdump -d xyz.o xyz.o: file format elf64-x86-64 Disassembly of section .text: 0000000000000000 <f>: 0: 48 83 ec 08 sub $0x8,%rsp 4: 48 8b 3d 00 00 00 00 mov 0x0(%rip),%rdi # b <f+0xb> b: e8 00 00 00 00 callq 10 <f+0x10> 10: 48 83 c4 08 add $0x8,%rsp 14: c3 retq