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