1 /*
2 * Copyright (C) 2021 Collabora, Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include "bi_builder.h"
25 #include "bi_test.h"
26 #include "va_compiler.h"
27
28 #include <gtest/gtest.h>
29
30 #define CASE(instr, expected) INSTRUCTION_CASE(instr, expected, va_lower_isel)
31 #define NEGCASE(instr) CASE(instr, instr)
32
33 class LowerIsel : public testing::Test {
34 protected:
LowerIsel()35 LowerIsel()
36 {
37 mem_ctx = ralloc_context(NULL);
38 reg = bi_register(1);
39 x = bi_register(2);
40 y = bi_register(3);
41 z = bi_register(4);
42 }
43
~LowerIsel()44 ~LowerIsel()
45 {
46 ralloc_free(mem_ctx);
47 }
48
49 void *mem_ctx;
50 bi_index reg, x, y, z;
51 };
52
53 TEST_F(LowerIsel, 8BitSwizzles)
54 {
55 for (unsigned i = 0; i < 4; ++i) {
56 CASE(bi_swz_v4i8_to(b, reg, bi_byte(reg, i)),
57 bi_iadd_v4u8_to(b, reg, bi_byte(reg, i), bi_zero(), false));
58 }
59 }
60
61 TEST_F(LowerIsel, 16BitSwizzles)
62 {
63 for (unsigned i = 0; i < 2; ++i) {
64 for (unsigned j = 0; j < 2; ++j) {
65 CASE(bi_swz_v2i16_to(b, reg, bi_swz_16(reg, i, j)),
66 bi_iadd_v2u16_to(b, reg, bi_swz_16(reg, i, j), bi_zero(), false));
67 }
68 }
69 }
70
TEST_F(LowerIsel,JumpsLoweredToBranches)71 TEST_F(LowerIsel, JumpsLoweredToBranches)
72 {
73 bi_block block = {};
74
75 CASE(
76 {
77 bi_instr *I = bi_jump(b, bi_imm_u32(0xDEADBEEF));
78 I->branch_target = █
79 },
80 {
81 bi_instr *I =
82 bi_branchz_i16(b, bi_zero(), bi_imm_u32(0xDEADBEEF), BI_CMPF_EQ);
83 I->branch_target = █
84 });
85 }
86
TEST_F(LowerIsel,IndirectJumpsLoweredToBranches)87 TEST_F(LowerIsel, IndirectJumpsLoweredToBranches)
88 {
89 CASE(bi_jump(b, bi_register(17)),
90 bi_branchzi(b, bi_zero(), bi_register(17), BI_CMPF_EQ));
91 }
92
TEST_F(LowerIsel,IntegerCSEL)93 TEST_F(LowerIsel, IntegerCSEL)
94 {
95 CASE(bi_csel_i32(b, reg, reg, reg, reg, BI_CMPF_EQ),
96 bi_csel_u32(b, reg, reg, reg, reg, BI_CMPF_EQ));
97
98 CASE(bi_csel_v2i16(b, reg, reg, reg, reg, BI_CMPF_EQ),
99 bi_csel_v2u16(b, reg, reg, reg, reg, BI_CMPF_EQ));
100 }
101
TEST_F(LowerIsel,AvoidSimpleMux)102 TEST_F(LowerIsel, AvoidSimpleMux)
103 {
104 CASE(bi_mux_i32(b, x, y, z, BI_MUX_INT_ZERO),
105 bi_csel_u32(b, z, bi_zero(), x, y, BI_CMPF_EQ));
106 CASE(bi_mux_i32(b, x, y, z, BI_MUX_NEG),
107 bi_csel_s32(b, z, bi_zero(), x, y, BI_CMPF_LT));
108 CASE(bi_mux_i32(b, x, y, z, BI_MUX_FP_ZERO),
109 bi_csel_f32(b, z, bi_zero(), x, y, BI_CMPF_EQ));
110
111 CASE(bi_mux_v2i16(b, x, y, z, BI_MUX_INT_ZERO),
112 bi_csel_v2u16(b, z, bi_zero(), x, y, BI_CMPF_EQ));
113 CASE(bi_mux_v2i16(b, x, y, z, BI_MUX_NEG),
114 bi_csel_v2s16(b, z, bi_zero(), x, y, BI_CMPF_LT));
115 CASE(bi_mux_v2i16(b, x, y, z, BI_MUX_FP_ZERO),
116 bi_csel_v2f16(b, z, bi_zero(), x, y, BI_CMPF_EQ));
117 }
118
TEST_F(LowerIsel,BitwiseMux)119 TEST_F(LowerIsel, BitwiseMux)
120 {
121 NEGCASE(bi_mux_i32(b, x, y, z, BI_MUX_BIT));
122 NEGCASE(bi_mux_v2i16(b, x, y, z, BI_MUX_BIT));
123 NEGCASE(bi_mux_v4i8(b, x, y, z, BI_MUX_BIT));
124 }
125
TEST_F(LowerIsel,MuxInt8)126 TEST_F(LowerIsel, MuxInt8)
127 {
128 NEGCASE(bi_mux_v4i8(b, x, y, z, BI_MUX_INT_ZERO));
129 NEGCASE(bi_mux_v4i8(b, x, y, z, BI_MUX_NEG));
130 NEGCASE(bi_mux_v4i8(b, x, y, z, BI_MUX_FP_ZERO));
131 }
132
TEST_F(LowerIsel,FaddRscale)133 TEST_F(LowerIsel, FaddRscale)
134 {
135 CASE(
136 bi_fadd_rscale_f32_to(b, reg, x, y, z, BI_SPECIAL_NONE),
137 bi_fma_rscale_f32_to(b, reg, x, bi_imm_f32(1.0), y, z, BI_SPECIAL_NONE));
138
139 CASE(bi_fadd_rscale_f32_to(b, reg, x, y, z, BI_SPECIAL_N),
140 bi_fma_rscale_f32_to(b, reg, x, bi_imm_f32(1.0), y, z, BI_SPECIAL_N));
141 }
142
TEST_F(LowerIsel,Smoke)143 TEST_F(LowerIsel, Smoke)
144 {
145 NEGCASE(bi_fadd_f32_to(b, reg, reg, reg));
146 NEGCASE(bi_csel_s32_to(b, reg, reg, reg, reg, reg, BI_CMPF_LT));
147 NEGCASE(bi_csel_u32_to(b, reg, reg, reg, reg, reg, BI_CMPF_LT));
148 }
149