1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
4 define i1 @test1(i8 %A) {
6 ; CHECK-NEXT: ret i1 true
8 %B = sitofp i8 %A to double
9 %C = fcmp ult double %B, 128.0
13 define i1 @test2(i8 %A) {
14 ; CHECK-LABEL: @test2(
15 ; CHECK-NEXT: ret i1 true
17 %B = sitofp i8 %A to double
18 %C = fcmp ugt double %B, -128.1
22 define i1 @test3(i8 %A) {
23 ; CHECK-LABEL: @test3(
24 ; CHECK-NEXT: ret i1 true
26 %B = sitofp i8 %A to double
27 %C = fcmp ule double %B, 127.0
31 define i1 @test4(i8 %A) {
32 ; CHECK-LABEL: @test4(
33 ; CHECK-NEXT: [[C:%.*]] = icmp ne i8 [[A:%.*]], 127
34 ; CHECK-NEXT: ret i1 [[C]]
36 %B = sitofp i8 %A to double
37 %C = fcmp ult double %B, 127.0
41 define i32 @test5(i32 %A) {
42 ; CHECK-LABEL: @test5(
43 ; CHECK-NEXT: ret i32 [[A:%.*]]
45 %B = sitofp i32 %A to double
46 %C = fptosi double %B to i32
47 %D = uitofp i32 %C to double
48 %E = fptoui double %D to i32
52 define i32 @test6(i32 %A) {
53 ; CHECK-LABEL: @test6(
54 ; CHECK-NEXT: [[ADDCONV:%.*]] = and i32 [[A:%.*]], 39
55 ; CHECK-NEXT: ret i32 [[ADDCONV]]
59 %D = sitofp i32 %B to double
60 %E = sitofp i32 %C to double
61 %F = fadd double %D, %E
62 %G = fptosi double %F to i32
66 define i32 @test7(i32 %A) {
67 ; CHECK-LABEL: @test7(
68 ; CHECK-NEXT: ret i32 [[A:%.*]]
70 %B = sitofp i32 %A to double
71 %C = fptoui double %B to i32
75 define i32 @test8(i32 %A) {
76 ; CHECK-LABEL: @test8(
77 ; CHECK-NEXT: ret i32 [[A:%.*]]
79 %B = uitofp i32 %A to double
80 %C = fptosi double %B to i32
84 define i32 @test9(i8 %A) {
85 ; CHECK-LABEL: @test9(
86 ; CHECK-NEXT: [[C:%.*]] = zext i8 [[A:%.*]] to i32
87 ; CHECK-NEXT: ret i32 [[C]]
89 %B = sitofp i8 %A to float
90 %C = fptoui float %B to i32
94 define i32 @test10(i8 %A) {
95 ; CHECK-LABEL: @test10(
96 ; CHECK-NEXT: [[C:%.*]] = sext i8 [[A:%.*]] to i32
97 ; CHECK-NEXT: ret i32 [[C]]
99 %B = sitofp i8 %A to float
100 %C = fptosi float %B to i32
104 ; If the input value is outside of the range of the output cast, it's
105 ; undefined behavior, so we can assume it fits.
107 define i8 @test11(i32 %A) {
108 ; CHECK-LABEL: @test11(
109 ; CHECK-NEXT: [[C:%.*]] = trunc i32 [[A:%.*]] to i8
110 ; CHECK-NEXT: ret i8 [[C]]
112 %B = sitofp i32 %A to float
113 %C = fptosi float %B to i8
117 ; If the input value is negative, it'll be outside the range of the
118 ; output cast, and thus undefined behavior.
120 define i32 @test12(i8 %A) {
121 ; CHECK-LABEL: @test12(
122 ; CHECK-NEXT: [[C:%.*]] = zext i8 [[A:%.*]] to i32
123 ; CHECK-NEXT: ret i32 [[C]]
125 %B = sitofp i8 %A to float
126 %C = fptoui float %B to i32
130 ; This can't fold because the 25-bit input doesn't fit in the mantissa.
132 define i32 @test13(i25 %A) {
133 ; CHECK-LABEL: @test13(
134 ; CHECK-NEXT: [[B:%.*]] = uitofp i25 [[A:%.*]] to float
135 ; CHECK-NEXT: [[C:%.*]] = fptoui float [[B]] to i32
136 ; CHECK-NEXT: ret i32 [[C]]
138 %B = uitofp i25 %A to float
139 %C = fptoui float %B to i32
145 define i32 @test14(i24 %A) {
146 ; CHECK-LABEL: @test14(
147 ; CHECK-NEXT: [[C:%.*]] = zext i24 [[A:%.*]] to i32
148 ; CHECK-NEXT: ret i32 [[C]]
150 %B = uitofp i24 %A to float
151 %C = fptoui float %B to i32
155 ; And this one can too.
157 define i24 @test15(i32 %A) {
158 ; CHECK-LABEL: @test15(
159 ; CHECK-NEXT: [[C:%.*]] = trunc i32 [[A:%.*]] to i24
160 ; CHECK-NEXT: ret i24 [[C]]
162 %B = uitofp i32 %A to float
163 %C = fptoui float %B to i24
167 ; This can fold because the 25-bit input is signed and we discard the sign bit.
169 define i32 @test16(i25 %A) {
170 ; CHECK-LABEL: @test16(
171 ; CHECK-NEXT: [[C:%.*]] = zext i25 [[A:%.*]] to i32
172 ; CHECK-NEXT: ret i32 [[C]]
174 %B = sitofp i25 %A to float
175 %C = fptoui float %B to i32
179 ; This can't fold because the 26-bit input won't fit the mantissa
180 ; even after discarding the signed bit.
182 define i32 @test17(i26 %A) {
183 ; CHECK-LABEL: @test17(
184 ; CHECK-NEXT: [[B:%.*]] = sitofp i26 [[A:%.*]] to float
185 ; CHECK-NEXT: [[C:%.*]] = fptoui float [[B]] to i32
186 ; CHECK-NEXT: ret i32 [[C]]
188 %B = sitofp i26 %A to float
189 %C = fptoui float %B to i32
193 ; This can't fold because the 54-bit output is big enough to hold an input
194 ; that was rounded when converted to double.
196 define i54 @test18(i64 %A) {
197 ; CHECK-LABEL: @test18(
198 ; CHECK-NEXT: [[B:%.*]] = sitofp i64 [[A:%.*]] to double
199 ; CHECK-NEXT: [[C:%.*]] = fptosi double [[B]] to i54
200 ; CHECK-NEXT: ret i54 [[C]]
202 %B = sitofp i64 %A to double
203 %C = fptosi double %B to i54
207 ; This can't fold because the 55-bit output won't fit the mantissa
208 ; even after discarding the sign bit.
210 define i55 @test19(i64 %A) {
211 ; CHECK-LABEL: @test19(
212 ; CHECK-NEXT: [[B:%.*]] = sitofp i64 [[A:%.*]] to double
213 ; CHECK-NEXT: [[C:%.*]] = fptosi double [[B]] to i55
214 ; CHECK-NEXT: ret i55 [[C]]
216 %B = sitofp i64 %A to double
217 %C = fptosi double %B to i55
221 ; TODO: The mask guarantees that the input is small enough to eliminate the FP casts.
223 define i25 @masked_input(i25 %A) {
224 ; CHECK-LABEL: @masked_input(
225 ; CHECK-NEXT: [[M:%.*]] = and i25 [[A:%.*]], 65535
226 ; CHECK-NEXT: [[B:%.*]] = uitofp i25 [[M]] to float
227 ; CHECK-NEXT: [[C:%.*]] = fptoui float [[B]] to i25
228 ; CHECK-NEXT: ret i25 [[C]]
230 %m = and i25 %A, 65535
231 %B = uitofp i25 %m to float
232 %C = fptoui float %B to i25
236 ; TODO: Clear the low bit - guarantees that the input is converted to FP without rounding.
238 define i25 @low_masked_input(i25 %A) {
239 ; CHECK-LABEL: @low_masked_input(
240 ; CHECK-NEXT: [[M:%.*]] = and i25 [[A:%.*]], -2
241 ; CHECK-NEXT: [[B:%.*]] = uitofp i25 [[M]] to float
242 ; CHECK-NEXT: [[C:%.*]] = fptoui float [[B]] to i25
243 ; CHECK-NEXT: ret i25 [[C]]
246 %B = uitofp i25 %m to float
247 %C = fptoui float %B to i25
251 ; Output is small enough to ensure exact cast (overflow produces poison).
253 define i11 @s32_half_s11(i32 %x) {
254 ; CHECK-LABEL: @s32_half_s11(
255 ; CHECK-NEXT: [[R:%.*]] = trunc i32 [[X:%.*]] to i11
256 ; CHECK-NEXT: ret i11 [[R]]
258 %h = sitofp i32 %x to half
259 %r = fptosi half %h to i11
263 ; Output is small enough to ensure exact cast (overflow produces poison).
265 define i11 @s32_half_u11(i32 %x) {
266 ; CHECK-LABEL: @s32_half_u11(
267 ; CHECK-NEXT: [[R:%.*]] = trunc i32 [[X:%.*]] to i11
268 ; CHECK-NEXT: ret i11 [[R]]
270 %h = sitofp i32 %x to half
271 %r = fptoui half %h to i11
275 ; Output is small enough to ensure exact cast (overflow produces poison).
277 define i11 @u32_half_s11(i32 %x) {
278 ; CHECK-LABEL: @u32_half_s11(
279 ; CHECK-NEXT: [[R:%.*]] = trunc i32 [[X:%.*]] to i11
280 ; CHECK-NEXT: ret i11 [[R]]
282 %h = uitofp i32 %x to half
283 %r = fptosi half %h to i11
287 ; Output is small enough to ensure exact cast (overflow produces poison).
289 define i11 @u32_half_u11(i32 %x) {
290 ; CHECK-LABEL: @u32_half_u11(
291 ; CHECK-NEXT: [[R:%.*]] = trunc i32 [[X:%.*]] to i11
292 ; CHECK-NEXT: ret i11 [[R]]
294 %h = uitofp i32 %x to half
295 %r = fptoui half %h to i11
299 ; Too many bits in output to ensure exact cast.
301 define i12 @s32_half_s12(i32 %x) {
302 ; CHECK-LABEL: @s32_half_s12(
303 ; CHECK-NEXT: [[H:%.*]] = sitofp i32 [[X:%.*]] to half
304 ; CHECK-NEXT: [[R:%.*]] = fptosi half [[H]] to i12
305 ; CHECK-NEXT: ret i12 [[R]]
307 %h = sitofp i32 %x to half
308 %r = fptosi half %h to i12
312 ; Too many bits in output to ensure exact cast.
314 define i12 @s32_half_u12(i32 %x) {
315 ; CHECK-LABEL: @s32_half_u12(
316 ; CHECK-NEXT: [[H:%.*]] = sitofp i32 [[X:%.*]] to half
317 ; CHECK-NEXT: [[R:%.*]] = fptoui half [[H]] to i12
318 ; CHECK-NEXT: ret i12 [[R]]
320 %h = sitofp i32 %x to half
321 %r = fptoui half %h to i12
325 ; TODO: This is safe to convert to trunc.
327 define i12 @u32_half_s12(i32 %x) {
328 ; CHECK-LABEL: @u32_half_s12(
329 ; CHECK-NEXT: [[H:%.*]] = uitofp i32 [[X:%.*]] to half
330 ; CHECK-NEXT: [[R:%.*]] = fptosi half [[H]] to i12
331 ; CHECK-NEXT: ret i12 [[R]]
333 %h = uitofp i32 %x to half
334 %r = fptosi half %h to i12
338 ; Too many bits in output to ensure exact cast.
340 define i12 @u32_half_u12(i32 %x) {
341 ; CHECK-LABEL: @u32_half_u12(
342 ; CHECK-NEXT: [[H:%.*]] = uitofp i32 [[X:%.*]] to half
343 ; CHECK-NEXT: [[R:%.*]] = fptoui half [[H]] to i12
344 ; CHECK-NEXT: ret i12 [[R]]
346 %h = uitofp i32 %x to half
347 %r = fptoui half %h to i12