llvm.org GIT mirror llvm / 70957b9
Allow constant folding of round function whenever feasible git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@203198 91177308-0d34-0410-b5e6-96231b3b80d8 Karthik Bhat 5 years ago
2 changed file(s) with 97 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
11951195 case Intrinsic::fma:
11961196 case Intrinsic::fmuladd:
11971197 case Intrinsic::copysign:
1198 case Intrinsic::round:
11981199 case Intrinsic::sadd_with_overflow:
11991200 case Intrinsic::uadd_with_overflow:
12001201 case Intrinsic::ssub_with_overflow:
13431344
13441345 if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy())
13451346 return 0;
1347
1348 if (IntrinsicID == Intrinsic::round) {
1349 APFloat V = Op->getValueAPF();
1350 V.roundToIntegral(APFloat::rmNearestTiesToAway);
1351 return ConstantFP::get(Ty->getContext(), V);
1352 }
13461353
13471354 /// We only fold functions with finite arguments. Folding NaN and inf is
13481355 /// likely to be aborted with an exception anyway, and some host libms
0 ; RUN: opt -S -instcombine < %s | FileCheck %s
1
2 declare float @llvm.round.f32(float) #0
3 declare double @llvm.round.f64(double) #0
4
5 ; CHECK-LABEL: @constant_fold_round_f32_01
6 ; CHECK-NEXT: ret float 1.000000e+00
7 define float @constant_fold_round_f32_01() #0 {
8 %x = call float @llvm.round.f32(float 1.25) #0
9 ret float %x
10 }
11
12 ; CHECK-LABEL: @constant_fold_round_f32_02
13 ; CHECK-NEXT: ret float -1.000000e+00
14 define float @constant_fold_round_f32_02() #0 {
15 %x = call float @llvm.round.f32(float -1.25) #0
16 ret float %x
17 }
18
19 ; CHECK-LABEL: @constant_fold_round_f32_03
20 ; CHECK-NEXT: ret float 2.000000e+00
21 define float @constant_fold_round_f32_03() #0 {
22 %x = call float @llvm.round.f32(float 1.5) #0
23 ret float %x
24 }
25
26 ; CHECK-LABEL: @constant_fold_round_f32_04
27 ; CHECK-NEXT: ret float -2.000000e+00
28 define float @constant_fold_round_f32_04() #0 {
29 %x = call float @llvm.round.f32(float -1.5) #0
30 ret float %x
31 }
32
33 ; CHECK-LABEL: @constant_fold_round_f32_05
34 ; CHECK-NEXT: ret float 3.000000e+00
35 define float @constant_fold_round_f32_05() #0 {
36 %x = call float @llvm.round.f32(float 2.75) #0
37 ret float %x
38 }
39
40 ; CHECK-LABEL: @constant_fold_round_f32_06
41 ; CHECK-NEXT: ret float -3.000000e+00
42 define float @constant_fold_round_f32_06() #0 {
43 %x = call float @llvm.round.f32(float -2.75) #0
44 ret float %x
45 }
46
47 ; CHECK-LABEL: @constant_fold_round_f64_01
48 ; CHECK-NEXT: ret double 1.000000e+00
49 define double @constant_fold_round_f64_01() #0 {
50 %x = call double @llvm.round.f64(double 1.3) #0
51 ret double %x
52 }
53
54 ; CHECK-LABEL: @constant_fold_round_f64_02
55 ; CHECK-NEXT: ret double -1.000000e+00
56 define double @constant_fold_round_f64_02() #0 {
57 %x = call double @llvm.round.f64(double -1.3) #0
58 ret double %x
59 }
60
61 ; CHECK-LABEL: @constant_fold_round_f64_03
62 ; CHECK-NEXT: ret double 2.000000e+00
63 define double @constant_fold_round_f64_03() #0 {
64 %x = call double @llvm.round.f64(double 1.5) #0
65 ret double %x
66 }
67
68 ; CHECK-LABEL: @constant_fold_round_f64_04
69 ; CHECK-NEXT: ret double -2.000000e+00
70 define double @constant_fold_round_f64_04() #0 {
71 %x = call double @llvm.round.f64(double -1.5) #0
72 ret double %x
73 }
74
75 ; CHECK-LABEL: @constant_fold_round_f64_05
76 ; CHECK-NEXT: ret double 3.000000e+00
77 define double @constant_fold_round_f64_05() #0 {
78 %x = call double @llvm.round.f64(double 2.7) #0
79 ret double %x
80 }
81
82 ; CHECK-LABEL: @constant_fold_round_f64_06
83 ; CHECK-NEXT: ret double -3.000000e+00
84 define double @constant_fold_round_f64_06() #0 {
85 %x = call double @llvm.round.f64(double -2.7) #0
86 ret double %x
87 }
88
89 attributes #0 = { nounwind readnone }