Allow AVX vrsqrtps generation.
[oota-llvm.git] / test / CodeGen / X86 / sqrt-fastmath.ll
1 ; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mcpu=core2 | FileCheck %s
2 ; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mcpu=btver2 | FileCheck %s --check-prefix=BTVER2
3
4 ; generated using "clang -S -O2 -ffast-math -emit-llvm sqrt.c" from
5 ; #include <math.h>
6
7 ; double fd(double d){
8 ;   return sqrt(d);
9 ; }
10
11 ; float ff(float f){
12 ;   return sqrtf(f);
13 ; }
14
15 ; long double fld(long double ld){
16 ;   return sqrtl(ld);
17 ; }
18 ;
19 ; Tests conversion of sqrt function calls into sqrt instructions when
20 ; -ffast-math is in effect.
21
22 ; ModuleID = 'sqrt.c'
23 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
24 target triple = "x86_64-unknown-linux-gnu"
25
26 ; Function Attrs: nounwind readnone uwtable
27 define double @fd(double %d) #0 {
28 entry:
29 ; CHECK: sqrtsd
30   %call = tail call double @__sqrt_finite(double %d) #2
31   ret double %call
32 }
33
34 ; Function Attrs: nounwind readnone
35 declare double @__sqrt_finite(double) #1
36
37 ; Function Attrs: nounwind readnone uwtable
38 define float @ff(float %f) #0 {
39 entry:
40 ; CHECK: sqrtss
41   %call = tail call float @__sqrtf_finite(float %f) #2
42   ret float %call
43 }
44
45 ; Function Attrs: nounwind readnone
46 declare float @__sqrtf_finite(float) #1
47
48 ; Function Attrs: nounwind readnone uwtable
49 define x86_fp80 @fld(x86_fp80 %ld) #0 {
50 entry:
51 ; CHECK: fsqrt
52   %call = tail call x86_fp80 @__sqrtl_finite(x86_fp80 %ld) #2
53   ret x86_fp80 %call
54 }
55
56 declare x86_fp80 @__sqrtl_finite(x86_fp80) #1
57
58 declare float @llvm.sqrt.f32(float) #1
59 declare <4 x float> @llvm.sqrt.v4f32(<4 x float>) #1
60 declare <8 x float> @llvm.sqrt.v8f32(<8 x float>) #1
61
62 ; If the target's sqrtss and divss instructions are substantially
63 ; slower than rsqrtss with a Newton-Raphson refinement, we should
64 ; generate the estimate sequence.
65
66 define float @reciprocal_square_root(float %x) #0 {
67   %sqrt = tail call float @llvm.sqrt.f32(float %x)
68   %div = fdiv fast float 1.0, %sqrt
69   ret float %div
70
71 ; CHECK-LABEL: reciprocal_square_root:
72 ; CHECK: sqrtss
73 ; CHECK-NEXT: movss
74 ; CHECK-NEXT: divss
75 ; CHECK-NEXT: retq
76 ; BTVER2-LABEL: reciprocal_square_root:
77 ; BTVER2: vrsqrtss
78 ; BTVER2-NEXT: vmulss
79 ; BTVER2-NEXT: vmulss
80 ; BTVER2-NEXT: vmulss
81 ; BTVER2-NEXT: vaddss
82 ; BTVER2-NEXT: vmulss
83 ; BTVER2-NEXT: retq
84 }
85
86 define <4 x float> @reciprocal_square_root_v4f32(<4 x float> %x) #0 {
87   %sqrt = tail call <4 x float> @llvm.sqrt.v4f32(<4 x float> %x)
88   %div = fdiv fast <4 x float> <float 1.0, float 1.0, float 1.0, float 1.0>, %sqrt
89   ret <4 x float> %div
90
91 ; CHECK-LABEL: reciprocal_square_root_v4f32:
92 ; CHECK: sqrtps
93 ; CHECK-NEXT: movaps
94 ; CHECK-NEXT: divps
95 ; CHECK-NEXT: retq
96 ; BTVER2-LABEL: reciprocal_square_root_v4f32:
97 ; BTVER2: vrsqrtps
98 ; BTVER2-NEXT: vmulps
99 ; BTVER2-NEXT: vmulps
100 ; BTVER2-NEXT: vmulps
101 ; BTVER2-NEXT: vaddps
102 ; BTVER2-NEXT: vmulps
103 ; BTVER2-NEXT: retq
104 }
105
106 define <8 x float> @reciprocal_square_root_v8f32(<8 x float> %x) #0 {
107   %sqrt = tail call <8 x float> @llvm.sqrt.v8f32(<8 x float> %x)
108   %div = fdiv fast <8 x float> <float 1.0, float 1.0, float 1.0, float 1.0, float 1.0, float 1.0, float 1.0, float 1.0>, %sqrt
109   ret <8 x float> %div
110
111 ; CHECK-LABEL: reciprocal_square_root_v8f32:
112 ; CHECK: sqrtps
113 ; CHECK-NEXT: sqrtps
114 ; CHECK-NEXT: movaps
115 ; CHECK-NEXT: movaps
116 ; CHECK-NEXT: divps
117 ; CHECK-NEXT: divps
118 ; CHECK-NEXT: retq
119 ; BTVER2-LABEL: reciprocal_square_root_v8f32:
120 ; BTVER2: vrsqrtps
121 ; BTVER2-NEXT: vmulps
122 ; BTVER2-NEXT: vmulps
123 ; BTVER2-NEXT: vmulps
124 ; BTVER2-NEXT: vaddps
125 ; BTVER2-NEXT: vmulps
126 ; BTVER2-NEXT: retq
127 }
128
129
130 attributes #0 = { nounwind readnone uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "unsafe-fp-math"="true" "use-soft-float"="false" }
131 attributes #1 = { nounwind readnone "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "unsafe-fp-math"="true" "use-soft-float"="false" }
132 attributes #2 = { nounwind readnone }