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
8 ; RUN: opt -instcombine -S < %s -mtriple=x86_64-netbsd | FileCheck %s --check-prefix=CHECK-NO-EXP10
9 ; RUN: opt -instcombine -S < %s -mtriple=arm-apple-tvos9.0 | FileCheck %s --check-prefix=CHECK-EXP10
10 ; RUN: opt -instcombine -S < %s -mtriple=arm-apple-watchos2.0 | FileCheck %s --check-prefix=CHECK-EXP10
13 ; NOTE: The readonly attribute on the pow call should be preserved
14 ; in the cases below where pow is transformed into another function call.
16 declare float @powf(float, float) nounwind readonly
17 declare double @pow(double, double) nounwind readonly
19 ; Check pow(1.0, x) -> 1.0.
21 define float @test_simplify1(float %x) {
22 ; CHECK-LABEL: @test_simplify1(
23 %retval = call float @powf(float 1.0, float %x)
25 ; CHECK-NEXT: ret float 1.000000e+00
28 define double @test_simplify2(double %x) {
29 ; CHECK-LABEL: @test_simplify2(
30 %retval = call double @pow(double 1.0, double %x)
32 ; CHECK-NEXT: ret double 1.000000e+00
35 ; Check pow(2.0, x) -> exp2(x).
37 define float @test_simplify3(float %x) {
38 ; CHECK-LABEL: @test_simplify3(
39 %retval = call float @powf(float 2.0, float %x)
40 ; CHECK-NEXT: [[EXP2F:%[a-z0-9]+]] = call float @exp2f(float %x) [[NUW_RO:#[0-9]+]]
42 ; CHECK-NEXT: ret float [[EXP2F]]
45 define double @test_simplify4(double %x) {
46 ; CHECK-LABEL: @test_simplify4(
47 %retval = call double @pow(double 2.0, double %x)
48 ; CHECK-NEXT: [[EXP2:%[a-z0-9]+]] = call double @exp2(double %x) [[NUW_RO]]
50 ; CHECK-NEXT: ret double [[EXP2]]
53 ; Check pow(x, 0.0) -> 1.0.
55 define float @test_simplify5(float %x) {
56 ; CHECK-LABEL: @test_simplify5(
57 %retval = call float @powf(float %x, float 0.0)
59 ; CHECK-NEXT: ret float 1.000000e+00
62 define double @test_simplify6(double %x) {
63 ; CHECK-LABEL: @test_simplify6(
64 %retval = call double @pow(double %x, double 0.0)
66 ; CHECK-NEXT: ret double 1.000000e+00
69 ; Check pow(x, 0.5) -> fabs(sqrt(x)), where x != -infinity.
71 define float @test_simplify7(float %x) {
72 ; CHECK-LABEL: @test_simplify7(
73 %retval = call float @powf(float %x, float 0.5)
74 ; CHECK-NEXT: [[SQRTF:%[a-z0-9]+]] = call float @sqrtf(float %x) [[NUW_RO]]
75 ; CHECK-NEXT: [[FABSF:%[a-z0-9]+]] = call float @fabsf(float [[SQRTF]]) [[NUW_RO]]
76 ; CHECK-NEXT: [[FCMP:%[a-z0-9]+]] = fcmp oeq float %x, 0xFFF0000000000000
77 ; CHECK-NEXT: [[SELECT:%[a-z0-9]+]] = select i1 [[FCMP]], float 0x7FF0000000000000, float [[FABSF]]
79 ; CHECK-NEXT: ret float [[SELECT]]
82 define double @test_simplify8(double %x) {
83 ; CHECK-LABEL: @test_simplify8(
84 %retval = call double @pow(double %x, double 0.5)
85 ; CHECK-NEXT: [[SQRT:%[a-z0-9]+]] = call double @sqrt(double %x) [[NUW_RO]]
86 ; CHECK-NEXT: [[FABS:%[a-z0-9]+]] = call double @fabs(double [[SQRT]]) [[NUW_RO]]
87 ; CHECK-NEXT: [[FCMP:%[a-z0-9]+]] = fcmp oeq double %x, 0xFFF0000000000000
88 ; CHECK-NEXT: [[SELECT:%[a-z0-9]+]] = select i1 [[FCMP]], double 0x7FF0000000000000, double [[FABS]]
90 ; CHECK-NEXT: ret double [[SELECT]]
93 ; Check pow(-infinity, 0.5) -> +infinity.
95 define float @test_simplify9(float %x) {
96 ; CHECK-LABEL: @test_simplify9(
97 %retval = call float @powf(float 0xFFF0000000000000, float 0.5)
99 ; CHECK-NEXT: ret float 0x7FF0000000000000
102 define double @test_simplify10(double %x) {
103 ; CHECK-LABEL: @test_simplify10(
104 %retval = call double @pow(double 0xFFF0000000000000, double 0.5)
106 ; CHECK-NEXT: ret double 0x7FF0000000000000
109 ; Check pow(x, 1.0) -> x.
111 define float @test_simplify11(float %x) {
112 ; CHECK-LABEL: @test_simplify11(
113 %retval = call float @powf(float %x, float 1.0)
115 ; CHECK-NEXT: ret float %x
118 define double @test_simplify12(double %x) {
119 ; CHECK-LABEL: @test_simplify12(
120 %retval = call double @pow(double %x, double 1.0)
122 ; CHECK-NEXT: ret double %x
125 ; Check pow(x, 2.0) -> x*x.
127 define float @test_simplify13(float %x) {
128 ; CHECK-LABEL: @test_simplify13(
129 %retval = call float @powf(float %x, float 2.0)
130 ; CHECK-NEXT: [[SQUARE:%[a-z0-9]+]] = fmul float %x, %x
132 ; CHECK-NEXT: ret float [[SQUARE]]
135 define double @test_simplify14(double %x) {
136 ; CHECK-LABEL: @test_simplify14(
137 %retval = call double @pow(double %x, double 2.0)
138 ; CHECK-NEXT: [[SQUARE:%[a-z0-9]+]] = fmul double %x, %x
140 ; CHECK-NEXT: ret double [[SQUARE]]
143 ; Check pow(x, -1.0) -> 1.0/x.
145 define float @test_simplify15(float %x) {
146 ; CHECK-LABEL: @test_simplify15(
147 %retval = call float @powf(float %x, float -1.0)
148 ; CHECK-NEXT: [[RECIPROCAL:%[a-z0-9]+]] = fdiv float 1.000000e+00, %x
150 ; CHECK-NEXT: ret float [[RECIPROCAL]]
153 define double @test_simplify16(double %x) {
154 ; CHECK-LABEL: @test_simplify16(
155 %retval = call double @pow(double %x, double -1.0)
156 ; CHECK-NEXT: [[RECIPROCAL:%[a-z0-9]+]] = fdiv double 1.000000e+00, %x
158 ; CHECK-NEXT: ret double [[RECIPROCAL]]
161 declare double @llvm.pow.f64(double %Val, double %Power)
162 define double @test_simplify17(double %x) {
163 ; CHECK-LABEL: @test_simplify17(
164 %retval = call double @llvm.pow.f64(double %x, double 0.5)
165 ; CHECK-NEXT: [[SQRT:%[a-z0-9]+]] = call double @sqrt(double %x)
166 ; CHECK-NEXT: [[FABS:%[a-z0-9]+]] = call double @fabs(double [[SQRT]])
167 ; CHECK-NEXT: [[FCMP:%[a-z0-9]+]] = fcmp oeq double %x, 0xFFF0000000000000
168 ; CHECK-NEXT: [[SELECT:%[a-z0-9]+]] = select i1 [[FCMP]], double 0x7FF0000000000000, double [[FABS]]
170 ; CHECK-NEXT: ret double [[SELECT]]
173 ; Check pow(10.0, x) -> __exp10(x) on OS X 10.9+ and iOS 7.0+.
175 define float @test_simplify18(float %x) {
176 ; CHECK-LABEL: @test_simplify18(
177 %retval = call float @powf(float 10.0, float %x)
178 ; CHECK-EXP10: [[EXP10F:%[_a-z0-9]+]] = call float @__exp10f(float %x) [[NUW_RO:#[0-9]+]]
180 ; CHECK-EXP10: ret float [[EXP10F]]
181 ; CHECK-NO-EXP10: call float @powf
184 define double @test_simplify19(double %x) {
185 ; CHECK-LABEL: @test_simplify19(
186 %retval = call double @pow(double 10.0, double %x)
187 ; CHECK-EXP10: [[EXP10:%[_a-z0-9]+]] = call double @__exp10(double %x) [[NUW_RO]]
189 ; CHECK-EXP10: ret double [[EXP10]]
190 ; CHECK-NO-EXP10: call double @pow
193 ; CHECK: attributes [[NUW_RO]] = { nounwind readonly }