Why doesn't the compiler optimize these unattainable instructions?

Given this code:

int x;

int a (int b) {
    b = a (b);
    b += x;
    return b;
}

why GCC returns this output (Intel Syntax): http://goo.gl/8D32F1 - Godbolt GCC Explorer

a:
    sub rsp, 8
    call    a
    mov edx, DWORD PTR x[rip]
    add rsp, 8
    lea eax, [rax+rdx*8]
    add eax, edx
    ret

and Clang return this result (AT & T syntax): http://goo.gl/Zz2rKA - Godbolt Clang Explorer

a:                                      # @a
    pushq   %rax
    callq   a
    addl    x(%rip), %eax
    popq    %rdx
    ret

when is part of the code clearly unreachable? Since the first function operator

b = a (b);

the function will always call itself recursively (until the stack overflows and you get segfault). This means that you will never go beyond this line, and therefore the rest of the code is unreachable. Reachability optimization should theoretically remove code, right?


Both compilers were running on x64 and with the following flags

  • -O3 - maximum optimization
  • -march=native - []
  • -x c - , C

, - ():

GCC ( Intel):

a:
.L1:
    jmp .L1

Clang ( AT & T):

a:
.LBB0_1:
    jmp .LBB0_1

: .


, - , ?


EDIT:

:

:

int j (int x) {
    while (1) {};
    x++;
    return x;
}

GCC : http://goo.gl/CYSUW2

j:
.L2:
    jmp .L2

:

j:                                      # @j
.LBB0_1:                                # =>This Inner Loop Header: Depth=1
    jmp .LBB0_1

:

:

int r (int x) {
    return r (x);
}

GCC : http://goo.gl/eWo2Nb

r:
.L2:
    jmp .L2

Clang : http://goo.gl/CVJKiZ

r:                                      # @r
    ret
+4
1

, , , , . (, , , .) , .

: , . , ; , .

"- " , , , , , . . ; .

+4

Source: https://habr.com/ru/post/1546450/


All Articles