freedreno/ir3: add input/output iterators
[mesa.git] / src / freedreno / ir3 / ir3_print.c
1 /*
2 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
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:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include <stdarg.h>
28 #include <stdio.h>
29
30 #include "ir3.h"
31
32 #define PTRID(x) ((unsigned long)(x))
33
34 /* ansi escape sequences: */
35 #define RESET "\x1b[0m"
36 #define RED "\x1b[0;31m"
37 #define GREEN "\x1b[0;32m"
38 #define BLUE "\x1b[0;34m"
39 #define MAGENTA "\x1b[0;35m"
40
41 /* syntax coloring, mostly to make it easier to see different sorts of
42 * srcs (immediate, constant, ssa, array, ...)
43 */
44 #define SYN_REG(x) RED x RESET
45 #define SYN_IMMED(x) GREEN x RESET
46 #define SYN_CONST(x) GREEN x RESET
47 #define SYN_SSA(x) BLUE x RESET
48 #define SYN_ARRAY(x) MAGENTA x RESET
49
50 static const char *
51 type_name(type_t type)
52 {
53 static const char *type_names[] = {
54 [TYPE_F16] = "f16",
55 [TYPE_F32] = "f32",
56 [TYPE_U16] = "u16",
57 [TYPE_U32] = "u32",
58 [TYPE_S16] = "s16",
59 [TYPE_S32] = "s32",
60 [TYPE_U8] = "u8",
61 [TYPE_S8] = "s8",
62 };
63 return type_names[type];
64 }
65
66 static void print_instr_name(struct ir3_instruction *instr, bool flags)
67 {
68 if (!instr)
69 return;
70 #ifdef DEBUG
71 printf("%04u:", instr->serialno);
72 #endif
73 printf("%04u:", instr->name);
74 printf("%04u:", instr->ip);
75 printf("%03d:", instr->depth);
76
77 if (flags) {
78 printf("\t");
79 if (instr->flags & IR3_INSTR_SY)
80 printf("(sy)");
81 if (instr->flags & IR3_INSTR_SS)
82 printf("(ss)");
83 if (instr->flags & IR3_INSTR_JP)
84 printf("(jp)");
85 if (instr->repeat)
86 printf("(rpt%d)", instr->repeat);
87 if (instr->nop)
88 printf("(nop%d)", instr->nop);
89 if (instr->flags & IR3_INSTR_UL)
90 printf("(ul)");
91 } else {
92 printf(" ");
93 }
94
95 if (is_meta(instr)) {
96 switch (instr->opc) {
97 case OPC_META_INPUT: printf("_meta:in"); break;
98 case OPC_META_SPLIT: printf("_meta:split"); break;
99 case OPC_META_COLLECT: printf("_meta:collect"); break;
100 case OPC_META_TEX_PREFETCH: printf("_meta:tex_prefetch"); break;
101
102 /* shouldn't hit here.. just for debugging: */
103 default: printf("_meta:%d", instr->opc); break;
104 }
105 } else if (instr->opc == OPC_MOV) {
106 if (instr->cat1.src_type == instr->cat1.dst_type)
107 printf("mov");
108 else
109 printf("cov");
110 printf(".%s%s", type_name(instr->cat1.src_type),
111 type_name(instr->cat1.dst_type));
112 } else {
113 printf("%s", ir3_instr_name(instr));
114 if (instr->flags & IR3_INSTR_3D)
115 printf(".3d");
116 if (instr->flags & IR3_INSTR_A)
117 printf(".a");
118 if (instr->flags & IR3_INSTR_O)
119 printf(".o");
120 if (instr->flags & IR3_INSTR_P)
121 printf(".p");
122 if (instr->flags & IR3_INSTR_S)
123 printf(".s");
124 if (instr->flags & IR3_INSTR_S2EN)
125 printf(".s2en");
126 }
127 }
128
129 static void print_reg_name(struct ir3_register *reg)
130 {
131 if ((reg->flags & (IR3_REG_FABS | IR3_REG_SABS)) &&
132 (reg->flags & (IR3_REG_FNEG | IR3_REG_SNEG | IR3_REG_BNOT)))
133 printf("(absneg)");
134 else if (reg->flags & (IR3_REG_FNEG | IR3_REG_SNEG | IR3_REG_BNOT))
135 printf("(neg)");
136 else if (reg->flags & (IR3_REG_FABS | IR3_REG_SABS))
137 printf("(abs)");
138
139 if (reg->flags & IR3_REG_HIGH)
140 printf("H");
141 if (reg->flags & IR3_REG_HALF)
142 printf("h");
143
144 if (reg->flags & IR3_REG_IMMED) {
145 printf(SYN_IMMED("imm[%f,%d,0x%x]"), reg->fim_val, reg->iim_val, reg->iim_val);
146 } else if (reg->flags & IR3_REG_ARRAY) {
147 printf(SYN_ARRAY("arr[id=%u, offset=%d, size=%u"), reg->array.id,
148 reg->array.offset, reg->size);
149 /* for ARRAY we could have null src, for example first write
150 * instruction..
151 */
152 if (reg->instr) {
153 printf(SYN_ARRAY(", "));
154 printf(SYN_SSA("_["));
155 print_instr_name(reg->instr, false);
156 printf(SYN_SSA("]"));
157 }
158 printf(SYN_ARRAY("]"));
159 } else if (reg->flags & IR3_REG_SSA) {
160 printf(SYN_SSA("_["));
161 print_instr_name(reg->instr, false);
162 printf(SYN_SSA("]"));
163 } else if (reg->flags & IR3_REG_RELATIV) {
164 if (reg->flags & IR3_REG_CONST)
165 printf(SYN_CONST("c<a0.x + %d>"), reg->array.offset);
166 else
167 printf(SYN_REG("r<a0.x + %d>")" (%u)", reg->array.offset, reg->size);
168 } else {
169 if (reg->flags & IR3_REG_CONST)
170 printf(SYN_CONST("c%u.%c"), reg_num(reg), "xyzw"[reg_comp(reg)]);
171 else
172 printf(SYN_REG("r%u.%c"), reg_num(reg), "xyzw"[reg_comp(reg)]);
173 }
174
175 if (reg->wrmask > 0x1)
176 printf(" (wrmask=0x%x)", reg->wrmask);
177 }
178
179 static void
180 tab(int lvl)
181 {
182 for (int i = 0; i < lvl; i++)
183 printf("\t");
184 }
185
186 static void
187 print_instr(struct ir3_instruction *instr, int lvl)
188 {
189 unsigned i;
190
191 tab(lvl);
192
193 print_instr_name(instr, true);
194
195 if (is_tex(instr)) {
196 printf(" (%s)(", type_name(instr->cat5.type));
197 for (i = 0; i < 4; i++)
198 if (instr->regs[0]->wrmask & (1 << i))
199 printf("%c", "xyzw"[i]);
200 printf(")");
201 } else if (instr->regs_count > 0) {
202 printf(" ");
203 }
204
205 for (i = 0; i < instr->regs_count; i++) {
206 struct ir3_register *reg = instr->regs[i];
207
208 /* skip the samp/tex src if it has been lowered to immed: */
209 if ((i == 1) && is_tex(instr) && !(instr->flags & IR3_INSTR_S2EN))
210 continue;
211
212 printf(i ? ", " : "");
213 print_reg_name(reg);
214 }
215
216 if (is_tex(instr) && !(instr->flags & IR3_INSTR_S2EN))
217 printf(", s#%d, t#%d", instr->cat5.samp, instr->cat5.tex);
218
219 if (instr->address) {
220 printf(", address=_");
221 printf("[");
222 print_instr_name(instr->address, false);
223 printf("]");
224 }
225
226 if (instr->cp.left) {
227 printf(", left=_");
228 printf("[");
229 print_instr_name(instr->cp.left, false);
230 printf("]");
231 }
232
233 if (instr->cp.right) {
234 printf(", right=_");
235 printf("[");
236 print_instr_name(instr->cp.right, false);
237 printf("]");
238 }
239
240 if (instr->opc == OPC_META_SPLIT) {
241 printf(", off=%d", instr->split.off);
242 } else if (instr->opc == OPC_META_TEX_PREFETCH) {
243 printf(", tex=%d, samp=%d, input_offset=%d", instr->prefetch.tex,
244 instr->prefetch.samp, instr->prefetch.input_offset);
245 }
246
247 if (is_flow(instr) && instr->cat0.target) {
248 /* the predicate register src is implied: */
249 if (instr->opc == OPC_BR) {
250 printf(" %sp0.x", instr->cat0.inv ? "!" : "");
251 }
252 printf(", target=block%u", block_id(instr->cat0.target));
253 }
254
255 if (instr->deps_count) {
256 printf(", false-deps:");
257 for (unsigned i = 0; i < instr->deps_count; i++) {
258 if (i > 0)
259 printf(", ");
260 printf("_[");
261 print_instr_name(instr->deps[i], false);
262 printf("]");
263 }
264 }
265
266 printf("\n");
267 }
268
269 void ir3_print_instr(struct ir3_instruction *instr)
270 {
271 print_instr(instr, 0);
272 }
273
274 static void
275 print_block(struct ir3_block *block, int lvl)
276 {
277 tab(lvl); printf("block%u {\n", block_id(block));
278
279 if (block->predecessors->entries > 0) {
280 unsigned i = 0;
281 tab(lvl+1);
282 printf("pred: ");
283 set_foreach(block->predecessors, entry) {
284 struct ir3_block *pred = (struct ir3_block *)entry->key;
285 if (i++)
286 printf(", ");
287 printf("block%u", block_id(pred));
288 }
289 printf("\n");
290 }
291
292 list_for_each_entry (struct ir3_instruction, instr, &block->instr_list, node) {
293 print_instr(instr, lvl+1);
294 }
295
296 tab(lvl+1); printf("/* keeps:\n");
297 for (unsigned i = 0; i < block->keeps_count; i++) {
298 print_instr(block->keeps[i], lvl+2);
299 }
300 tab(lvl+1); printf(" */\n");
301
302 if (block->successors[1]) {
303 /* leading into if/else: */
304 tab(lvl+1);
305 printf("/* succs: if _[");
306 print_instr_name(block->condition, false);
307 printf("] block%u; else block%u; */\n",
308 block_id(block->successors[0]),
309 block_id(block->successors[1]));
310 } else if (block->successors[0]) {
311 tab(lvl+1);
312 printf("/* succs: block%u; */\n",
313 block_id(block->successors[0]));
314 }
315 tab(lvl); printf("}\n");
316 }
317
318 void
319 ir3_print(struct ir3 *ir)
320 {
321 list_for_each_entry (struct ir3_block, block, &ir->block_list, node)
322 print_block(block, 0);
323
324 struct ir3_instruction *out;
325 foreach_output_n(out, i, ir) {
326 printf("out%d: ", i);
327 print_instr(out, 0);
328 }
329 }