File: sqrt.ll

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (214 lines) | stat: -rw-r--r-- 7,469 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -S -passes=instcombine < %s | FileCheck %s

define float @test1(float %x) nounwind readnone ssp {
; CHECK-LABEL: @test1(
; CHECK-NEXT:    [[SQRTF:%.*]] = call float @sqrtf(float [[X:%.*]]) #[[ATTR4:[0-9]+]]
; CHECK-NEXT:    ret float [[SQRTF]]
;
  %conv = fpext float %x to double
  %call = tail call double @sqrt(double %conv) readnone nounwind
  %conv1 = fptrunc double %call to float
  ret float %conv1
}

; PR8096

define float @test2(float %x) nounwind readnone ssp {
; CHECK-LABEL: @test2(
; CHECK-NEXT:    [[SQRTF:%.*]] = call float @sqrtf(float [[X:%.*]]) #[[ATTR4]]
; CHECK-NEXT:    ret float [[SQRTF]]
;
  %conv = fpext float %x to double
  %call = tail call double @sqrt(double %conv) nounwind
  %conv1 = fptrunc double %call to float
  ret float %conv1
}

; rdar://9763193
; Can't fold (fptrunc (sqrt (fpext x))) -> (sqrtf x) since there is another
; use of sqrt result.

define float @test3(ptr %v) nounwind uwtable ssp {
; CHECK-LABEL: @test3(
; CHECK-NEXT:    [[CALL34:%.*]] = call double @sqrt(double 0x7FF8000000000000) #[[ATTR4]]
; CHECK-NEXT:    [[CALL36:%.*]] = call i32 @foo(double [[CALL34]]) #[[ATTR5:[0-9]+]]
; CHECK-NEXT:    [[CONV38:%.*]] = fptrunc double [[CALL34]] to float
; CHECK-NEXT:    ret float [[CONV38]]
;
  %arrayidx13 = getelementptr inbounds float, ptr %v, i64 2
  %tmp14 = load float, ptr %arrayidx13
  %mul18 = fmul float %tmp14, %tmp14
  %add19 = fadd float undef, %mul18
  %conv = fpext float %add19 to double
  %call34 = call double @sqrt(double %conv) readnone
  %call36 = call i32 (double) @foo(double %call34) nounwind
  %conv38 = fptrunc double %call34 to float
  ret float %conv38
}

; PR43347 - https://bugs.llvm.org/show_bug.cgi?id=43347

define void @0(float %f) {
; CHECK-LABEL: @0(
; CHECK-NEXT:    [[SQRTF:%.*]] = call float @sqrtf(float [[F:%.*]]) #[[ATTR2:[0-9]+]]
; CHECK-NEXT:    ret void
;
  %d = fpext float %f to double
  %r = call double @sqrt(double %d)
  ret void
}

define float @sqrt_call_nnan_f32(float %x) {
; CHECK-LABEL: @sqrt_call_nnan_f32(
; CHECK-NEXT:    [[SQRT:%.*]] = call nnan float @sqrtf(float [[X:%.*]])
; CHECK-NEXT:    ret float [[SQRT]]
;
  %sqrt = call nnan float @sqrtf(float %x)
  ret float %sqrt
}

define double @sqrt_call_nnan_f64(double %x) {
; CHECK-LABEL: @sqrt_call_nnan_f64(
; CHECK-NEXT:    [[SQRT:%.*]] = tail call nnan ninf double @sqrt(double [[X:%.*]])
; CHECK-NEXT:    ret double [[SQRT]]
;
  %sqrt = tail call nnan ninf double @sqrt(double %x)
  ret double %sqrt
}

define float @sqrt_call_fabs_f32(float %x) {
; CHECK-LABEL: @sqrt_call_fabs_f32(
; CHECK-NEXT:    [[A:%.*]] = call float @llvm.fabs.f32(float [[X:%.*]])
; CHECK-NEXT:    [[SQRT:%.*]] = tail call float @sqrtf(float [[A]])
; CHECK-NEXT:    ret float [[SQRT]]
;
  %a = call float @llvm.fabs.f32(float %x)
  %sqrt = tail call float @sqrtf(float %a)
  ret float %sqrt
}

define double @sqrt_exp(double %x) {
; CHECK-LABEL: @sqrt_exp(
; CHECK-NEXT:    [[MERGED_SQRT:%.*]] = fmul reassoc double [[X:%.*]], 5.000000e-01
; CHECK-NEXT:    [[E:%.*]] = call reassoc double @llvm.exp.f64(double [[MERGED_SQRT]])
; CHECK-NEXT:    ret double [[E]]
;
  %e = call reassoc double @llvm.exp.f64(double %x)
  %res = call reassoc double @llvm.sqrt.f64(double %e)
  ret double %res
}

define double @sqrt_exp_2(double %x) {
; CHECK-LABEL: @sqrt_exp_2(
; CHECK-NEXT:    [[MERGED_SQRT:%.*]] = fmul reassoc double [[X:%.*]], 5.000000e-01
; CHECK-NEXT:    [[E:%.*]] = call reassoc double @exp(double [[MERGED_SQRT]])
; CHECK-NEXT:    ret double [[E]]
;
  %e = call reassoc double @exp(double %x)
  %res = call reassoc double @sqrt(double %e)
  ret double %res
}

define double @sqrt_exp2(double %x) {
; CHECK-LABEL: @sqrt_exp2(
; CHECK-NEXT:    [[MERGED_SQRT:%.*]] = fmul reassoc double [[X:%.*]], 5.000000e-01
; CHECK-NEXT:    [[E:%.*]] = call reassoc double @exp2(double [[MERGED_SQRT]])
; CHECK-NEXT:    ret double [[E]]
;
  %e = call reassoc double @exp2(double %x)
  %res = call reassoc double @sqrt(double %e)
  ret double %res
}

define double @sqrt_exp10(double %x) {
; CHECK-LABEL: @sqrt_exp10(
; CHECK-NEXT:    [[MERGED_SQRT:%.*]] = fmul reassoc double [[X:%.*]], 5.000000e-01
; CHECK-NEXT:    [[E:%.*]] = call reassoc double @exp10(double [[MERGED_SQRT]])
; CHECK-NEXT:    ret double [[E]]
;
  %e = call reassoc double @exp10(double %x)
  %res = call reassoc double @sqrt(double %e)
  ret double %res
}

; Negative test
define double @sqrt_exp_nofast_1(double %x) {
; CHECK-LABEL: @sqrt_exp_nofast_1(
; CHECK-NEXT:    [[E:%.*]] = call double @llvm.exp.f64(double [[X:%.*]])
; CHECK-NEXT:    [[RES:%.*]] = call reassoc double @llvm.sqrt.f64(double [[E]])
; CHECK-NEXT:    ret double [[RES]]
;
  %e = call double @llvm.exp.f64(double %x)
  %res = call reassoc double @llvm.sqrt.f64(double %e)
  ret double %res
}

; Negative test
define double @sqrt_exp_nofast_2(double %x) {
; CHECK-LABEL: @sqrt_exp_nofast_2(
; CHECK-NEXT:    [[E:%.*]] = call reassoc double @llvm.exp.f64(double [[X:%.*]])
; CHECK-NEXT:    [[RES:%.*]] = call double @llvm.sqrt.f64(double [[E]])
; CHECK-NEXT:    ret double [[RES]]
;
  %e = call reassoc double @llvm.exp.f64(double %x)
  %res = call double @llvm.sqrt.f64(double %e)
  ret double %res
}

define double @sqrt_exp_merge_constant(double %x, double %y) {
; CHECK-LABEL: @sqrt_exp_merge_constant(
; CHECK-NEXT:    [[MERGED_SQRT:%.*]] = fmul reassoc nsz double [[X:%.*]], 5.000000e+00
; CHECK-NEXT:    [[E:%.*]] = call reassoc double @llvm.exp.f64(double [[MERGED_SQRT]])
; CHECK-NEXT:    ret double [[E]]
;
  %mul = fmul reassoc nsz double %x, 10.0
  %e = call reassoc double @llvm.exp.f64(double %mul)
  %res = call reassoc nsz double @llvm.sqrt.f64(double %e)
  ret double %res
}

define double @sqrt_exp_intr_and_libcall(double %x) {
; CHECK-LABEL: @sqrt_exp_intr_and_libcall(
; CHECK-NEXT:    [[MERGED_SQRT:%.*]] = fmul reassoc double [[X:%.*]], 5.000000e-01
; CHECK-NEXT:    [[E:%.*]] = call reassoc double @exp(double [[MERGED_SQRT]])
; CHECK-NEXT:    ret double [[E]]
;
  %e = call reassoc double @exp(double %x)
  %res = call reassoc double @llvm.sqrt.f64(double %e)
  ret double %res
}

define double @sqrt_exp_intr_and_libcall_2(double %x) {
; CHECK-LABEL: @sqrt_exp_intr_and_libcall_2(
; CHECK-NEXT:    [[MERGED_SQRT:%.*]] = fmul reassoc double [[X:%.*]], 5.000000e-01
; CHECK-NEXT:    [[E:%.*]] = call reassoc double @llvm.exp.f64(double [[MERGED_SQRT]])
; CHECK-NEXT:    ret double [[E]]
;
  %e = call reassoc double @llvm.exp.f64(double %x)
  %res = call reassoc double @sqrt(double %e)
  ret double %res
}

define <2 x float> @sqrt_exp_vec(<2 x float> %x) {
; CHECK-LABEL: @sqrt_exp_vec(
; CHECK-NEXT:    [[MERGED_SQRT:%.*]] = fmul reassoc <2 x float> [[X:%.*]], <float 5.000000e-01, float 5.000000e-01>
; CHECK-NEXT:    [[E:%.*]] = call reassoc <2 x float> @llvm.exp.v2f32(<2 x float> [[MERGED_SQRT]])
; CHECK-NEXT:    ret <2 x float> [[E]]
;
  %e = call reassoc <2 x float> @llvm.exp.v2f32(<2 x float> %x)
  %res = call reassoc <2 x float> @llvm.sqrt.v2f32(<2 x float> %e)
  ret <2 x float> %res
}

declare i32 @foo(double)
declare double @sqrt(double) readnone
declare float @sqrtf(float)
declare float @llvm.fabs.f32(float)
declare double @llvm.exp.f64(double)
declare double @llvm.sqrt.f64(double)
declare double @exp(double)
declare double @exp2(double)
declare double @exp10(double)
declare <2 x float> @llvm.exp.v2f32(<2 x float>)
declare <2 x float> @llvm.sqrt.v2f32(<2 x float>)