Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LLVM not using machine instructions

Tags:

c

gcc

llvm

I was testing the quality of code generated by LLVM vs gcc

I have a small program like this

#include<math.h>
double mysqrt(double a){
    return sqrt(a);
}

int main()
{
    mysqrt(0.1);
    return 1;
}

Clang was emitting

mysqrt:                                 # @mysqrt
# BB#0:                                 # %entry
jmp sqrt                    # TAILCALL

which means it was calling sqrt function

gcc was emitting

mysqrt:
.LFB25:
.cfi_startproc
subl    $28, %esp
.cfi_def_cfa_offset 32
fldl    32(%esp)
fld %st(0)
fsqrt
fucomi  %st(0), %st

which means it was using direct machine instruction fsqrt (which i suppose is much faster than calling function). This was done for X86 machine with O3 level of optimization. Does anybody know why LLVM is calling function instead of using machine instruction?

like image 257
sarda Avatar asked Nov 22 '25 11:11

sarda


1 Answers

The behavior of gcc is not standard C, since that inlined call is missing error checks. To have standard conforming behavior with gcc, compile with option -std=c99 or similar. To force clang to forget about its standard compliance something like -ffast-math might help.

like image 194
Jens Gustedt Avatar answered Nov 25 '25 01:11

Jens Gustedt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!