blob: fb3b7d796160173c7d43d1b4a39a1f405964649b [file] [log] [blame]
Meador Inge193e0352012-11-13 04:16:17 +00001; Test that the pow library call simplifier works correctly.
2;
3; RUN: opt < %s -instcombine -S | FileCheck %s
Yi Jiangf92a5742013-12-12 01:55:04 +00004; RUN: opt -instcombine -S < %s -mtriple=x86_64-apple-macosx10.9 | FileCheck %s --check-prefix=CHECK-EXP10
5; RUN: opt -instcombine -S < %s -mtriple=arm-apple-ios7.0 | FileCheck %s --check-prefix=CHECK-EXP10
6; RUN: opt -instcombine -S < %s -mtriple=x86_64-apple-macosx10.8 | FileCheck %s --check-prefix=CHECK-NO-EXP10
7; RUN: opt -instcombine -S < %s -mtriple=arm-apple-ios6.0 | FileCheck %s --check-prefix=CHECK-NO-EXP10
Joerg Sonnenbergerddb58282013-12-15 20:36:17 +00008; RUN: opt -instcombine -S < %s -mtriple=x86_64-netbsd | FileCheck %s --check-prefix=CHECK-NO-EXP10
Meador Inge193e0352012-11-13 04:16:17 +00009; rdar://7251832
10
11; NOTE: The readonly attribute on the pow call should be preserved
12; in the cases below where pow is transformed into another function call.
13
14declare float @powf(float, float) nounwind readonly
15declare double @pow(double, double) nounwind readonly
16
17; Check pow(1.0, x) -> 1.0.
18
19define float @test_simplify1(float %x) {
Stephen Linc1c7a132013-07-14 01:42:54 +000020; CHECK-LABEL: @test_simplify1(
Meador Inge193e0352012-11-13 04:16:17 +000021 %retval = call float @powf(float 1.0, float %x)
22 ret float %retval
23; CHECK-NEXT: ret float 1.000000e+00
24}
25
26define double @test_simplify2(double %x) {
Stephen Linc1c7a132013-07-14 01:42:54 +000027; CHECK-LABEL: @test_simplify2(
Meador Inge193e0352012-11-13 04:16:17 +000028 %retval = call double @pow(double 1.0, double %x)
29 ret double %retval
30; CHECK-NEXT: ret double 1.000000e+00
31}
32
33; Check pow(2.0, x) -> exp2(x).
34
35define float @test_simplify3(float %x) {
Stephen Linc1c7a132013-07-14 01:42:54 +000036; CHECK-LABEL: @test_simplify3(
Meador Inge193e0352012-11-13 04:16:17 +000037 %retval = call float @powf(float 2.0, float %x)
Bill Wendlinga0323742013-02-22 09:09:42 +000038; CHECK-NEXT: [[EXP2F:%[a-z0-9]+]] = call float @exp2f(float %x) [[NUW_RO:#[0-9]+]]
Meador Inge193e0352012-11-13 04:16:17 +000039 ret float %retval
40; CHECK-NEXT: ret float [[EXP2F]]
41}
42
43define double @test_simplify4(double %x) {
Stephen Linc1c7a132013-07-14 01:42:54 +000044; CHECK-LABEL: @test_simplify4(
Meador Inge193e0352012-11-13 04:16:17 +000045 %retval = call double @pow(double 2.0, double %x)
Bill Wendlinga0323742013-02-22 09:09:42 +000046; CHECK-NEXT: [[EXP2:%[a-z0-9]+]] = call double @exp2(double %x) [[NUW_RO]]
Meador Inge193e0352012-11-13 04:16:17 +000047 ret double %retval
48; CHECK-NEXT: ret double [[EXP2]]
49}
50
51; Check pow(x, 0.0) -> 1.0.
52
53define float @test_simplify5(float %x) {
Stephen Linc1c7a132013-07-14 01:42:54 +000054; CHECK-LABEL: @test_simplify5(
Meador Inge193e0352012-11-13 04:16:17 +000055 %retval = call float @powf(float %x, float 0.0)
56 ret float %retval
57; CHECK-NEXT: ret float 1.000000e+00
58}
59
60define double @test_simplify6(double %x) {
Stephen Linc1c7a132013-07-14 01:42:54 +000061; CHECK-LABEL: @test_simplify6(
Meador Inge193e0352012-11-13 04:16:17 +000062 %retval = call double @pow(double %x, double 0.0)
63 ret double %retval
64; CHECK-NEXT: ret double 1.000000e+00
65}
66
67; Check pow(x, 0.5) -> fabs(sqrt(x)), where x != -infinity.
68
69define float @test_simplify7(float %x) {
Stephen Linc1c7a132013-07-14 01:42:54 +000070; CHECK-LABEL: @test_simplify7(
Meador Inge193e0352012-11-13 04:16:17 +000071 %retval = call float @powf(float %x, float 0.5)
Bill Wendlinga0323742013-02-22 09:09:42 +000072; CHECK-NEXT: [[SQRTF:%[a-z0-9]+]] = call float @sqrtf(float %x) [[NUW_RO]]
73; CHECK-NEXT: [[FABSF:%[a-z0-9]+]] = call float @fabsf(float [[SQRTF]]) [[NUW_RO]]
Meador Inge193e0352012-11-13 04:16:17 +000074; CHECK-NEXT: [[FCMP:%[a-z0-9]+]] = fcmp oeq float %x, 0xFFF0000000000000
75; CHECK-NEXT: [[SELECT:%[a-z0-9]+]] = select i1 [[FCMP]], float 0x7FF0000000000000, float [[FABSF]]
76 ret float %retval
77; CHECK-NEXT: ret float [[SELECT]]
78}
79
80define double @test_simplify8(double %x) {
Stephen Linc1c7a132013-07-14 01:42:54 +000081; CHECK-LABEL: @test_simplify8(
Meador Inge193e0352012-11-13 04:16:17 +000082 %retval = call double @pow(double %x, double 0.5)
Bill Wendlinga0323742013-02-22 09:09:42 +000083; CHECK-NEXT: [[SQRT:%[a-z0-9]+]] = call double @sqrt(double %x) [[NUW_RO]]
84; CHECK-NEXT: [[FABS:%[a-z0-9]+]] = call double @fabs(double [[SQRT]]) [[NUW_RO]]
Meador Inge193e0352012-11-13 04:16:17 +000085; CHECK-NEXT: [[FCMP:%[a-z0-9]+]] = fcmp oeq double %x, 0xFFF0000000000000
86; CHECK-NEXT: [[SELECT:%[a-z0-9]+]] = select i1 [[FCMP]], double 0x7FF0000000000000, double [[FABS]]
87 ret double %retval
88; CHECK-NEXT: ret double [[SELECT]]
89}
90
91; Check pow(-infinity, 0.5) -> +infinity.
92
93define float @test_simplify9(float %x) {
Stephen Linc1c7a132013-07-14 01:42:54 +000094; CHECK-LABEL: @test_simplify9(
Meador Inge193e0352012-11-13 04:16:17 +000095 %retval = call float @powf(float 0xFFF0000000000000, float 0.5)
96 ret float %retval
97; CHECK-NEXT: ret float 0x7FF0000000000000
98}
99
100define double @test_simplify10(double %x) {
Stephen Linc1c7a132013-07-14 01:42:54 +0000101; CHECK-LABEL: @test_simplify10(
Meador Inge193e0352012-11-13 04:16:17 +0000102 %retval = call double @pow(double 0xFFF0000000000000, double 0.5)
103 ret double %retval
104; CHECK-NEXT: ret double 0x7FF0000000000000
105}
106
107; Check pow(x, 1.0) -> x.
108
109define float @test_simplify11(float %x) {
Stephen Linc1c7a132013-07-14 01:42:54 +0000110; CHECK-LABEL: @test_simplify11(
Meador Inge193e0352012-11-13 04:16:17 +0000111 %retval = call float @powf(float %x, float 1.0)
112 ret float %retval
113; CHECK-NEXT: ret float %x
114}
115
116define double @test_simplify12(double %x) {
Stephen Linc1c7a132013-07-14 01:42:54 +0000117; CHECK-LABEL: @test_simplify12(
Meador Inge193e0352012-11-13 04:16:17 +0000118 %retval = call double @pow(double %x, double 1.0)
119 ret double %retval
120; CHECK-NEXT: ret double %x
121}
122
123; Check pow(x, 2.0) -> x*x.
124
125define float @test_simplify13(float %x) {
Stephen Linc1c7a132013-07-14 01:42:54 +0000126; CHECK-LABEL: @test_simplify13(
Meador Inge193e0352012-11-13 04:16:17 +0000127 %retval = call float @powf(float %x, float 2.0)
128; CHECK-NEXT: [[SQUARE:%[a-z0-9]+]] = fmul float %x, %x
129 ret float %retval
130; CHECK-NEXT: ret float [[SQUARE]]
131}
132
133define double @test_simplify14(double %x) {
Stephen Linc1c7a132013-07-14 01:42:54 +0000134; CHECK-LABEL: @test_simplify14(
Meador Inge193e0352012-11-13 04:16:17 +0000135 %retval = call double @pow(double %x, double 2.0)
136; CHECK-NEXT: [[SQUARE:%[a-z0-9]+]] = fmul double %x, %x
137 ret double %retval
138; CHECK-NEXT: ret double [[SQUARE]]
139}
140
141; Check pow(x, -1.0) -> 1.0/x.
142
143define float @test_simplify15(float %x) {
Stephen Linc1c7a132013-07-14 01:42:54 +0000144; CHECK-LABEL: @test_simplify15(
Meador Inge193e0352012-11-13 04:16:17 +0000145 %retval = call float @powf(float %x, float -1.0)
146; CHECK-NEXT: [[RECIPROCAL:%[a-z0-9]+]] = fdiv float 1.000000e+00, %x
147 ret float %retval
148; CHECK-NEXT: ret float [[RECIPROCAL]]
149}
150
151define double @test_simplify16(double %x) {
Stephen Linc1c7a132013-07-14 01:42:54 +0000152; CHECK-LABEL: @test_simplify16(
Meador Inge193e0352012-11-13 04:16:17 +0000153 %retval = call double @pow(double %x, double -1.0)
154; CHECK-NEXT: [[RECIPROCAL:%[a-z0-9]+]] = fdiv double 1.000000e+00, %x
155 ret double %retval
156; CHECK-NEXT: ret double [[RECIPROCAL]]
157}
Bill Wendlinga0323742013-02-22 09:09:42 +0000158
Michael Kuperstein4bb3f8f2013-08-19 06:55:47 +0000159declare double @llvm.pow.f64(double %Val, double %Power)
160define double @test_simplify17(double %x) {
161; CHECK-LABEL: @test_simplify17(
162 %retval = call double @llvm.pow.f64(double %x, double 0.5)
Raul E. Silverab741b942014-03-06 00:18:15 +0000163; CHECK-NEXT: [[SQRT:%[a-z0-9]+]] = call double @sqrt(double %x)
164; CHECK-NEXT: [[FABS:%[a-z0-9]+]] = call double @fabs(double [[SQRT]])
Michael Kuperstein4bb3f8f2013-08-19 06:55:47 +0000165; CHECK-NEXT: [[FCMP:%[a-z0-9]+]] = fcmp oeq double %x, 0xFFF0000000000000
166; CHECK-NEXT: [[SELECT:%[a-z0-9]+]] = select i1 [[FCMP]], double 0x7FF0000000000000, double [[FABS]]
167 ret double %retval
168; CHECK-NEXT: ret double [[SELECT]]
169}
170
Yi Jiangf92a5742013-12-12 01:55:04 +0000171; Check pow(10.0, x) -> __exp10(x) on OS X 10.9+ and iOS 7.0+.
172
173define float @test_simplify18(float %x) {
174; CHECK-LABEL: @test_simplify18(
175 %retval = call float @powf(float 10.0, float %x)
176; CHECK-EXP10: [[EXP10F:%[_a-z0-9]+]] = call float @__exp10f(float %x) [[NUW_RO:#[0-9]+]]
177 ret float %retval
178; CHECK-EXP10: ret float [[EXP10F]]
179; CHECK-NO-EXP10: call float @powf
180}
181
182define double @test_simplify19(double %x) {
183; CHECK-LABEL: @test_simplify19(
184 %retval = call double @pow(double 10.0, double %x)
185; CHECK-EXP10: [[EXP10:%[_a-z0-9]+]] = call double @__exp10(double %x) [[NUW_RO]]
186 ret double %retval
187; CHECK-EXP10: ret double [[EXP10]]
188; CHECK-NO-EXP10: call double @pow
189}
190
Bill Wendlinga0323742013-02-22 09:09:42 +0000191; CHECK: attributes [[NUW_RO]] = { nounwind readonly }
Michael Kuperstein4bb3f8f2013-08-19 06:55:47 +0000192