pan/midgard: Extract simple source mod check
[mesa.git] / src / panfrost / midgard / mir.c
1 /*
2 * Copyright (C) 2019 Alyssa Rosenzweig <alyssa@rosenzweig.io>
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 "compiler.h"
25 #include "midgard_ops.h"
26
27 void mir_rewrite_index_src_single(midgard_instruction *ins, unsigned old, unsigned new)
28 {
29 if (ins->ssa_args.src0 == old)
30 ins->ssa_args.src0 = new;
31
32 if (ins->ssa_args.src1 == old &&
33 !ins->ssa_args.inline_constant)
34 ins->ssa_args.src1 = new;
35 }
36
37
38 void
39 mir_rewrite_index_src(compiler_context *ctx, unsigned old, unsigned new)
40 {
41 mir_foreach_instr_global(ctx, ins) {
42 mir_rewrite_index_src_single(ins, old, new);
43 }
44 }
45
46 void
47 mir_rewrite_index_src_tag(compiler_context *ctx, unsigned old, unsigned new, unsigned tag)
48 {
49 mir_foreach_instr_global(ctx, ins) {
50 if (ins->type != tag)
51 continue;
52
53 mir_rewrite_index_src_single(ins, old, new);
54 }
55 }
56
57
58
59 void
60 mir_rewrite_index_dst(compiler_context *ctx, unsigned old, unsigned new)
61 {
62 mir_foreach_instr_global(ctx, ins) {
63 if (ins->ssa_args.dest == old)
64 ins->ssa_args.dest = new;
65 }
66 }
67
68 void
69 mir_rewrite_index_dst_tag(compiler_context *ctx, unsigned old, unsigned new, unsigned tag)
70 {
71 mir_foreach_instr_global(ctx, ins) {
72 if (ins->type != tag)
73 continue;
74
75 if (ins->ssa_args.dest == old)
76 ins->ssa_args.dest = new;
77 }
78 }
79
80
81
82 void
83 mir_rewrite_index(compiler_context *ctx, unsigned old, unsigned new)
84 {
85 mir_rewrite_index_src(ctx, old, new);
86 mir_rewrite_index_dst(ctx, old, new);
87 }
88
89 unsigned
90 mir_use_count(compiler_context *ctx, unsigned value)
91 {
92 unsigned used_count = 0;
93
94 mir_foreach_instr_global(ctx, ins) {
95 if (mir_has_arg(ins, value))
96 ++used_count;
97 }
98
99 return used_count;
100 }
101
102 /* Checks if a value is used only once (or totally dead), which is an important
103 * heuristic to figure out if certain optimizations are Worth It (TM) */
104
105 bool
106 mir_single_use(compiler_context *ctx, unsigned value)
107 {
108 return mir_use_count(ctx, value) <= 1;
109 }
110
111 static bool
112 mir_nontrivial_raw_mod(midgard_vector_alu_src src, bool is_int)
113 {
114 if (is_int)
115 return src.mod == midgard_int_shift;
116 else
117 return src.mod;
118 }
119
120 bool
121 mir_nontrivial_mod(midgard_vector_alu_src src, bool is_int, unsigned mask)
122 {
123 if (mir_nontrivial_raw_mod(src, is_int)) return true;
124
125 /* size-conversion */
126 if (src.half) return true;
127
128 /* swizzle */
129 for (unsigned c = 0; c < 4; ++c) {
130 if (!(mask & (1 << c))) continue;
131 if (((src.swizzle >> (2*c)) & 3) != c) return true;
132 }
133
134 return false;
135 }
136
137 bool
138 mir_nontrivial_source2_mod(midgard_instruction *ins)
139 {
140 bool is_int = midgard_is_integer_op(ins->alu.op);
141
142 midgard_vector_alu_src src2 =
143 vector_alu_from_unsigned(ins->alu.src2);
144
145 return mir_nontrivial_mod(src2, is_int, ins->mask);
146 }
147
148 bool
149 mir_nontrivial_source2_mod_simple(midgard_instruction *ins)
150 {
151 bool is_int = midgard_is_integer_op(ins->alu.op);
152
153 midgard_vector_alu_src src2 =
154 vector_alu_from_unsigned(ins->alu.src2);
155
156 return mir_nontrivial_raw_mod(src2, is_int) && !src2.half;
157 }
158
159 bool
160 mir_nontrivial_outmod(midgard_instruction *ins)
161 {
162 bool is_int = midgard_is_integer_op(ins->alu.op);
163 unsigned mod = ins->alu.outmod;
164
165 /* Pseudo-outmod */
166 if (ins->invert)
167 return true;
168
169 /* Type conversion is a sort of outmod */
170 if (ins->alu.dest_override != midgard_dest_override_none)
171 return true;
172
173 if (is_int)
174 return mod != midgard_outmod_int_wrap;
175 else
176 return mod != midgard_outmod_none;
177 }
178
179 /* Checks if an index will be used as a special register -- basically, if we're
180 * used as the input to a non-ALU op */
181
182 bool
183 mir_special_index(compiler_context *ctx, unsigned idx)
184 {
185 mir_foreach_instr_global(ctx, ins) {
186 bool is_ldst = ins->type == TAG_LOAD_STORE_4;
187 bool is_tex = ins->type == TAG_TEXTURE_4;
188
189 if (!(is_ldst || is_tex))
190 continue;
191
192 if (mir_has_arg(ins, idx))
193 return true;
194 }
195
196 return false;
197 }
198
199 /* Is a node written before a given instruction? */
200
201 bool
202 mir_is_written_before(compiler_context *ctx, midgard_instruction *ins, unsigned node)
203 {
204 if ((node < 0) || (node >= SSA_FIXED_MINIMUM))
205 return true;
206
207 mir_foreach_instr_global(ctx, q) {
208 if (q == ins)
209 break;
210
211 if (q->ssa_args.dest == node)
212 return true;
213 }
214
215 return false;
216 }
217
218 /* Creates a mask of the components of a node read by an instruction, by
219 * analyzing the swizzle with respect to the instruction's mask. E.g.:
220 *
221 * fadd r0.xz, r1.yyyy, r2.zwyx
222 *
223 * will return a mask of Z/Y for r2
224 */
225
226 static unsigned
227 mir_mask_of_read_components_single(unsigned src, unsigned outmask)
228 {
229 midgard_vector_alu_src s = vector_alu_from_unsigned(src);
230 unsigned mask = 0;
231
232 for (unsigned c = 0; c < 4; ++c) {
233 if (!(outmask & (1 << c))) continue;
234
235 unsigned comp = (s.swizzle >> (2*c)) & 3;
236 mask |= (1 << comp);
237 }
238
239 return mask;
240 }
241
242 unsigned
243 mir_mask_of_read_components(midgard_instruction *ins, unsigned node)
244 {
245 assert(ins->type == TAG_ALU_4);
246
247 unsigned mask = 0;
248
249 if (ins->ssa_args.src0 == node)
250 mask |= mir_mask_of_read_components_single(ins->alu.src1, ins->mask);
251
252 if (ins->ssa_args.src1 == node && !ins->ssa_args.inline_constant)
253 mask |= mir_mask_of_read_components_single(ins->alu.src2, ins->mask);
254
255 return mask;
256 }