[cfe-dev] Implement foo in terms of __builtin_foo fails

Jon Chesterfield via cfe-dev cfe-dev at lists.llvm.org
Fri Feb 23 09:03:46 PST 2018


Hello clang devs,

I'm implementing libm for a toolchain based on LLVM. There is instruction
level support for some libm functions that I'd like to target via clang.
The IR to machine code part is working so I think the next step is writing
C that generates calls to the llvm intrinsics.

For example:
double sin(double x) { return __builtin_sin(x); }

I expected this to compile to:
define double @sin(double %x)  {
  %res = call double @llvm.sin.f64(double %x)
  ret double %res
}

Our back end recognises llvm.sin.f64 so this would compile into sensible
instructions.

However, at -O0 clang generates a call to sin.

define double @sin(double %x) {
  %x.addr = alloca double, align 8
  store double %x, double* %x.addr, align 8
  %0 = load double, double* %x.addr, align 8
  %call = call double @sin(double %0)
  ret double %call
}

At higher optimisations, this recursive call is detected and optimised into:
define double @sin(double %x) {
entry:
  ret double undef
}

How can I write C that generates a call to the llvm.sin.f64 intrinsic?

Cheers,

Jon
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20180223/9fba739e/attachment.html>


More information about the cfe-dev mailing list