415024b985f0576ac05a21d97951cd83712bc2dc
[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 static void print_instr_name(struct ir3_instruction *instr)
35 {
36 if (!instr)
37 return;
38 #ifdef DEBUG
39 printf("%04u:", instr->serialno);
40 #endif
41 printf("%04u:", instr->name);
42 printf("%04u:", instr->ip);
43 printf("%03u: ", instr->depth);
44
45 if (instr->flags & IR3_INSTR_SY)
46 printf("(sy)");
47 if (instr->flags & IR3_INSTR_SS)
48 printf("(ss)");
49
50 if (is_meta(instr)) {
51 switch (instr->opc) {
52 case OPC_META_INPUT: printf("_meta:in"); break;
53 case OPC_META_FO: printf("_meta:fo"); break;
54 case OPC_META_FI: printf("_meta:fi"); break;
55
56 /* shouldn't hit here.. just for debugging: */
57 default: printf("_meta:%d", instr->opc); break;
58 }
59 } else if (instr->opc == OPC_MOV) {
60 static const char *type[] = {
61 [TYPE_F16] = "f16",
62 [TYPE_F32] = "f32",
63 [TYPE_U16] = "u16",
64 [TYPE_U32] = "u32",
65 [TYPE_S16] = "s16",
66 [TYPE_S32] = "s32",
67 [TYPE_U8] = "u8",
68 [TYPE_S8] = "s8",
69 };
70 if (instr->cat1.src_type == instr->cat1.dst_type)
71 printf("mov");
72 else
73 printf("cov");
74 printf(".%s%s", type[instr->cat1.src_type], type[instr->cat1.dst_type]);
75 } else {
76 printf("%s", ir3_instr_name(instr));
77 if (instr->flags & IR3_INSTR_3D)
78 printf(".3d");
79 if (instr->flags & IR3_INSTR_A)
80 printf(".a");
81 if (instr->flags & IR3_INSTR_O)
82 printf(".o");
83 if (instr->flags & IR3_INSTR_P)
84 printf(".p");
85 if (instr->flags & IR3_INSTR_S)
86 printf(".s");
87 if (instr->flags & IR3_INSTR_S2EN)
88 printf(".s2en");
89 }
90 }
91
92 static void print_reg_name(struct ir3_register *reg)
93 {
94 if ((reg->flags & (IR3_REG_FABS | IR3_REG_SABS)) &&
95 (reg->flags & (IR3_REG_FNEG | IR3_REG_SNEG | IR3_REG_BNOT)))
96 printf("(absneg)");
97 else if (reg->flags & (IR3_REG_FNEG | IR3_REG_SNEG | IR3_REG_BNOT))
98 printf("(neg)");
99 else if (reg->flags & (IR3_REG_FABS | IR3_REG_SABS))
100 printf("(abs)");
101
102 if (reg->flags & IR3_REG_IMMED) {
103 printf("imm[%f,%d,0x%x]", reg->fim_val, reg->iim_val, reg->iim_val);
104 } else if (reg->flags & IR3_REG_ARRAY) {
105 printf("arr[id=%u, offset=%d, size=%u", reg->array.id,
106 reg->array.offset, reg->size);
107 /* for ARRAY we could have null src, for example first write
108 * instruction..
109 */
110 if (reg->instr) {
111 printf(", _[");
112 print_instr_name(reg->instr);
113 printf("]");
114 }
115 printf("]");
116 } else if (reg->flags & IR3_REG_SSA) {
117 printf("_[");
118 print_instr_name(reg->instr);
119 printf("]");
120 } else if (reg->flags & IR3_REG_RELATIV) {
121 if (reg->flags & IR3_REG_HALF)
122 printf("h");
123 if (reg->flags & IR3_REG_CONST)
124 printf("c<a0.x + %d>", reg->array.offset);
125 else
126 printf("\x1b[0;31mr<a0.x + %d>\x1b[0m (%u)", reg->array.offset, reg->size);
127 } else {
128 if (reg->flags & IR3_REG_HIGH)
129 printf("H");
130 if (reg->flags & IR3_REG_HALF)
131 printf("h");
132 if (reg->flags & IR3_REG_CONST)
133 printf("c%u.%c", reg_num(reg), "xyzw"[reg_comp(reg)]);
134 else
135 printf("\x1b[0;31mr%u.%c\x1b[0m", reg_num(reg), "xyzw"[reg_comp(reg)]);
136 }
137 }
138
139 static void
140 tab(int lvl)
141 {
142 for (int i = 0; i < lvl; i++)
143 printf("\t");
144 }
145
146 static void
147 print_instr(struct ir3_instruction *instr, int lvl)
148 {
149 unsigned i;
150
151 tab(lvl);
152
153 print_instr_name(instr);
154 for (i = 0; i < instr->regs_count; i++) {
155 struct ir3_register *reg = instr->regs[i];
156 printf(i ? ", " : " ");
157 print_reg_name(reg);
158 }
159
160 if (instr->address) {
161 printf(", address=_");
162 printf("[");
163 print_instr_name(instr->address);
164 printf("]");
165 }
166
167 if (instr->cp.left) {
168 printf(", left=_");
169 printf("[");
170 print_instr_name(instr->cp.left);
171 printf("]");
172 }
173
174 if (instr->cp.right) {
175 printf(", right=_");
176 printf("[");
177 print_instr_name(instr->cp.right);
178 printf("]");
179 }
180
181 if (instr->opc == OPC_META_FO) {
182 printf(", off=%d", instr->fo.off);
183 }
184
185 if (is_flow(instr) && instr->cat0.target) {
186 /* the predicate register src is implied: */
187 if (instr->opc == OPC_BR) {
188 printf(" %sp0.x", instr->cat0.inv ? "!" : "");
189 }
190 printf(", target=block%u", block_id(instr->cat0.target));
191 }
192
193 if (instr->deps_count) {
194 printf(", false-deps:");
195 for (unsigned i = 0; i < instr->deps_count; i++) {
196 if (i > 0)
197 printf(", ");
198 printf("_[");
199 print_instr_name(instr->deps[i]);
200 printf("]");
201 }
202 }
203
204 printf("\n");
205 }
206
207 void ir3_print_instr(struct ir3_instruction *instr)
208 {
209 print_instr(instr, 0);
210 }
211
212 static void
213 print_block(struct ir3_block *block, int lvl)
214 {
215 tab(lvl); printf("block%u {\n", block_id(block));
216
217 if (block->predecessors_count > 0) {
218 tab(lvl+1);
219 printf("pred: ");
220 for (unsigned i = 0; i < block->predecessors_count; i++) {
221 if (i)
222 printf(", ");
223 printf("block%u", block_id(block->predecessors[i]));
224 }
225 printf("\n");
226 }
227
228 list_for_each_entry (struct ir3_instruction, instr, &block->instr_list, node) {
229 print_instr(instr, lvl+1);
230 }
231
232 tab(lvl+1); printf("/* keeps:\n");
233 for (unsigned i = 0; i < block->keeps_count; i++) {
234 print_instr(block->keeps[i], lvl+2);
235 }
236 tab(lvl+1); printf(" */\n");
237
238 if (block->successors[1]) {
239 /* leading into if/else: */
240 tab(lvl+1);
241 printf("/* succs: if _[");
242 print_instr_name(block->condition);
243 printf("] block%u; else block%u; */\n",
244 block_id(block->successors[0]),
245 block_id(block->successors[1]));
246 } else if (block->successors[0]) {
247 tab(lvl+1);
248 printf("/* succs: block%u; */\n",
249 block_id(block->successors[0]));
250 }
251 tab(lvl); printf("}\n");
252 }
253
254 void
255 ir3_print(struct ir3 *ir)
256 {
257 list_for_each_entry (struct ir3_block, block, &ir->block_list, node)
258 print_block(block, 0);
259
260 for (unsigned i = 0; i < ir->noutputs; i++) {
261 if (!ir->outputs[i])
262 continue;
263 printf("out%d: ", i);
264 print_instr(ir->outputs[i], 0);
265 }
266 }