强制clang生成固有cos

时间:2018-12-05 18:07:05

标签: clang llvm intrinsics cos

编译cos.c

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

使用

clang -S -emit-llvm -c cos.c

我有

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)

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

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

declare double @llvm.cos.f64(double)

如何强制clang这样做?也许我应该使用另一个函数而不是__builtin_cos

1 个答案:

答案 0 :(得分:2)

使用-ffast-math(表示-fno-math-errno,c -O3会将__builtin_cos内联到@llvm.cos.f64

double func(double in) {
    double a = __builtin_cos(in);
    return a;
}

clang -O3 -ffast-math -emit-llvm on Godbolt(已删除调试内容)

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
}