What does “sibling calls” mean?

后端 未结 3 1562
独厮守ぢ
独厮守ぢ 2021-02-05 04:49

On GCC manual,

-foptimize-sibling-calls

Optimize sibling and tail recursive calls.

I kn

3条回答
  •  失恋的感觉
    2021-02-05 05:22

    Tail Calls

    If a function call is a last action performed in another function, it is said to be a tail call.

    The name stems from the fact that the function call appears at the tail position of other function.

    int foo(int a, int b) {
        // some code ...
        return bar(b);    // Tail call which is neither sibling call nor tail recursive call.
    }
    

    bar appears at the tail position of foo. Call to bar is a tail call.


    Tail Recursive Calls

    Tail recursive call is a special case of tail call where callee function is same as caller function.

    int foo(int a, int b) {
        if (a > 0) {
            return foo(a - 1, b + 1);    // Tail recursive call
        } else {
            return b;
        }
    }
    

    Sibling Calls

    Sibling call is another special case of tail call where caller function and callee function do not need to be same, but they have compatible stack footprint.

    That means the return types of both functions must be same, and the arguments being passed must take same stack space.

    int foo(int a, int b) {
        // some code ...
        return bar(a - 1, b);    // Sibling call, also a tail call, but not a tail recursive call.
    }
    

    Every tail recursive call is a sibling call, Since the definition implies every function is a sibling of itself.


    Why Distinction?

    Because of identical stack footprint, replacing stack frame becomes relatively easier. Compiler writers don't have to resize stack frame, and in place mutation becomes straightforward.

提交回复
热议问题