Skip to content

Commit 04e11b7

Browse files
committed
[SimplifyLibCalls] Shrink sin, cos to sinf, cosf when allowed
1 parent 6b37eee commit 04e11b7

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4136,6 +4136,11 @@ Value *LibCallSimplifier::optimizeCall(CallInst *CI, IRBuilderBase &Builder) {
41364136
return optimizeMemCpy(CI, Builder);
41374137
case Intrinsic::memmove:
41384138
return optimizeMemMove(CI, Builder);
4139+
case Intrinsic::sin:
4140+
case Intrinsic::cos:
4141+
if (UnsafeFPShrink)
4142+
return optimizeUnaryDoubleFP(CI, Builder, TLI, /*isPrecise=*/true);
4143+
return nullptr;
41394144
default:
41404145
return nullptr;
41414146
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt < %s -passes=instcombine -S | FileCheck %s --check-prefixes=ANY,NO-FLOAT-SHRINK
3+
; RUN: opt < %s -passes=instcombine -enable-double-float-shrink -S | FileCheck %s --check-prefixes=ANY,DO-FLOAT-SHRINK
4+
5+
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
6+
7+
declare double @llvm.cos.f64(double)
8+
declare float @llvm.cos.f32(float)
9+
10+
declare double @llvm.sin.f64(double)
11+
declare float @llvm.sin.f32(float)
12+
13+
; cos -> cosf
14+
15+
; ANY-LABEL: define float @cos_no_fastmath(float %f) {
16+
; NO-FLOAT-SHRINK: call double @llvm.cos.f64
17+
; DO-FLOAT-SHRINK: %[[RES:[0-9]+]] = call float @llvm.cos.f32
18+
; DO-FLOAT-SHRINK: ret float %[[RES]]
19+
; ANY: }
20+
define float @cos_no_fastmath(float %f) {
21+
%d = fpext float %f to double
22+
%result = call double @llvm.cos.f64(double %d)
23+
%truncated_result = fptrunc double %result to float
24+
ret float %truncated_result
25+
}
26+
27+
; ANY-LABEL: define float @cos_fastmath(float %f) {
28+
; ANY: %[[RES:[0-9]+]] = call fast float @llvm.cos.f32
29+
; ANY: ret float %[[RES]]
30+
; ANY: }
31+
define float @cos_fastmath(float %f) {
32+
%d = fpext fast float %f to double
33+
%result = call fast double @llvm.cos.f64(double %d)
34+
%truncated_result = fptrunc fast double %result to float
35+
ret float %truncated_result
36+
}
37+
38+
; sin -> sinf
39+
40+
; ANY-LABEL: define float @sin_no_fastmath(float %f) {
41+
; NO-FLOAT-SHRINK: call double @llvm.sin.f64
42+
; DO-FLOAT-SHRINK: %[[RES:[0-9]+]] = call float @llvm.sin.f32
43+
; DO-FLOAT-SHRINK: ret float %[[RES]]
44+
; ANY: }
45+
define float @sin_no_fastmath(float %f) {
46+
%d = fpext float %f to double
47+
%result = call double @llvm.sin.f64(double %d)
48+
%truncated_result = fptrunc double %result to float
49+
ret float %truncated_result
50+
}
51+
52+
; ANY-LABEL: define float @sin_fastmath(float %f) {
53+
; ANY: %[[RES:[0-9]+]] = call fast float @llvm.sin.f32
54+
; ANY: ret float %[[RES]]
55+
; ANY: }
56+
define float @sin_fastmath(float %f) {
57+
%d = fpext fast float %f to double
58+
%result = call fast double @llvm.sin.f64(double %d)
59+
%truncated_result = fptrunc fast double %result to float
60+
ret float %truncated_result
61+
}

0 commit comments

Comments
 (0)