Merge branch 'vulkan' into 'vulkan'
[mesa.git] / src / gallium / drivers / freedreno / ir3 / ir3_print.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Authors:
26 * Rob Clark <robclark@freedesktop.org>
27 */
28
29 #include <stdarg.h>
30 #include <stdio.h>
31
32 #include "ir3.h"
33
34 #define PTRID(x) ((unsigned long)(x))
35
36 static void print_instr_name(struct ir3_instruction *instr)
37 {
38 #ifdef DEBUG
39 printf("%04u:", instr->serialno);
40 #endif
41 printf("%03u: ", instr->depth);
42
43 if (instr->flags & IR3_INSTR_SY)
44 printf("(sy)");
45 if (instr->flags & IR3_INSTR_SS)
46 printf("(ss)");
47
48 if (is_meta(instr)) {
49 switch(instr->opc) {
50 case OPC_META_PHI:
51 printf("&#934;");
52 break;
53 default:
54 /* shouldn't hit here.. just for debugging: */
55 switch (instr->opc) {
56 case OPC_META_INPUT: printf("_meta:in"); break;
57 case OPC_META_FO: printf("_meta:fo"); break;
58 case OPC_META_FI: printf("_meta:fi"); break;
59
60 default: printf("_meta:%d", instr->opc); break;
61 }
62 break;
63 }
64 } else if (instr->category == 1) {
65 static const char *type[] = {
66 [TYPE_F16] = "f16",
67 [TYPE_F32] = "f32",
68 [TYPE_U16] = "u16",
69 [TYPE_U32] = "u32",
70 [TYPE_S16] = "s16",
71 [TYPE_S32] = "s32",
72 [TYPE_U8] = "u8",
73 [TYPE_S8] = "s8",
74 };
75 if (instr->cat1.src_type == instr->cat1.dst_type)
76 printf("mov");
77 else
78 printf("cov");
79 printf(".%s%s", type[instr->cat1.src_type], type[instr->cat1.dst_type]);
80 } else {
81 printf("%s", ir3_instr_name(instr));
82 if (instr->flags & IR3_INSTR_3D)
83 printf(".3d");
84 if (instr->flags & IR3_INSTR_A)
85 printf(".a");
86 if (instr->flags & IR3_INSTR_O)
87 printf(".o");
88 if (instr->flags & IR3_INSTR_P)
89 printf(".p");
90 if (instr->flags & IR3_INSTR_S)
91 printf(".s");
92 if (instr->flags & IR3_INSTR_S2EN)
93 printf(".s2en");
94 }
95 }
96
97 static void print_reg_name(struct ir3_register *reg)
98 {
99 if ((reg->flags & (IR3_REG_FABS | IR3_REG_SABS)) &&
100 (reg->flags & (IR3_REG_FNEG | IR3_REG_SNEG | IR3_REG_BNOT)))
101 printf("(absneg)");
102 else if (reg->flags & (IR3_REG_FNEG | IR3_REG_SNEG | IR3_REG_BNOT))
103 printf("(neg)");
104 else if (reg->flags & (IR3_REG_FABS | IR3_REG_SABS))
105 printf("(abs)");
106
107 if (reg->flags & IR3_REG_IMMED) {
108 printf("imm[%f,%d,0x%x]", reg->fim_val, reg->iim_val, reg->iim_val);
109 } else if (reg->flags & IR3_REG_ARRAY) {
110 printf("arr[id=%u, offset=%d, size=%u", reg->array.id,
111 reg->array.offset, reg->size);
112 /* for ARRAY we could have null src, for example first write
113 * instruction..
114 */
115 if (reg->instr) {
116 printf(", _[");
117 print_instr_name(reg->instr);
118 printf("]");
119 }
120 printf("]");
121 } else if (reg->flags & IR3_REG_SSA) {
122 printf("_[");
123 print_instr_name(reg->instr);
124 printf("]");
125 } else if (reg->flags & IR3_REG_RELATIV) {
126 if (reg->flags & IR3_REG_HALF)
127 printf("h");
128 if (reg->flags & IR3_REG_CONST)
129 printf("c<a0.x + %d>", reg->array.offset);
130 else
131 printf("\x1b[0;31mr<a0.x + %d>\x1b[0m (%u)", reg->array.offset, reg->size);
132 } else {
133 if (reg->flags & IR3_REG_HALF)
134 printf("h");
135 if (reg->flags & IR3_REG_CONST)
136 printf("c%u.%c", reg_num(reg), "xyzw"[reg_comp(reg)]);
137 else
138 printf("\x1b[0;31mr%u.%c\x1b[0m", reg_num(reg), "xyzw"[reg_comp(reg)]);
139 }
140 }
141
142 static void
143 tab(int lvl)
144 {
145 for (int i = 0; i < lvl; i++)
146 printf("\t");
147 }
148
149 static uint32_t
150 block_id(struct ir3_block *block)
151 {
152 #ifdef DEBUG
153 return block->serialno;
154 #else
155 return (uint32_t)(unsigned long)block;
156 #endif
157 }
158
159 static void
160 print_instr(struct ir3_instruction *instr, int lvl)
161 {
162 unsigned i;
163
164 tab(lvl);
165
166 print_instr_name(instr);
167 for (i = 0; i < instr->regs_count; i++) {
168 struct ir3_register *reg = instr->regs[i];
169 printf(i ? ", " : " ");
170 print_reg_name(reg);
171 }
172
173 if (instr->address) {
174 printf(", address=_");
175 printf("[");
176 print_instr_name(instr->address);
177 printf("]");
178 }
179
180 if (instr->cp.left) {
181 printf(", left=_");
182 printf("[");
183 print_instr_name(instr->cp.left);
184 printf("]");
185 }
186
187 if (instr->cp.right) {
188 printf(", right=_");
189 printf("[");
190 print_instr_name(instr->cp.right);
191 printf("]");
192 }
193
194 if (is_meta(instr)) {
195 if (instr->opc == OPC_META_FO) {
196 printf(", off=%d", instr->fo.off);
197 }
198 }
199
200 if (is_flow(instr) && instr->cat0.target) {
201 /* the predicate register src is implied: */
202 if (instr->opc == OPC_BR) {
203 printf(" %sp0.x", instr->cat0.inv ? "!" : "");
204 }
205 printf(", target=block%u", block_id(instr->cat0.target));
206 }
207
208 printf("\n");
209 }
210
211 void ir3_print_instr(struct ir3_instruction *instr)
212 {
213 print_instr(instr, 0);
214 }
215
216 static void
217 print_block(struct ir3_block *block, int lvl)
218 {
219 tab(lvl); printf("block%u {\n", block_id(block));
220 list_for_each_entry (struct ir3_instruction, instr, &block->instr_list, node) {
221 print_instr(instr, lvl+1);
222 }
223 if (block->successors[1]) {
224 /* leading into if/else: */
225 tab(lvl+1);
226 printf("/* succs: if _[");
227 print_instr_name(block->condition);
228 printf("] block%u; else block%u; */\n",
229 block_id(block->successors[0]),
230 block_id(block->successors[1]));
231 } else if (block->successors[0]) {
232 tab(lvl+1);
233 printf("/* succs: block%u; */\n",
234 block_id(block->successors[0]));
235 }
236 tab(lvl); printf("}\n");
237 }
238
239 void
240 ir3_print(struct ir3 *ir)
241 {
242 list_for_each_entry (struct ir3_block, block, &ir->block_list, node)
243 print_block(block, 0);
244
245 for (unsigned i = 0; i < ir->noutputs; i++) {
246 if (!ir->outputs[i])
247 continue;
248 printf("out%d: ", i);
249 print_instr(ir->outputs[i], 0);
250 }
251 }