Resubmit r196544: Apply transformation on OS X 10.9+ and iOS 7.0+: pow(10, x) ―>...
[oota-llvm.git] / test / Transforms / InstCombine / pow-1.ll
1 ; Test that the pow library call simplifier works correctly.
2 ;
3 ; RUN: opt < %s -instcombine -S | FileCheck %s
4 ; 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
8 ; rdar://7251832
9
10 ; NOTE: The readonly attribute on the pow call should be preserved
11 ; in the cases below where pow is transformed into another function call.
12
13 declare float @powf(float, float) nounwind readonly
14 declare double @pow(double, double) nounwind readonly
15
16 ; Check pow(1.0, x) -> 1.0.
17
18 define float @test_simplify1(float %x) {
19 ; CHECK-LABEL: @test_simplify1(
20   %retval = call float @powf(float 1.0, float %x)
21   ret float %retval
22 ; CHECK-NEXT: ret float 1.000000e+00
23 }
24
25 define double @test_simplify2(double %x) {
26 ; CHECK-LABEL: @test_simplify2(
27   %retval = call double @pow(double 1.0, double %x)
28   ret double %retval
29 ; CHECK-NEXT: ret double 1.000000e+00
30 }
31
32 ; Check pow(2.0, x) -> exp2(x).
33
34 define float @test_simplify3(float %x) {
35 ; CHECK-LABEL: @test_simplify3(
36   %retval = call float @powf(float 2.0, float %x)
37 ; CHECK-NEXT: [[EXP2F:%[a-z0-9]+]] = call float @exp2f(float %x) [[NUW_RO:#[0-9]+]]
38   ret float %retval
39 ; CHECK-NEXT: ret float [[EXP2F]]
40 }
41
42 define double @test_simplify4(double %x) {
43 ; CHECK-LABEL: @test_simplify4(
44   %retval = call double @pow(double 2.0, double %x)
45 ; CHECK-NEXT: [[EXP2:%[a-z0-9]+]] = call double @exp2(double %x) [[NUW_RO]]
46   ret double %retval
47 ; CHECK-NEXT: ret double [[EXP2]]
48 }
49
50 ; Check pow(x, 0.0) -> 1.0.
51
52 define float @test_simplify5(float %x) {
53 ; CHECK-LABEL: @test_simplify5(
54   %retval = call float @powf(float %x, float 0.0)
55   ret float %retval
56 ; CHECK-NEXT: ret float 1.000000e+00
57 }
58
59 define double @test_simplify6(double %x) {
60 ; CHECK-LABEL: @test_simplify6(
61   %retval = call double @pow(double %x, double 0.0)
62   ret double %retval
63 ; CHECK-NEXT: ret double 1.000000e+00
64 }
65
66 ; Check pow(x, 0.5) -> fabs(sqrt(x)), where x != -infinity.
67
68 define float @test_simplify7(float %x) {
69 ; CHECK-LABEL: @test_simplify7(
70   %retval = call float @powf(float %x, float 0.5)
71 ; CHECK-NEXT: [[SQRTF:%[a-z0-9]+]] = call float @sqrtf(float %x) [[NUW_RO]]
72 ; CHECK-NEXT: [[FABSF:%[a-z0-9]+]] = call float @fabsf(float [[SQRTF]]) [[NUW_RO]]
73 ; CHECK-NEXT: [[FCMP:%[a-z0-9]+]] = fcmp oeq float %x, 0xFFF0000000000000
74 ; CHECK-NEXT: [[SELECT:%[a-z0-9]+]] = select i1 [[FCMP]], float 0x7FF0000000000000, float [[FABSF]]
75   ret float %retval
76 ; CHECK-NEXT: ret float [[SELECT]]
77 }
78
79 define double @test_simplify8(double %x) {
80 ; CHECK-LABEL: @test_simplify8(
81   %retval = call double @pow(double %x, double 0.5)
82 ; CHECK-NEXT: [[SQRT:%[a-z0-9]+]] = call double @sqrt(double %x) [[NUW_RO]]
83 ; CHECK-NEXT: [[FABS:%[a-z0-9]+]] = call double @fabs(double [[SQRT]]) [[NUW_RO]]
84 ; CHECK-NEXT: [[FCMP:%[a-z0-9]+]] = fcmp oeq double %x, 0xFFF0000000000000
85 ; CHECK-NEXT: [[SELECT:%[a-z0-9]+]] = select i1 [[FCMP]], double 0x7FF0000000000000, double [[FABS]]
86   ret double %retval
87 ; CHECK-NEXT: ret double [[SELECT]]
88 }
89
90 ; Check pow(-infinity, 0.5) -> +infinity.
91
92 define float @test_simplify9(float %x) {
93 ; CHECK-LABEL: @test_simplify9(
94   %retval = call float @powf(float 0xFFF0000000000000, float 0.5)
95   ret float %retval
96 ; CHECK-NEXT: ret float 0x7FF0000000000000
97 }
98
99 define double @test_simplify10(double %x) {
100 ; CHECK-LABEL: @test_simplify10(
101   %retval = call double @pow(double 0xFFF0000000000000, double 0.5)
102   ret double %retval
103 ; CHECK-NEXT: ret double 0x7FF0000000000000
104 }
105
106 ; Check pow(x, 1.0) -> x.
107
108 define float @test_simplify11(float %x) {
109 ; CHECK-LABEL: @test_simplify11(
110   %retval = call float @powf(float %x, float 1.0)
111   ret float %retval
112 ; CHECK-NEXT: ret float %x
113 }
114
115 define double @test_simplify12(double %x) {
116 ; CHECK-LABEL: @test_simplify12(
117   %retval = call double @pow(double %x, double 1.0)
118   ret double %retval
119 ; CHECK-NEXT: ret double %x
120 }
121
122 ; Check pow(x, 2.0) -> x*x.
123
124 define float @test_simplify13(float %x) {
125 ; CHECK-LABEL: @test_simplify13(
126   %retval = call float @powf(float %x, float 2.0)
127 ; CHECK-NEXT: [[SQUARE:%[a-z0-9]+]] = fmul float %x, %x
128   ret float %retval
129 ; CHECK-NEXT: ret float [[SQUARE]]
130 }
131
132 define double @test_simplify14(double %x) {
133 ; CHECK-LABEL: @test_simplify14(
134   %retval = call double @pow(double %x, double 2.0)
135 ; CHECK-NEXT: [[SQUARE:%[a-z0-9]+]] = fmul double %x, %x
136   ret double %retval
137 ; CHECK-NEXT: ret double [[SQUARE]]
138 }
139
140 ; Check pow(x, -1.0) -> 1.0/x.
141
142 define float @test_simplify15(float %x) {
143 ; CHECK-LABEL: @test_simplify15(
144   %retval = call float @powf(float %x, float -1.0)
145 ; CHECK-NEXT: [[RECIPROCAL:%[a-z0-9]+]] = fdiv float 1.000000e+00, %x
146   ret float %retval
147 ; CHECK-NEXT: ret float [[RECIPROCAL]]
148 }
149
150 define double @test_simplify16(double %x) {
151 ; CHECK-LABEL: @test_simplify16(
152   %retval = call double @pow(double %x, double -1.0)
153 ; CHECK-NEXT: [[RECIPROCAL:%[a-z0-9]+]] = fdiv double 1.000000e+00, %x
154   ret double %retval
155 ; CHECK-NEXT: ret double [[RECIPROCAL]]
156 }
157
158 declare double @llvm.pow.f64(double %Val, double %Power)
159 define double @test_simplify17(double %x) {
160 ; CHECK-LABEL: @test_simplify17(
161   %retval = call double @llvm.pow.f64(double %x, double 0.5)
162 ; CHECK-NEXT: [[SQRT:%[a-z0-9]+]] = call double @sqrt(double %x) [[NUW_RO]]
163 ; CHECK-NEXT: [[FABS:%[a-z0-9]+]] = call double @fabs(double [[SQRT]]) [[NUW_RO]]
164 ; CHECK-NEXT: [[FCMP:%[a-z0-9]+]] = fcmp oeq double %x, 0xFFF0000000000000
165 ; CHECK-NEXT: [[SELECT:%[a-z0-9]+]] = select i1 [[FCMP]], double 0x7FF0000000000000, double [[FABS]]
166   ret double %retval
167 ; CHECK-NEXT: ret double [[SELECT]]
168 }
169
170 ; Check pow(10.0, x) -> __exp10(x) on OS X 10.9+ and iOS 7.0+.
171
172 define float @test_simplify18(float %x) {
173 ; CHECK-LABEL: @test_simplify18(
174   %retval = call float @powf(float 10.0, float %x)
175 ; CHECK-EXP10: [[EXP10F:%[_a-z0-9]+]] = call float @__exp10f(float %x) [[NUW_RO:#[0-9]+]]
176   ret float %retval
177 ; CHECK-EXP10: ret float [[EXP10F]]
178 ; CHECK-NO-EXP10: call float @powf
179 }
180
181 define double @test_simplify19(double %x) {
182 ; CHECK-LABEL: @test_simplify19(
183   %retval = call double @pow(double 10.0, double %x)
184 ; CHECK-EXP10: [[EXP10:%[_a-z0-9]+]] = call double @__exp10(double %x) [[NUW_RO]]
185   ret double %retval
186 ; CHECK-EXP10: ret double [[EXP10]]
187 ; CHECK-NO-EXP10: call double @pow
188 }
189
190 ; CHECK: attributes [[NUW_RO]] = { nounwind readonly }
191