Difference between clang and gcc [closed]

喜你入骨 提交于 2020-01-01 01:48:33

问题


I used both of these compilers in different projects.

How they are different in terms of code processing and output generations? For example both gcc and clang has -O2 options for optimization. Are they operating the same way (high level) in terms of optimizing code? I did a little test , for example if I have the following code:

int foo(int num) {
    if(num % 2 == 1)
        return num * num;
    else
        return num * num +1;  
}

the following are the output assemblies with clang and gcc with -O2:

----gcc 5.3.0-----                              ----clang 3.8.0----
foo(int):                                       foo(int):
        movl    %edi, %edx                              movl    %edi, %eax
        shrl    $31, %edx                               shrl    $31, %eax
        leal    (%rdi,%rdx), %eax                       addl    %edi, %eax
        andl    $1, %eax                                andl    $-2, %eax
        subl    %edx, %eax                              movl    %edi, %ecx
        cmpl    $1, %eax                                subl    %eax, %ecx
        je      .L5                                     imull   %edi, %edi
        imull   %edi, %edi                              cmpl    $1, %ecx
        leal    1(%rdi), %eax                           setne   %al
        ret                                             movzbl  %al, %eax
.L5:                                                    addl    %edi, %eax
        movl    %edi, %eax                              retq
        imull   %edi, %eax
        ret

as it can be seen the output has different instructions. So my question is does one of them has advantage over another in different projects?


回答1:


Yes. And no.

This is like asking whether an Audi car has an advantage over a Mercedes car. Like them, the two compilers are two different projects aiming to do the same thing. In some cases, gcc will emit better code, in others it will be clang.

When you need to know, you have to compile your code with both and then measure it.

There is an argument here and somewhat less related here.




回答2:


In this case the Clang output is better, because it does not branch; instead it loads the value of num % 2 == 1 to al the code generated by gcc uses jumps. If num is expected to be even/odd with 50 % chances, and with no repeating patterns, the code generated by GCC will be susceptible to branch prediction failure.


However you can make the code well-behaved on GCC as well by doing

int foo(int num) {
    return num * num + (num % 2 != 1);
}

Even more so, as it seems that your algorithm is really defined for unsigned numbers only, you should use unsigned int (they're different for negative numbers) - actually you get a major speedup by using unsigned int for the argument, as now GCC/Clang can optimize num % 2 to num & 1:

unsigned int foo(unsigned int num) {
    return num * num + (num % 2 != 1);
}

The resulting code generated by gcc -O2

movl    %edi, %edx
imull   %edi, %edi
andl    $1, %edx
xorl    $1, %edx
leal    (%rdi,%rdx), %eax
ret

is much better than the code for your original function generated by either compiler. Thus a compiler does not matter as much as does a programmer who knows what he's doing.



来源:https://stackoverflow.com/questions/36542284/difference-between-clang-and-gcc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!