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 "va_compiler.h"
25 #include "bi_test.h"
26 #include "bi_builder.h"
27
28 #include <gtest/gtest.h>
29
30 static inline void
case_cb(bi_context * ctx)31 case_cb(bi_context *ctx)
32 {
33 bi_foreach_instr_global(ctx, I) {
34 va_lower_isel(I);
35 }
36 }
37
38 #define CASE(instr, expected) INSTRUCTION_CASE(instr, expected, case_cb)
39 #define NEGCASE(instr) CASE(instr, instr)
40
41 class LowerIsel : public testing::Test {
42 protected:
LowerIsel()43 LowerIsel() {
44 mem_ctx = ralloc_context(NULL);
45 reg = bi_register(1);
46 x = bi_register(2);
47 y = bi_register(3);
48 z = bi_register(4);
49 }
50
~LowerIsel()51 ~LowerIsel() {
52 ralloc_free(mem_ctx);
53 }
54
55 void *mem_ctx;
56 bi_index reg, x, y, z;
57 };
58
59 TEST_F(LowerIsel, 8BitSwizzles) {
60 for (unsigned i = 0; i < 4; ++i) {
61 CASE(bi_swz_v4i8_to(b, reg, bi_byte(reg, i)),
62 bi_iadd_v4u8_to(b, reg, bi_byte(reg, i), bi_zero(), false));
63 }
64 }
65
66 TEST_F(LowerIsel, 16BitSwizzles) {
67 for (unsigned i = 0; i < 2; ++i) {
68 for (unsigned j = 0; j < 2; ++j) {
69 CASE(bi_swz_v2i16_to(b, reg, bi_swz_16(reg, i, j)),
70 bi_iadd_v2u16_to(b, reg, bi_swz_16(reg, i, j), bi_zero(), false));
71 }
72 }
73 }
74
TEST_F(LowerIsel,JumpsLoweredToBranches)75 TEST_F(LowerIsel, JumpsLoweredToBranches) {
76 bi_block block = { };
77
78 CASE({
79 bi_instr *I = bi_jump(b, bi_imm_u32(0xDEADBEEF));
80 I->branch_target = █
81 }, {
82 bi_instr *I = 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 CASE(bi_jump(b, bi_register(17)),
89 bi_branchzi(b, bi_zero(), bi_register(17), BI_CMPF_EQ));
90 }
91
TEST_F(LowerIsel,IntegerCSEL)92 TEST_F(LowerIsel, IntegerCSEL) {
93 CASE(bi_csel_i32(b, reg, reg, reg, reg, BI_CMPF_EQ),
94 bi_csel_u32(b, reg, reg, reg, reg, BI_CMPF_EQ));
95
96 CASE(bi_csel_v2i16(b, reg, reg, reg, reg, BI_CMPF_EQ),
97 bi_csel_v2u16(b, reg, reg, reg, reg, BI_CMPF_EQ));
98 }
99
TEST_F(LowerIsel,AvoidSimpleMux)100 TEST_F(LowerIsel, AvoidSimpleMux) {
101 CASE(bi_mux_i32(b, x, y, z, BI_MUX_INT_ZERO),
102 bi_csel_u32(b, z, bi_zero(), x, y, BI_CMPF_EQ));
103 CASE(bi_mux_i32(b, x, y, z, BI_MUX_NEG),
104 bi_csel_s32(b, z, bi_zero(), x, y, BI_CMPF_LT));
105 CASE(bi_mux_i32(b, x, y, z, BI_MUX_FP_ZERO),
106 bi_csel_f32(b, z, bi_zero(), x, y, BI_CMPF_EQ));
107
108 CASE(bi_mux_v2i16(b, x, y, z, BI_MUX_INT_ZERO),
109 bi_csel_v2u16(b, z, bi_zero(), x, y, BI_CMPF_EQ));
110 CASE(bi_mux_v2i16(b, x, y, z, BI_MUX_NEG),
111 bi_csel_v2s16(b, z, bi_zero(), x, y, BI_CMPF_LT));
112 CASE(bi_mux_v2i16(b, x, y, z, BI_MUX_FP_ZERO),
113 bi_csel_v2f16(b, z, bi_zero(), x, y, BI_CMPF_EQ));
114 }
115
TEST_F(LowerIsel,BitwiseMux)116 TEST_F(LowerIsel, BitwiseMux) {
117 NEGCASE(bi_mux_i32(b, x, y, z, BI_MUX_BIT));
118 NEGCASE(bi_mux_v2i16(b, x, y, z, BI_MUX_BIT));
119 NEGCASE(bi_mux_v4i8(b, x, y, z, BI_MUX_BIT));
120 }
121
TEST_F(LowerIsel,MuxInt8)122 TEST_F(LowerIsel, MuxInt8) {
123 NEGCASE(bi_mux_v4i8(b, x, y, z, BI_MUX_INT_ZERO));
124 NEGCASE(bi_mux_v4i8(b, x, y, z, BI_MUX_NEG));
125 NEGCASE(bi_mux_v4i8(b, x, y, z, BI_MUX_FP_ZERO));
126 }
127
TEST_F(LowerIsel,FaddRscale)128 TEST_F(LowerIsel, FaddRscale) {
129 CASE(bi_fadd_rscale_f32_to(b, reg, x, y, z, BI_SPECIAL_NONE),
130 bi_fma_rscale_f32_to(b, reg, x, bi_imm_f32(1.0), y, z, BI_SPECIAL_NONE));
131
132 CASE(bi_fadd_rscale_f32_to(b, reg, x, y, z, BI_SPECIAL_N),
133 bi_fma_rscale_f32_to(b, reg, x, bi_imm_f32(1.0), y, z, BI_SPECIAL_N));
134 }
135
TEST_F(LowerIsel,Smoke)136 TEST_F(LowerIsel, Smoke) {
137 NEGCASE(bi_fadd_f32_to(b, reg, reg, reg));
138 NEGCASE(bi_csel_s32_to(b, reg, reg, reg, reg, reg, BI_CMPF_LT));
139 NEGCASE(bi_csel_u32_to(b, reg, reg, reg, reg, reg, BI_CMPF_LT));
140 }
141