1 ; Test that the pow library call simplifier works correctly.
3 ; RUN: opt < %s -instcombine -S | FileCheck %s
6 ; NOTE: The readonly attribute on the pow call should be preserved
7 ; in the cases below where pow is transformed into another function call.
9 declare float @powf(float, float) nounwind readonly
10 declare double @pow(double, double) nounwind readonly
12 ; Check pow(1.0, x) -> 1.0.
14 define float @test_simplify1(float %x) {
15 ; CHECK-LABEL: @test_simplify1(
16 %retval = call float @powf(float 1.0, float %x)
18 ; CHECK-NEXT: ret float 1.000000e+00
21 define double @test_simplify2(double %x) {
22 ; CHECK-LABEL: @test_simplify2(
23 %retval = call double @pow(double 1.0, double %x)
25 ; CHECK-NEXT: ret double 1.000000e+00
28 ; Check pow(2.0, x) -> exp2(x).
30 define float @test_simplify3(float %x) {
31 ; CHECK-LABEL: @test_simplify3(
32 %retval = call float @powf(float 2.0, float %x)
33 ; CHECK-NEXT: [[EXP2F:%[a-z0-9]+]] = call float @exp2f(float %x) [[NUW_RO:#[0-9]+]]
35 ; CHECK-NEXT: ret float [[EXP2F]]
38 define double @test_simplify4(double %x) {
39 ; CHECK-LABEL: @test_simplify4(
40 %retval = call double @pow(double 2.0, double %x)
41 ; CHECK-NEXT: [[EXP2:%[a-z0-9]+]] = call double @exp2(double %x) [[NUW_RO]]
43 ; CHECK-NEXT: ret double [[EXP2]]
46 ; Check pow(x, 0.0) -> 1.0.
48 define float @test_simplify5(float %x) {
49 ; CHECK-LABEL: @test_simplify5(
50 %retval = call float @powf(float %x, float 0.0)
52 ; CHECK-NEXT: ret float 1.000000e+00
55 define double @test_simplify6(double %x) {
56 ; CHECK-LABEL: @test_simplify6(
57 %retval = call double @pow(double %x, double 0.0)
59 ; CHECK-NEXT: ret double 1.000000e+00
62 ; Check pow(x, 0.5) -> fabs(sqrt(x)), where x != -infinity.
64 define float @test_simplify7(float %x) {
65 ; CHECK-LABEL: @test_simplify7(
66 %retval = call float @powf(float %x, float 0.5)
67 ; CHECK-NEXT: [[SQRTF:%[a-z0-9]+]] = call float @sqrtf(float %x) [[NUW_RO]]
68 ; CHECK-NEXT: [[FABSF:%[a-z0-9]+]] = call float @fabsf(float [[SQRTF]]) [[NUW_RO]]
69 ; CHECK-NEXT: [[FCMP:%[a-z0-9]+]] = fcmp oeq float %x, 0xFFF0000000000000
70 ; CHECK-NEXT: [[SELECT:%[a-z0-9]+]] = select i1 [[FCMP]], float 0x7FF0000000000000, float [[FABSF]]
72 ; CHECK-NEXT: ret float [[SELECT]]
75 define double @test_simplify8(double %x) {
76 ; CHECK-LABEL: @test_simplify8(
77 %retval = call double @pow(double %x, double 0.5)
78 ; CHECK-NEXT: [[SQRT:%[a-z0-9]+]] = call double @sqrt(double %x) [[NUW_RO]]
79 ; CHECK-NEXT: [[FABS:%[a-z0-9]+]] = call double @fabs(double [[SQRT]]) [[NUW_RO]]
80 ; CHECK-NEXT: [[FCMP:%[a-z0-9]+]] = fcmp oeq double %x, 0xFFF0000000000000
81 ; CHECK-NEXT: [[SELECT:%[a-z0-9]+]] = select i1 [[FCMP]], double 0x7FF0000000000000, double [[FABS]]
83 ; CHECK-NEXT: ret double [[SELECT]]
86 ; Check pow(-infinity, 0.5) -> +infinity.
88 define float @test_simplify9(float %x) {
89 ; CHECK-LABEL: @test_simplify9(
90 %retval = call float @powf(float 0xFFF0000000000000, float 0.5)
92 ; CHECK-NEXT: ret float 0x7FF0000000000000
95 define double @test_simplify10(double %x) {
96 ; CHECK-LABEL: @test_simplify10(
97 %retval = call double @pow(double 0xFFF0000000000000, double 0.5)
99 ; CHECK-NEXT: ret double 0x7FF0000000000000
102 ; Check pow(x, 1.0) -> x.
104 define float @test_simplify11(float %x) {
105 ; CHECK-LABEL: @test_simplify11(
106 %retval = call float @powf(float %x, float 1.0)
108 ; CHECK-NEXT: ret float %x
111 define double @test_simplify12(double %x) {
112 ; CHECK-LABEL: @test_simplify12(
113 %retval = call double @pow(double %x, double 1.0)
115 ; CHECK-NEXT: ret double %x
118 ; Check pow(x, 2.0) -> x*x.
120 define float @test_simplify13(float %x) {
121 ; CHECK-LABEL: @test_simplify13(
122 %retval = call float @powf(float %x, float 2.0)
123 ; CHECK-NEXT: [[SQUARE:%[a-z0-9]+]] = fmul float %x, %x
125 ; CHECK-NEXT: ret float [[SQUARE]]
128 define double @test_simplify14(double %x) {
129 ; CHECK-LABEL: @test_simplify14(
130 %retval = call double @pow(double %x, double 2.0)
131 ; CHECK-NEXT: [[SQUARE:%[a-z0-9]+]] = fmul double %x, %x
133 ; CHECK-NEXT: ret double [[SQUARE]]
136 ; Check pow(x, -1.0) -> 1.0/x.
138 define float @test_simplify15(float %x) {
139 ; CHECK-LABEL: @test_simplify15(
140 %retval = call float @powf(float %x, float -1.0)
141 ; CHECK-NEXT: [[RECIPROCAL:%[a-z0-9]+]] = fdiv float 1.000000e+00, %x
143 ; CHECK-NEXT: ret float [[RECIPROCAL]]
146 define double @test_simplify16(double %x) {
147 ; CHECK-LABEL: @test_simplify16(
148 %retval = call double @pow(double %x, double -1.0)
149 ; CHECK-NEXT: [[RECIPROCAL:%[a-z0-9]+]] = fdiv double 1.000000e+00, %x
151 ; CHECK-NEXT: ret double [[RECIPROCAL]]
154 declare double @llvm.pow.f64(double %Val, double %Power)
155 define double @test_simplify17(double %x) {
156 ; CHECK-LABEL: @test_simplify17(
157 %retval = call double @llvm.pow.f64(double %x, double 0.5)
158 ; CHECK-NEXT: [[SQRT:%[a-z0-9]+]] = call double @sqrt(double %x) [[NUW_RO]]
159 ; CHECK-NEXT: [[FABS:%[a-z0-9]+]] = call double @fabs(double [[SQRT]]) [[NUW_RO]]
160 ; CHECK-NEXT: [[FCMP:%[a-z0-9]+]] = fcmp oeq double %x, 0xFFF0000000000000
161 ; CHECK-NEXT: [[SELECT:%[a-z0-9]+]] = select i1 [[FCMP]], double 0x7FF0000000000000, double [[FABS]]
163 ; CHECK-NEXT: ret double [[SELECT]]
166 ; CHECK: attributes [[NUW_RO]] = { nounwind readonly }