1; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mcpu=x86-64 | FileCheck %s
2
3; More than one 'arcp' division using a single divisor operand
4; should be converted into a reciprocal and multiplication.
5
6; Don't do anything for just one division.
7
8define float @div1_arcp(float %x, float %y, float %z) {
9; CHECK-LABEL: div1_arcp:
10; CHECK:       # BB#0:
11; CHECK-NEXT:    divss %xmm1, %xmm0
12; CHECK-NEXT:    retq
13  %div1 = fdiv arcp float %x, %y
14  ret float %div1
15}
16
17; All math instructions are 'arcp', so optimize.
18
19define float @div2_arcp_all(float %x, float %y, float %z) {
20; CHECK-LABEL: div2_arcp_all:
21; CHECK:       # BB#0:
22; CHECK-NEXT:    movss {{.*#+}} xmm3 = mem[0],zero,zero,zero
23; CHECK-NEXT:    divss %xmm2, %xmm3
24; CHECK-NEXT:    mulss %xmm3, %xmm0
25; CHECK-NEXT:    mulss %xmm1, %xmm0
26; CHECK-NEXT:    mulss %xmm3, %xmm0
27; CHECK-NEXT:    retq
28  %div1 = fdiv arcp float %x, %z
29  %mul = fmul arcp float %div1, %y
30  %div2 = fdiv arcp float %mul, %z
31  ret float %div2
32}
33
34; The first division is not 'arcp', so do not optimize.
35
36define float @div2_arcp_partial1(float %x, float %y, float %z) {
37; CHECK-LABEL: div2_arcp_partial1:
38; CHECK:       # BB#0:
39; CHECK-NEXT:    divss %xmm2, %xmm0
40; CHECK-NEXT:    mulss %xmm1, %xmm0
41; CHECK-NEXT:    divss %xmm2, %xmm0
42; CHECK-NEXT:    retq
43  %div1 = fdiv float %x, %z
44  %mul = fmul arcp float %div1, %y
45  %div2 = fdiv arcp float %mul, %z
46  ret float %div2
47}
48
49; The second division is not 'arcp', so do not optimize.
50
51define float @div2_arcp_partial2(float %x, float %y, float %z) {
52; CHECK-LABEL: div2_arcp_partial2:
53; CHECK:       # BB#0:
54; CHECK-NEXT:    divss %xmm2, %xmm0
55; CHECK-NEXT:    mulss %xmm1, %xmm0
56; CHECK-NEXT:    divss %xmm2, %xmm0
57; CHECK-NEXT:    retq
58  %div1 = fdiv arcp float %x, %z
59  %mul = fmul arcp float %div1, %y
60  %div2 = fdiv float %mul, %z
61  ret float %div2
62}
63
64; The multiply is not 'arcp', but that does not prevent optimizing the divisions.
65
66define float @div2_arcp_partial3(float %x, float %y, float %z) {
67; CHECK-LABEL: div2_arcp_partial3:
68; CHECK:       # BB#0:
69; CHECK-NEXT:    movss {{.*#+}} xmm3 = mem[0],zero,zero,zero
70; CHECK-NEXT:    divss %xmm2, %xmm3
71; CHECK-NEXT:    mulss %xmm3, %xmm0
72; CHECK-NEXT:    mulss %xmm1, %xmm0
73; CHECK-NEXT:    mulss %xmm3, %xmm0
74; CHECK-NEXT:    retq
75  %div1 = fdiv arcp float %x, %z
76  %mul = fmul float %div1, %y
77  %div2 = fdiv arcp float %mul, %z
78  ret float %div2
79}
80
81; If the reciprocal is already calculated, we should not
82; generate an extra multiplication by 1.0.
83
84define double @div3_arcp(double %x, double %y, double %z) {
85; CHECK-LABEL: div3_arcp:
86; CHECK:       # BB#0:
87; CHECK-NEXT:    movsd{{.*#+}} xmm2 = mem[0],zero
88; CHECK-NEXT:    divsd %xmm1, %xmm2
89; CHECK-NEXT:    mulsd %xmm2, %xmm0
90; CHECK-NEXT:    addsd %xmm2, %xmm0
91; CHECK-NEXT:    retq
92  %div1 = fdiv fast double 1.0, %y
93  %div2 = fdiv fast double %x, %y
94  %ret = fadd fast double %div2, %div1
95  ret double %ret
96}
97
98define void @PR24141() {
99; CHECK-LABEL: PR24141:
100; CHECK:	callq
101; CHECK-NEXT:	divsd
102; CHECK-NEXT:	jmp
103entry:
104  br label %while.body
105
106while.body:
107  %x.0 = phi double [ undef, %entry ], [ %div, %while.body ]
108  %call = call { double, double } @g(double %x.0)
109  %xv0 = extractvalue { double, double } %call, 0
110  %xv1 = extractvalue { double, double } %call, 1
111  %div = fdiv arcp double %xv0, %xv1
112  br label %while.body
113}
114
115declare { double, double } @g(double)
116
117