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 "valhall.h"
26bf215546Sopenharmony_ci#include "bi_builder.h"
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_civoid
29bf215546Sopenharmony_civa_lower_isel(bi_instr *I)
30bf215546Sopenharmony_ci{
31bf215546Sopenharmony_ci   switch (I->op) {
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci   /* Integer addition has swizzles and addition with 0 is canonical swizzle */
34bf215546Sopenharmony_ci   case BI_OPCODE_SWZ_V2I16:
35bf215546Sopenharmony_ci      I->op = BI_OPCODE_IADD_V2U16;
36bf215546Sopenharmony_ci      I->src[1] = bi_zero();
37bf215546Sopenharmony_ci      break;
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_ci   case BI_OPCODE_SWZ_V4I8:
40bf215546Sopenharmony_ci      I->op = BI_OPCODE_IADD_V4U8;
41bf215546Sopenharmony_ci      I->src[1] = bi_zero();
42bf215546Sopenharmony_ci      break;
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci   /* Extra source in Valhall not yet modeled in the Bifrost IR */
45bf215546Sopenharmony_ci   case BI_OPCODE_ICMP_I32:
46bf215546Sopenharmony_ci      I->op = BI_OPCODE_ICMP_U32;
47bf215546Sopenharmony_ci      I->src[2] = bi_zero();
48bf215546Sopenharmony_ci      break;
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci   case BI_OPCODE_ICMP_V2I16:
51bf215546Sopenharmony_ci      I->op = BI_OPCODE_ICMP_V2U16;
52bf215546Sopenharmony_ci      I->src[2] = bi_zero();
53bf215546Sopenharmony_ci      break;
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_ci   case BI_OPCODE_ICMP_V4I8:
56bf215546Sopenharmony_ci      I->op = BI_OPCODE_ICMP_V4U8;
57bf215546Sopenharmony_ci      I->src[2] = bi_zero();
58bf215546Sopenharmony_ci      break;
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ci   case BI_OPCODE_ICMP_U32:
61bf215546Sopenharmony_ci   case BI_OPCODE_ICMP_V2U16:
62bf215546Sopenharmony_ci   case BI_OPCODE_ICMP_V4U8:
63bf215546Sopenharmony_ci   case BI_OPCODE_ICMP_S32:
64bf215546Sopenharmony_ci   case BI_OPCODE_ICMP_V2S16:
65bf215546Sopenharmony_ci   case BI_OPCODE_ICMP_V4S8:
66bf215546Sopenharmony_ci   case BI_OPCODE_FCMP_F32:
67bf215546Sopenharmony_ci   case BI_OPCODE_FCMP_V2F16:
68bf215546Sopenharmony_ci      I->src[2] = bi_zero();
69bf215546Sopenharmony_ci      break;
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_ci   /* Integer CSEL must have a signedness */
72bf215546Sopenharmony_ci   case BI_OPCODE_CSEL_I32:
73bf215546Sopenharmony_ci   case BI_OPCODE_CSEL_V2I16:
74bf215546Sopenharmony_ci      assert(I->cmpf == BI_CMPF_EQ || I->cmpf == BI_CMPF_NE);
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_ci      I->op = (I->op == BI_OPCODE_CSEL_I32) ? BI_OPCODE_CSEL_U32 :
77bf215546Sopenharmony_ci              BI_OPCODE_CSEL_V2U16;
78bf215546Sopenharmony_ci      break;
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci   /* Jump -> conditional branch with condition tied to true. */
81bf215546Sopenharmony_ci   case BI_OPCODE_JUMP:
82bf215546Sopenharmony_ci      I->op = I->branch_target ? BI_OPCODE_BRANCHZ_I16 : BI_OPCODE_BRANCHZI;
83bf215546Sopenharmony_ci      I->src[1] = I->src[0];
84bf215546Sopenharmony_ci      I->src[0] = bi_zero();
85bf215546Sopenharmony_ci      I->cmpf = BI_CMPF_EQ;
86bf215546Sopenharmony_ci      break;
87bf215546Sopenharmony_ci
88bf215546Sopenharmony_ci   case BI_OPCODE_AXCHG_I32:
89bf215546Sopenharmony_ci      I->op = BI_OPCODE_ATOM_RETURN_I32;
90bf215546Sopenharmony_ci      I->atom_opc = BI_ATOM_OPC_AXCHG;
91bf215546Sopenharmony_ci      I->sr_count = 1;
92bf215546Sopenharmony_ci      break;
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_ci   case BI_OPCODE_ACMPXCHG_I32:
95bf215546Sopenharmony_ci      I->op = BI_OPCODE_ATOM_RETURN_I32;
96bf215546Sopenharmony_ci      I->atom_opc = BI_ATOM_OPC_ACMPXCHG;
97bf215546Sopenharmony_ci      /* Reads 2, this is special cased in bir.c */
98bf215546Sopenharmony_ci      I->sr_count = 1;
99bf215546Sopenharmony_ci      break;
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci   case BI_OPCODE_ATOM_RETURN_I32:
102bf215546Sopenharmony_ci      if (bi_is_null(I->dest[0]))
103bf215546Sopenharmony_ci         I->op = BI_OPCODE_ATOM_I32;
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci      break;
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_ci   case BI_OPCODE_MUX_I32:
108bf215546Sopenharmony_ci   case BI_OPCODE_MUX_V2I16:
109bf215546Sopenharmony_ci      if (bi_can_replace_with_csel(I))
110bf215546Sopenharmony_ci         bi_replace_mux_with_csel(I, true);
111bf215546Sopenharmony_ci
112bf215546Sopenharmony_ci      break;
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_ci   /* FADD_RSCALE.f32(x, y, z) -> FMA_RSCALE.f32(x, 1.0, y, z) */
115bf215546Sopenharmony_ci   case BI_OPCODE_FADD_RSCALE_F32:
116bf215546Sopenharmony_ci      I->op = BI_OPCODE_FMA_RSCALE_F32;
117bf215546Sopenharmony_ci      I->src[3] = I->src[2];
118bf215546Sopenharmony_ci      I->src[2] = I->src[1];
119bf215546Sopenharmony_ci      I->src[1] = bi_imm_f32(1.0);
120bf215546Sopenharmony_ci      break;
121bf215546Sopenharmony_ci
122bf215546Sopenharmony_ci   default:
123bf215546Sopenharmony_ci      break;
124bf215546Sopenharmony_ci   }
125bf215546Sopenharmony_ci}
126