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