pan/bi: Structify FMA ICMP 16
[mesa.git] / src / panfrost / bifrost / bir.c
1 /*
2 * Copyright (C) 2020 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 * Authors (Collabora):
24 * Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
25 */
26
27 #include "compiler.h"
28
29 /* Does an instruction respect outmods and source mods? Depend
30 * on the types involved */
31
32 bool
33 bi_has_outmod(bi_instruction *ins)
34 {
35 bool classy = bi_class_props[ins->type] & BI_MODS;
36 bool floaty = nir_alu_type_get_base_type(ins->dest_type) == nir_type_float;
37
38 return classy && floaty;
39 }
40
41 /* Technically we should check the source type, not the dest
42 * type, but the type converting opcodes (i2f, f2i) don't
43 * actually support mods so it doesn't matter. */
44
45 bool
46 bi_has_source_mods(bi_instruction *ins)
47 {
48 return bi_has_outmod(ins);
49 }
50
51 /* A source is swizzled if the op is swizzlable, in 8-bit or
52 * 16-bit mode, and the swizzled op. TODO: multi args */
53
54 bool
55 bi_is_src_swizzled(bi_instruction *ins, unsigned s)
56 {
57 bool classy = bi_class_props[ins->type] & BI_SWIZZLABLE;
58 bool small = nir_alu_type_get_type_size(ins->dest_type) < 32;
59 bool first = (s == 0); /* TODO: prop? */
60
61 return classy && small && first;
62 }
63
64 bool
65 bi_has_arg(bi_instruction *ins, unsigned arg)
66 {
67 if (!ins)
68 return false;
69
70 bi_foreach_src(ins, s) {
71 if (ins->src[s] == arg)
72 return true;
73 }
74
75 return false;
76 }
77
78 uint16_t
79 bi_from_bytemask(uint16_t bytemask, unsigned bytes)
80 {
81 unsigned value = 0;
82
83 for (unsigned c = 0, d = 0; c < 16; c += bytes, ++d) {
84 bool a = (bytemask & (1 << c)) != 0;
85
86 for (unsigned q = c; q < bytes; ++q)
87 assert(((bytemask & (1 << q)) != 0) == a);
88
89 value |= (a << d);
90 }
91
92 return value;
93 }
94
95 unsigned
96 bi_get_component_count(bi_instruction *ins, signed src)
97 {
98 if (bi_class_props[ins->type] & BI_VECTOR) {
99 assert(ins->vector_channels);
100 return (src <= 0) ? ins->vector_channels : 1;
101 } else {
102 unsigned bytes = nir_alu_type_get_type_size(src < 0 ? ins->dest_type : ins->src_types[src]);
103
104 if (ins->type == BI_ATEST || ins->type == BI_SELECT)
105 return 1;
106
107 return MAX2(32 / bytes, 1);
108 }
109 }
110
111 uint16_t
112 bi_bytemask_of_read_components(bi_instruction *ins, unsigned node)
113 {
114 uint16_t mask = 0x0;
115
116 bi_foreach_src(ins, s) {
117 if (ins->src[s] != node) continue;
118 unsigned component_count = bi_get_component_count(ins, s);
119 nir_alu_type T = ins->src_types[s];
120 unsigned size = nir_alu_type_get_type_size(T);
121 unsigned bytes = size / 8;
122 unsigned cmask = (1 << bytes) - 1;
123
124 for (unsigned i = 0; i < component_count; ++i) {
125 unsigned c = ins->swizzle[s][i];
126 mask |= (cmask << (c * bytes));
127 }
128 }
129
130 return mask;
131 }
132
133 uint64_t
134 bi_get_immediate(bi_instruction *ins, unsigned index)
135 {
136 unsigned v = ins->src[index];
137 assert(v & BIR_INDEX_CONSTANT);
138 unsigned shift = v & ~BIR_INDEX_CONSTANT;
139 uint64_t shifted = ins->constant.u64 >> shift;
140
141 /* Mask off the accessed part */
142 unsigned sz = nir_alu_type_get_type_size(ins->src_types[index]);
143
144 if (sz == 64)
145 return shifted;
146 else
147 return shifted & ((1ull << sz) - 1);
148 }
149
150 bool
151 bi_writes_component(bi_instruction *ins, unsigned comp)
152 {
153 return comp < bi_get_component_count(ins, -1);
154 }
155
156 unsigned
157 bi_writemask(bi_instruction *ins)
158 {
159 nir_alu_type T = ins->dest_type;
160 unsigned size = nir_alu_type_get_type_size(T);
161 unsigned bytes_per_comp = size / 8;
162 unsigned components = bi_get_component_count(ins, -1);
163 unsigned bytes = bytes_per_comp * components;
164 unsigned mask = (1 << bytes) - 1;
165 unsigned shift = ins->dest_offset * 4; /* 32-bit words */
166 return (mask << shift);
167 }