1 ; Test that the pow library call simplifier works correctly.
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
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.
13 declare float @powf(float, float) nounwind readonly
14 declare double @pow(double, double) nounwind readonly
16 ; Check pow(1.0, x) -> 1.0.
18 define float @test_simplify1(float %x) {
19 ; CHECK-LABEL: @test_simplify1(
20 %retval = call float @powf(float 1.0, float %x)
22 ; CHECK-NEXT: ret float 1.000000e+00
25 define double @test_simplify2(double %x) {
26 ; CHECK-LABEL: @test_simplify2(
27 %retval = call double @pow(double 1.0, double %x)
29 ; CHECK-NEXT: ret double 1.000000e+00
32 ; Check pow(2.0, x) -> exp2(x).
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]+]]
39 ; CHECK-NEXT: ret float [[EXP2F]]
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]]
47 ; CHECK-NEXT: ret double [[EXP2]]
50 ; Check pow(x, 0.0) -> 1.0.
52 define float @test_simplify5(float %x) {
53 ; CHECK-LABEL: @test_simplify5(
54 %retval = call float @powf(float %x, float 0.0)
56 ; CHECK-NEXT: ret float 1.000000e+00
59 define double @test_simplify6(double %x) {
60 ; CHECK-LABEL: @test_simplify6(
61 %retval = call double @pow(double %x, double 0.0)
63 ; CHECK-NEXT: ret double 1.000000e+00
66 ; Check pow(x, 0.5) -> fabs(sqrt(x)), where x != -infinity.
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]]
76 ; CHECK-NEXT: ret float [[SELECT]]
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]]
87 ; CHECK-NEXT: ret double [[SELECT]]
90 ; Check pow(-infinity, 0.5) -> +infinity.
92 define float @test_simplify9(float %x) {
93 ; CHECK-LABEL: @test_simplify9(
94 %retval = call float @powf(float 0xFFF0000000000000, float 0.5)
96 ; CHECK-NEXT: ret float 0x7FF0000000000000
99 define double @test_simplify10(double %x) {
100 ; CHECK-LABEL: @test_simplify10(
101 %retval = call double @pow(double 0xFFF0000000000000, double 0.5)
103 ; CHECK-NEXT: ret double 0x7FF0000000000000
106 ; Check pow(x, 1.0) -> x.
108 define float @test_simplify11(float %x) {
109 ; CHECK-LABEL: @test_simplify11(
110 %retval = call float @powf(float %x, float 1.0)
112 ; CHECK-NEXT: ret float %x
115 define double @test_simplify12(double %x) {
116 ; CHECK-LABEL: @test_simplify12(
117 %retval = call double @pow(double %x, double 1.0)
119 ; CHECK-NEXT: ret double %x
122 ; Check pow(x, 2.0) -> x*x.
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
129 ; CHECK-NEXT: ret float [[SQUARE]]
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
137 ; CHECK-NEXT: ret double [[SQUARE]]
140 ; Check pow(x, -1.0) -> 1.0/x.
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
147 ; CHECK-NEXT: ret float [[RECIPROCAL]]
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
155 ; CHECK-NEXT: ret double [[RECIPROCAL]]
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]]
167 ; CHECK-NEXT: ret double [[SELECT]]
170 ; Check pow(10.0, x) -> __exp10(x) on OS X 10.9+ and iOS 7.0+.
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]+]]
177 ; CHECK-EXP10: ret float [[EXP10F]]
178 ; CHECK-NO-EXP10: call float @powf
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]]
186 ; CHECK-EXP10: ret double [[EXP10]]
187 ; CHECK-NO-EXP10: call double @pow
190 ; CHECK: attributes [[NUW_RO]] = { nounwind readonly }