xref: /aosp_15_r20/external/llvm/test/Transforms/InstCombine/fpcast.ll (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker; Test some floating point casting cases
2*9880d681SAndroid Build Coastguard Worker; RUN: opt < %s -instcombine -S | FileCheck %s
3*9880d681SAndroid Build Coastguard Worker
4*9880d681SAndroid Build Coastguard Workerdefine i8 @test1() {
5*9880d681SAndroid Build Coastguard Worker        %x = fptoui float 2.550000e+02 to i8            ; <i8> [#uses=1]
6*9880d681SAndroid Build Coastguard Worker        ret i8 %x
7*9880d681SAndroid Build Coastguard Worker; CHECK: ret i8 -1
8*9880d681SAndroid Build Coastguard Worker}
9*9880d681SAndroid Build Coastguard Worker
10*9880d681SAndroid Build Coastguard Workerdefine i8 @test2() {
11*9880d681SAndroid Build Coastguard Worker        %x = fptosi float -1.000000e+00 to i8           ; <i8> [#uses=1]
12*9880d681SAndroid Build Coastguard Worker        ret i8 %x
13*9880d681SAndroid Build Coastguard Worker; CHECK: ret i8 -1
14*9880d681SAndroid Build Coastguard Worker}
15*9880d681SAndroid Build Coastguard Worker
16*9880d681SAndroid Build Coastguard Worker; CHECK: test3
17*9880d681SAndroid Build Coastguard Workerdefine half @test3(float %a) {
18*9880d681SAndroid Build Coastguard Worker; CHECK: fptrunc
19*9880d681SAndroid Build Coastguard Worker; CHECK: llvm.fabs.f16
20*9880d681SAndroid Build Coastguard Worker  %b = call float @llvm.fabs.f32(float %a)
21*9880d681SAndroid Build Coastguard Worker  %c = fptrunc float %b to half
22*9880d681SAndroid Build Coastguard Worker  ret half %c
23*9880d681SAndroid Build Coastguard Worker}
24*9880d681SAndroid Build Coastguard Worker
25*9880d681SAndroid Build Coastguard Worker; CHECK: test4
26*9880d681SAndroid Build Coastguard Workerdefine half @test4(float %a) {
27*9880d681SAndroid Build Coastguard Worker; CHECK: fptrunc
28*9880d681SAndroid Build Coastguard Worker; CHECK: fsub
29*9880d681SAndroid Build Coastguard Worker  %b = fsub float -0.0, %a
30*9880d681SAndroid Build Coastguard Worker  %c = fptrunc float %b to half
31*9880d681SAndroid Build Coastguard Worker  ret half %c
32*9880d681SAndroid Build Coastguard Worker}
33*9880d681SAndroid Build Coastguard Worker
34*9880d681SAndroid Build Coastguard Worker; CHECK: test4-fast
35*9880d681SAndroid Build Coastguard Workerdefine half @test4-fast(float %a) {
36*9880d681SAndroid Build Coastguard Worker; CHECK: fptrunc
37*9880d681SAndroid Build Coastguard Worker; CHECK: fsub fast
38*9880d681SAndroid Build Coastguard Worker  %b = fsub fast float -0.0, %a
39*9880d681SAndroid Build Coastguard Worker  %c = fptrunc float %b to half
40*9880d681SAndroid Build Coastguard Worker  ret half %c
41*9880d681SAndroid Build Coastguard Worker}
42*9880d681SAndroid Build Coastguard Worker
43*9880d681SAndroid Build Coastguard Worker; CHECK: test5
44*9880d681SAndroid Build Coastguard Workerdefine half @test5(float %a, float %b, float %c) {
45*9880d681SAndroid Build Coastguard Worker; CHECK: fcmp ogt
46*9880d681SAndroid Build Coastguard Worker; CHECK: fptrunc
47*9880d681SAndroid Build Coastguard Worker; CHECK: select
48*9880d681SAndroid Build Coastguard Worker; CHECK: half 0xH3C00
49*9880d681SAndroid Build Coastguard Worker  %d = fcmp ogt float %a, %b
50*9880d681SAndroid Build Coastguard Worker  %e = select i1 %d, float %c, float 1.0
51*9880d681SAndroid Build Coastguard Worker  %f = fptrunc float %e to half
52*9880d681SAndroid Build Coastguard Worker  ret half %f
53*9880d681SAndroid Build Coastguard Worker}
54*9880d681SAndroid Build Coastguard Worker
55*9880d681SAndroid Build Coastguard Workerdeclare float @llvm.fabs.f32(float) nounwind readonly
56*9880d681SAndroid Build Coastguard Worker
57*9880d681SAndroid Build Coastguard Workerdefine <1 x float> @test6(<1 x double> %V) {
58*9880d681SAndroid Build Coastguard Worker  %frem = frem <1 x double> %V, %V
59*9880d681SAndroid Build Coastguard Worker  %trunc = fptrunc <1 x double> %frem to <1 x float>
60*9880d681SAndroid Build Coastguard Worker  ret <1 x float> %trunc
61*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test6
62*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: %[[frem:.*]]  = frem <1 x double> %V, %V
63*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: %[[trunc:.*]] = fptrunc <1 x double> %[[frem]] to <1 x float>
64*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: ret <1 x float> %trunc
65*9880d681SAndroid Build Coastguard Worker}
66*9880d681SAndroid Build Coastguard Worker
67*9880d681SAndroid Build Coastguard Workerdefine float @test7(double %V) {
68*9880d681SAndroid Build Coastguard Worker  %frem = frem double %V, 1.000000e+00
69*9880d681SAndroid Build Coastguard Worker  %trunc = fptrunc double %frem to float
70*9880d681SAndroid Build Coastguard Worker  ret float %trunc
71*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test7
72*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: %[[frem:.*]]  = frem double %V, 1.000000e+00
73*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: %[[trunc:.*]] = fptrunc double %frem to float
74*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: ret float %trunc
75*9880d681SAndroid Build Coastguard Worker}
76*9880d681SAndroid Build Coastguard Worker
77*9880d681SAndroid Build Coastguard Workerdefine float @test8(float %V) {
78*9880d681SAndroid Build Coastguard Worker  %fext = fpext float %V to double
79*9880d681SAndroid Build Coastguard Worker  %frem = frem double %fext, 1.000000e-01
80*9880d681SAndroid Build Coastguard Worker  %trunc = fptrunc double %frem to float
81*9880d681SAndroid Build Coastguard Worker  ret float %trunc
82*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test8
83*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: %[[fext:.*]]  = fpext float %V to double
84*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: %[[frem:.*]]  = frem double %fext, 1.000000e-01
85*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: %[[trunc:.*]] = fptrunc double %frem to float
86*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: ret float %trunc
87*9880d681SAndroid Build Coastguard Worker}
88*9880d681SAndroid Build Coastguard Worker
89*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test_fptrunc_fptrunc
90*9880d681SAndroid Build Coastguard Worker; CHECK-NOT: fptrunc double {{.*}} to half
91*9880d681SAndroid Build Coastguard Workerdefine half @test_fptrunc_fptrunc(double %V) {
92*9880d681SAndroid Build Coastguard Worker  %t1 = fptrunc double %V to float
93*9880d681SAndroid Build Coastguard Worker  %t2 = fptrunc float %t1 to half
94*9880d681SAndroid Build Coastguard Worker  ret half %t2
95*9880d681SAndroid Build Coastguard Worker}
96