简体   繁体   English

布尔AND的C尾调用优化

[英]C tail call optimization for boolean AND

Does GCC perform Tail call optimization on the following function? GCC是否对以下功能执行尾部调用优化?

bool isEqual(Node *head1, Node *head2)
{
    if(head1 == NULL || head2 == NULL)
        return head1 == NULL && head2 == NULL;
    return head1->data == head2->data && isEqual(head1->next, head2->next);
}

Yes it does, at least the way I compiled it. 是的,至少在编译方式上确实如此。 The assembly code for that function was 该函数的汇编代码为

isEqual(Node*, Node*):
    test    rdi, rdi
    sete    dl
    test    rsi, rsi
    sete    cl
    mov eax, ecx
    or  al, dl
    jne .L2
    mov edx, DWORD PTR [rsi+8]
    cmp DWORD PTR [rdi+8], edx
    je  .L5
    rep ret
.L2:
    mov eax, ecx
    and eax, edx
    ret
.L5:
    mov rdi, QWORD PTR [rdi]
    mov rsi, QWORD PTR [rsi]
    test    rdi, rdi
    sete    dl
    test    rsi, rsi
    sete    cl
    mov eax, ecx
    or  al, dl
    jne .L2
    mov ecx, DWORD PTR [rsi+8]
    cmp DWORD PTR [rdi+8], ecx
    je  .L5
    rep ret

That's clearly a loop, and no nested calls appear. 显然这是一个循环,并且没有嵌套调用出现。

You should verify whether it does something similar with your version of GCC on your platform and with your compilation options. 您应该验证它是否与您平台上的GCC版本以及您的编译选项有相似之处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM