Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22306

GCC - two identical functions but the code generated differs. Why?

$
0
0

The code:

#define OPPOSITE(c) (*((typeof(x) *)&(x)))int foo(volatile int x){    OPPOSITE(x) = OPPOSITE(x) + OPPOSITE(x);    return x;}int bar(volatile int x){    OPPOSITE(x) = OPPOSITE(x) + OPPOSITE(x);    return x;}

The result (-Os):

foo:        mov     DWORD PTR [rsp-4], edi        mov     eax, DWORD PTR [rsp-4]        mov     edx, DWORD PTR [rsp-4]        add     eax, edx        mov     DWORD PTR [rsp-4], eax        mov     eax, DWORD PTR [rsp-4]        retbar:        mov     DWORD PTR [rsp-4], edi        mov     eax, DWORD PTR [rsp-4]        add     eax, eax        ret

or ARM gcc. (-O3)

foo:        sub     sp, sp, #8        str     r0, [sp, #4]        ldr     r3, [sp, #4]        ldr     r2, [sp, #4]        add     r3, r3, r2        str     r3, [sp, #4]        ldr     r0, [sp, #4]        add     sp, sp, #8        bx      lrbar:        sub     sp, sp, #8        str     r0, [sp, #4]        ldr     r0, [sp, #4]        lsl     r0, r0, #1        add     sp, sp, #8        bx      lr

https://godbolt.org/z/6z5Td9GsP


Viewing all articles
Browse latest Browse all 22306

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>