强制clang生成内部cos

强制clang生成内部cos,clang,llvm,intrinsics,trigonometry,Clang,Llvm,Intrinsics,Trigonometry,编译cos.c void func() { double a = __builtin_cos(3.0); } 使用 我有 define dso_local void @func() { %1 = alloca double, align 8 %2 = call double @cos(double 3.000000e+00) store double %2, double* %1, align 8 ret void } declare dso_local doubl

编译
cos.c

void func() {
    double a = __builtin_cos(3.0);
}
使用

我有

define dso_local void @func() {
  %1 = alloca double, align 8
  %2 = call double @cos(double 3.000000e+00)
  store double %2, double* %1, align 8
  ret void
}


declare dso_local double @cos(double)
但是我想为
cos
获取llvm内部函数
@llvm.fcos.f64
,而不是
@cos
,即生成的代码应该是这样的

  ...
  %2 = call double @llvm.fcos.f64(double 3.000000e+00)
  ...
}

declare double @llvm.cos.f64(double)

我怎样才能强迫
clang
这样做?也许我应该用另一个函数来代替
\uuuuu builtin\u cos

-ffast math
(这意味着
-fno math errno
,clang
-O3
\uu builtin\u cos
内联到
@llvm.cos.f64

double func(double in) {
    double a = __builtin_cos(in);
    return a;
}
clang-O3-ffast math-emit llvm
(删除调试内容)


我想也许我们可以告诉clang在没有SSE的情况下为32位x86编译,这样它就可以作为x87
fcos
指令内联
cos
。(这就是LLVM
fcos.f64
吗?)但不是,它仍然想调用library
cos()
。即使使用
-O3-ffast math-m32-mno sse
(对于运行时变量函数arg,它不能通过
cos
传播常数,只返回一个常数。)@PeterCordes我认为使用
内置cos
是不好的,因为这个函数有副作用(它设置
errno
代码)
-ffast math
应包括
-fno math errno
。(它适用于GCC,但我对clang不太熟悉。)@PeterCordes你能把你的评论作为答案吗,因为它是正确的:
-ffast math
forces
clang
generate
@llvm.cos.f64
。非常感谢。我的第一条评论是错误的,结果我得到了正确的答案,但没有仔细阅读,哎呀!(我通常只看x86 asm,而不是LLVM-IR)。毕竟不需要强制32位no-sse。
double func(double in) {
    double a = __builtin_cos(in);
    return a;
}
define dso_local double @_Z4funcd(double) local_unnamed_addr #0 !dbg !7 {
  %2 = tail call fast double @llvm.cos.f64(double %0), !dbg !15
  ret double %2, !dbg !17
}