Re: How to turn any possible optimization during compilation?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 5/3/21 6:31 PM, Peng Yu via Gcc-help wrote:
In the following example, printf() is converted to puts(). What are
the options to turn off any possible optimization during compilation
and just let gcc literally translate the C code to the assembly code?

Note that clang does not perform such an optimization. Should gcc do
the same to turn off any optimization by default? Thanks.

Someone already answered the first question so just to address
the second: IMO, since these are optimizing transformations, without
optimization GCC should avoid introducing calls to library functions
that aren't in the source code.  The possible exceptions are calling
memcpy/memmove/memset to copy or initialize large aggregates, and
runtime library calls.

Martin


$ gcc -S -x c -o - - <<< '#include <stdio.h>'$'\n''int main() {
puts("Hello World1!"); printf("Hello World2!\n"); }'
     .file    ""
     .text
     .section    .rodata
.LC0:
     .string    "Hello World1!"
.LC1:
     .string    "Hello World2!"
     .text
     .globl    main
     .type    main, @function
main:
.LFB0:
     .cfi_startproc
     pushq    %rbp
     .cfi_def_cfa_offset 16
     .cfi_offset 6, -16
     movq    %rsp, %rbp
     .cfi_def_cfa_register 6
     leaq    .LC0(%rip), %rdi
     call    puts@PLT
     leaq    .LC1(%rip), %rdi
     call    puts@PLT
     movl    $0, %eax
     popq    %rbp
     .cfi_def_cfa 7, 8
     ret
     .cfi_endproc
.LFE0:
     .size    main, .-main
     .ident    "GCC: (Debian 10.2.1-6) 10.2.1 20210110"
     .section    .note.GNU-stack,"",@progbits

$ clang -S -x c -o - - <<< '#include <stdio.h>'$'\n''int main() {
puts("Hello World1!"); printf("Hello World2!\n"); }'
     .text
     .file    "-"
     .globl    main                            # -- Begin function main
     .p2align    4, 0x90
     .type    main,@function
main:                                   # @main
     .cfi_startproc
# %bb.0:
     pushq    %rbp
     .cfi_def_cfa_offset 16
     .cfi_offset %rbp, -16
     movq    %rsp, %rbp
     .cfi_def_cfa_register %rbp
     subq    $16, %rsp
     movabsq    $.L.str, %rdi
     callq    puts
     movabsq    $.L.str.1, %rdi
     movl    %eax, -4(%rbp)                  # 4-byte Spill
     movb    $0, %al
     callq    printf
     xorl    %ecx, %ecx
     movl    %eax, -8(%rbp)                  # 4-byte Spill
     movl    %ecx, %eax
     addq    $16, %rsp
     popq    %rbp
     .cfi_def_cfa %rsp, 8
     retq
.Lfunc_end0:
     .size    main, .Lfunc_end0-main
     .cfi_endproc
                                         # -- End function
     .type    .L.str,@object                  # @.str
     .section    .rodata.str1.1,"aMS",@progbits,1
.L.str:
     .asciz    "Hello World1!"
     .size    .L.str, 14

     .type    .L.str.1,@object                # @.str.1
.L.str.1:
     .asciz    "Hello World2!\n"
     .size    .L.str.1, 15

     .ident    "Debian clang version 11.0.1-2"
     .section    ".note.GNU-stack","",@progbits
     .addrsig
     .addrsig_sym puts
     .addrsig_sym printf





[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux