Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between clang and gcc [closed]

Tags:

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?

like image 865
Pooya Avatar asked Apr 11 '16 07:04

Pooya


People also ask

Will GCC be replaced by Clang?

Clang is a compiler front end for the C, C++, Objective-C, and Objective-C++ programming languages, as well as the OpenMP, OpenCL, RenderScript, CUDA, and HIP frameworks. It acts as a drop-in replacement for the GNU Compiler Collection (GCC), supporting most of its compilation flags and unofficial language extensions.

Does Google use Clang or GCC?

Google's Chrome browser is now built using the Clang compiler on Windows. Previously built using the Microsoft C++ compiler, Google is now using the same compiler for Windows, macOS, Linux, and Android, and the switch makes Chrome arguably the first major software project to use Clang on Windows.

What is the difference between Clang and LLVM?

LLVM is a backend compiler meant to build compilers on top of it. It deals with optimizations and production of code adapted to the target architecture. CLang is a front end which parses C, C++ and Objective C code and translates it into a representation suitable for LLVM.


1 Answers

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.

like image 199
Honza Remeš Avatar answered Oct 23 '22 16:10

Honza Remeš