1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; Test some floating point casting cases
3; RUN: opt < %s -instcombine -S | FileCheck %s
4
5define i8 @test1() {
6; CHECK-LABEL: @test1(
7; CHECK-NEXT:    ret i8 -1
8;
9  %x = fptoui float 2.550000e+02 to i8
10  ret i8 %x
11}
12
13define i8 @test2() {
14; CHECK-LABEL: @test2(
15; CHECK-NEXT:    ret i8 -1
16;
17  %x = fptosi float -1.000000e+00 to i8
18  ret i8 %x
19}
20
21define half @test3(float %a) {
22; CHECK-LABEL: @test3(
23; CHECK-NEXT:    [[TMP1:%.*]] = fptrunc float [[A:%.*]] to half
24; CHECK-NEXT:    [[C:%.*]] = call half @llvm.fabs.f16(half [[TMP1]])
25; CHECK-NEXT:    ret half [[C]]
26;
27  %b = call float @llvm.fabs.f32(float %a)
28  %c = fptrunc float %b to half
29  ret half %c
30}
31
32define half @fneg_fptrunc(float %a) {
33; CHECK-LABEL: @fneg_fptrunc(
34; CHECK-NEXT:    [[TMP1:%.*]] = fptrunc float [[A:%.*]] to half
35; CHECK-NEXT:    [[C:%.*]] = fneg half [[TMP1]]
36; CHECK-NEXT:    ret half [[C]]
37;
38  %b = fsub float -0.0, %a
39  %c = fptrunc float %b to half
40  ret half %c
41}
42
43define half @unary_fneg_fptrunc(float %a) {
44; CHECK-LABEL: @unary_fneg_fptrunc(
45; CHECK-NEXT:    [[TMP1:%.*]] = fptrunc float [[A:%.*]] to half
46; CHECK-NEXT:    [[C:%.*]] = fneg half [[TMP1]]
47; CHECK-NEXT:    ret half [[C]]
48;
49  %b = fneg float %a
50  %c = fptrunc float %b to half
51  ret half %c
52}
53
54define <2 x half> @fneg_fptrunc_vec_undef(<2 x float> %a) {
55; CHECK-LABEL: @fneg_fptrunc_vec_undef(
56; CHECK-NEXT:    [[TMP1:%.*]] = fptrunc <2 x float> [[A:%.*]] to <2 x half>
57; CHECK-NEXT:    [[C:%.*]] = fneg <2 x half> [[TMP1]]
58; CHECK-NEXT:    ret <2 x half> [[C]]
59;
60  %b = fsub <2 x float> <float -0.0, float undef>, %a
61  %c = fptrunc <2 x float> %b to <2 x half>
62  ret <2 x half> %c
63}
64
65define <2 x half> @unary_fneg_fptrunc_vec(<2 x float> %a) {
66; CHECK-LABEL: @unary_fneg_fptrunc_vec(
67; CHECK-NEXT:    [[TMP1:%.*]] = fptrunc <2 x float> [[A:%.*]] to <2 x half>
68; CHECK-NEXT:    [[C:%.*]] = fneg <2 x half> [[TMP1]]
69; CHECK-NEXT:    ret <2 x half> [[C]]
70;
71  %b = fneg <2 x float> %a
72  %c = fptrunc <2 x float> %b to <2 x half>
73  ret <2 x half> %c
74}
75
76define half @test4-fast(float %a) {
77; CHECK-LABEL: @test4-fast(
78; CHECK-NEXT:    [[TMP1:%.*]] = fptrunc float [[A:%.*]] to half
79; CHECK-NEXT:    [[C:%.*]] = fneg fast half [[TMP1]]
80; CHECK-NEXT:    ret half [[C]]
81;
82  %b = fsub fast float -0.0, %a
83  %c = fptrunc float %b to half
84  ret half %c
85}
86
87define half @test4_unary_fneg-fast(float %a) {
88; CHECK-LABEL: @test4_unary_fneg-fast(
89; CHECK-NEXT:    [[TMP1:%.*]] = fptrunc float [[A:%.*]] to half
90; CHECK-NEXT:    [[C:%.*]] = fneg fast half [[TMP1]]
91; CHECK-NEXT:    ret half [[C]]
92;
93  %b = fneg fast float %a
94  %c = fptrunc float %b to half
95  ret half %c
96}
97
98define half @test5(float %a, float %b, float %c) {
99; CHECK-LABEL: @test5(
100; CHECK-NEXT:    [[D:%.*]] = fcmp ogt float [[A:%.*]], [[B:%.*]]
101; CHECK-NEXT:    [[E:%.*]] = select i1 [[D]], float [[C:%.*]], float 1.000000e+00
102; CHECK-NEXT:    [[F:%.*]] = fptrunc float [[E]] to half
103; CHECK-NEXT:    ret half [[F]]
104;
105  %d = fcmp ogt float %a, %b
106  %e = select i1 %d, float %c, float 1.0
107  %f = fptrunc float %e to half
108  ret half %f
109}
110
111declare float @llvm.fabs.f32(float) nounwind readonly
112
113define <1 x float> @test6(<1 x double> %V) {
114; CHECK-LABEL: @test6(
115; CHECK-NEXT:    [[FREM:%.*]] = frem <1 x double> [[V:%.*]], [[V]]
116; CHECK-NEXT:    [[TRUNC:%.*]] = fptrunc <1 x double> [[FREM]] to <1 x float>
117; CHECK-NEXT:    ret <1 x float> [[TRUNC]]
118;
119  %frem = frem <1 x double> %V, %V
120  %trunc = fptrunc <1 x double> %frem to <1 x float>
121  ret <1 x float> %trunc
122}
123
124define float @test7(double %V) {
125; CHECK-LABEL: @test7(
126; CHECK-NEXT:    [[FREM:%.*]] = frem double [[V:%.*]], 1.000000e+00
127; CHECK-NEXT:    [[TRUNC:%.*]] = fptrunc double [[FREM]] to float
128; CHECK-NEXT:    ret float [[TRUNC]]
129;
130  %frem = frem double %V, 1.000000e+00
131  %trunc = fptrunc double %frem to float
132  ret float %trunc
133}
134
135define float @test8(float %V) {
136; CHECK-LABEL: @test8(
137; CHECK-NEXT:    [[FEXT:%.*]] = fpext float [[V:%.*]] to double
138; CHECK-NEXT:    [[FREM:%.*]] = frem double [[FEXT]], 1.000000e-01
139; CHECK-NEXT:    [[TRUNC:%.*]] = fptrunc double [[FREM]] to float
140; CHECK-NEXT:    ret float [[TRUNC]]
141;
142  %fext = fpext float %V to double
143  %frem = frem double %fext, 1.000000e-01
144  %trunc = fptrunc double %frem to float
145  ret float %trunc
146}
147
148define half @test_fptrunc_fptrunc(double %V) {
149; CHECK-LABEL: @test_fptrunc_fptrunc(
150; CHECK-NEXT:    [[T1:%.*]] = fptrunc double [[V:%.*]] to float
151; CHECK-NEXT:    [[T2:%.*]] = fptrunc float [[T1]] to half
152; CHECK-NEXT:    ret half [[T2]]
153;
154  %t1 = fptrunc double %V to float
155  %t2 = fptrunc float %t1 to half
156  ret half %t2
157}
158
159