views:

340

answers:

2

Following along from this question: how-do-i-check-if-gcc-is-performing-tail-recursion-optimization, I noticed that using gcc with -fPIC seems to destroy this optimization. I am creating a shared library, but I doesn't seem to need the -fPIC option.

Well, my question is, why does -fPIC change gcc optimizations ? Do I need to keep -fPIC for any reason ?

+2  A: 

In absence of details such as target architecture and compiler version, a possible explanation is this:

In position-dependent code, the tail-recursion optimization is essentially to reuse the current stack frame, and to replace the considered call by a jump. The syntax may be call function replaced by jmp <small offset of function>.

In position-independent code, the call may be written call function@PLT if the instruction set allows it (this example is amd64). It could perfectly well be replaced by jmp <small offset of function>@PLT, but the two settings do interfere and perhaps gcc developers did not get around to implementing tail-call optimization in the latter mode.

Pascal Cuoq
A: 

In ia32 linux, use of fpic means that you don't have ebx available for general purpose use, which will surely impact optimization. Perhaps the compiler decided against the tail recursion optimization due to register scheduling pressure.

Peeter Joot