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 8 define 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 19 define 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 36 define 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 51 define 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 66 define 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 84 define 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 98 define float @div_select_constant_fold(i1 zeroext %arg) { 99 ; CHECK-LABEL: div_select_constant_fold: 100 ; CHECK: # %bb.0: 101 ; CHECK-NEXT: testl %edi, %edi 102 ; CHECK-NEXT: jne .LBB6_1 103 ; CHECK-NEXT: # %bb.2: 104 ; CHECK-NEXT: movss {{.*#+}} xmm0 = mem[0],zero,zero,zero 105 ; CHECK-NEXT: retq 106 ; CHECK-NEXT: .LBB6_1: 107 ; CHECK-NEXT: movss {{.*#+}} xmm0 = mem[0],zero,zero,zero 108 ; CHECK-NEXT: retq 109 %tmp = select i1 %arg, float 5.000000e+00, float 6.000000e+00 110 %B2 = fdiv float %tmp, 1.000000e+00 111 ret float %B2 112 } 113 114 define float @div_select_constant_fold_zero(i1 zeroext %arg) { 115 ; CHECK-LABEL: div_select_constant_fold_zero: 116 ; CHECK: # %bb.0: 117 ; CHECK-NEXT: testl %edi, %edi 118 ; CHECK-NEXT: jne .LBB7_1 119 ; CHECK-NEXT: # %bb.2: 120 ; CHECK-NEXT: movss {{.*#+}} xmm0 = mem[0],zero,zero,zero 121 ; CHECK-NEXT: jmp .LBB7_3 122 ; CHECK-NEXT: .LBB7_1: 123 ; CHECK-NEXT: movss {{.*#+}} xmm0 = mem[0],zero,zero,zero 124 ; CHECK-NEXT: .LBB7_3: 125 ; CHECK-NEXT: xorps %xmm1, %xmm1 126 ; CHECK-NEXT: divss %xmm1, %xmm0 127 ; CHECK-NEXT: retq 128 %tmp = select i1 %arg, float 5.000000e+00, float 6.000000e+00 129 %B2 = fdiv float %tmp, 0.000000e+00 130 ret float %B2 131 } 132 133 define void @PR24141() { 134 ; CHECK-LABEL: PR24141: 135 ; CHECK: callq 136 ; CHECK-NEXT: divsd 137 ; CHECK-NEXT: jmp 138 entry: 139 br label %while.body 140 141 while.body: 142 %x.0 = phi double [ undef, %entry ], [ %div, %while.body ] 143 %call = call { double, double } @g(double %x.0) 144 %xv0 = extractvalue { double, double } %call, 0 145 %xv1 = extractvalue { double, double } %call, 1 146 %div = fdiv arcp double %xv0, %xv1 147 br label %while.body 148 } 149 150 declare { double, double } @g(double) 151 152