/src/php-src/ext/opcache/jit/ir/ir_dump.c
Line | Count | Source |
1 | | /* |
2 | | * IR - Lightweight JIT Compilation Framework |
3 | | * (debug dumps) |
4 | | * Copyright (C) 2022 Zend by Perforce. |
5 | | * Authors: Dmitry Stogov <dmitry@php.net> |
6 | | */ |
7 | | |
8 | | #include "ir.h" |
9 | | #include "ir_private.h" |
10 | | |
11 | | void ir_dump(const ir_ctx *ctx, FILE *f) |
12 | 0 | { |
13 | 0 | ir_ref i, j, n, ref, *p; |
14 | 0 | ir_insn *insn; |
15 | 0 | uint32_t flags; |
16 | |
|
17 | 0 | for (i = 1 - ctx->consts_count, insn = ctx->ir_base + i; i < IR_UNUSED; i++, insn++) { |
18 | 0 | fprintf(f, "%05d %s %s(", i, ir_op_name[insn->op], ir_type_name[insn->type]); |
19 | 0 | ir_print_const(ctx, insn, f, true); |
20 | 0 | fprintf(f, ")\n"); |
21 | 0 | } |
22 | |
|
23 | 0 | for (i = IR_UNUSED + 1, insn = ctx->ir_base + i; i < ctx->insns_count; i++, insn++) { |
24 | 0 | flags = ir_op_flags[insn->op]; |
25 | 0 | fprintf(f, "%05d %s", i, ir_op_name[insn->op]); |
26 | 0 | if ((flags & IR_OP_FLAG_DATA) || ((flags & IR_OP_FLAG_MEM) && insn->type != IR_VOID)) { |
27 | 0 | fprintf(f, " %s", ir_type_name[insn->type]); |
28 | 0 | } |
29 | 0 | n = ir_operands_count(ctx, insn); |
30 | 0 | for (j = 1, p = insn->ops + 1; j <= 3; j++, p++) { |
31 | 0 | ref = *p; |
32 | 0 | if (ref) { |
33 | 0 | fprintf(f, " %05d", ref); |
34 | 0 | } |
35 | 0 | } |
36 | 0 | if (n > 3) { |
37 | 0 | n -= 3; |
38 | 0 | do { |
39 | 0 | i++; |
40 | 0 | insn++; |
41 | 0 | fprintf(f, "\n%05d", i); |
42 | 0 | for (j = 0; j < 4; j++, p++) { |
43 | 0 | ref = *p; |
44 | 0 | if (ref) { |
45 | 0 | fprintf(f, " %05d", ref); |
46 | 0 | } |
47 | 0 | } |
48 | 0 | n -= 4; |
49 | 0 | } while (n > 0); |
50 | 0 | } |
51 | 0 | fprintf(f, "\n"); |
52 | 0 | } |
53 | 0 | } |
54 | | |
55 | | void ir_dump_dot(const ir_ctx *ctx, const char *name, FILE *f) |
56 | 0 | { |
57 | 0 | int DATA_WEIGHT = 0; |
58 | 0 | int CONTROL_WEIGHT = 5; |
59 | 0 | int REF_WEIGHT = 4; |
60 | 0 | ir_ref i, j, n, ref, *p; |
61 | 0 | ir_insn *insn; |
62 | 0 | uint32_t flags; |
63 | |
|
64 | 0 | fprintf(f, "digraph %s {\n", name); |
65 | 0 | fprintf(f, "\trankdir=TB;\n"); |
66 | 0 | for (i = 1 - ctx->consts_count, insn = ctx->ir_base + i; i < IR_UNUSED; i++, insn++) { |
67 | 0 | fprintf(f, "\tc%d [label=\"C%d: CONST %s(", -i, -i, ir_type_name[insn->type]); |
68 | | /* FIXME(tony): We still cannot handle strings with escaped double quote inside */ |
69 | 0 | ir_print_const(ctx, insn, f, false); |
70 | 0 | fprintf(f, ")\",style=filled,fillcolor=yellow];\n"); |
71 | 0 | } |
72 | |
|
73 | 0 | for (i = IR_UNUSED + 1, insn = ctx->ir_base + i; i < ctx->insns_count;) { |
74 | 0 | flags = ir_op_flags[insn->op]; |
75 | 0 | if (flags & IR_OP_FLAG_CONTROL) { |
76 | 0 | if (insn->op == IR_START) { |
77 | 0 | fprintf(f, "\t{rank=min; n%d [label=\"%d: %s\",shape=box,style=\"rounded,filled\",fillcolor=red];}\n", i, i, ir_op_name[insn->op]); |
78 | 0 | } else if (insn->op == IR_ENTRY) { |
79 | 0 | fprintf(f, "\t{n%d [label=\"%d: %s\",shape=box,style=\"rounded,filled\",fillcolor=red];}\n", i, i, ir_op_name[insn->op]); |
80 | 0 | } else if (flags & IR_OP_FLAG_TERMINATOR) { |
81 | 0 | fprintf(f, "\t{rank=max; n%d [label=\"%d: %s\",shape=box,style=\"rounded,filled\",fillcolor=red];}\n", i, i, ir_op_name[insn->op]); |
82 | 0 | } else if (flags & IR_OP_FLAG_MEM) { |
83 | 0 | fprintf(f, "\tn%d [label=\"%d: %s\",shape=box,style=filled,fillcolor=pink];\n", i, i, ir_op_name[insn->op]); |
84 | 0 | } else { |
85 | 0 | fprintf(f, "\tn%d [label=\"%d: %s\",shape=box,style=filled,fillcolor=lightcoral];\n", i, i, ir_op_name[insn->op]); |
86 | 0 | } |
87 | 0 | } else if (flags & IR_OP_FLAG_DATA) { |
88 | 0 | if (IR_OPND_KIND(flags, 1) == IR_OPND_DATA) { |
89 | | /* not a leaf */ |
90 | 0 | fprintf(f, "\tn%d [label=\"%d: %s\"", i, i, ir_op_name[insn->op]); |
91 | 0 | fprintf(f, ",shape=diamond,style=filled,fillcolor=deepskyblue];\n"); |
92 | 0 | } else { |
93 | 0 | if (insn->op == IR_PARAM) { |
94 | 0 | fprintf(f, "\tn%d [label=\"%d: %s %s \\\"%s\\\"\",style=filled,fillcolor=lightblue];\n", |
95 | 0 | i, i, ir_op_name[insn->op], ir_type_name[insn->type], ir_get_str(ctx, insn->op2)); |
96 | 0 | } else if (insn->op == IR_VAR) { |
97 | 0 | fprintf(f, "\tn%d [label=\"%d: %s %s \\\"%s\\\"\"];\n", i, i, ir_op_name[insn->op], ir_type_name[insn->type], ir_get_str(ctx, insn->op2)); |
98 | 0 | } else { |
99 | 0 | fprintf(f, "\tn%d [label=\"%d: %s %s\",style=filled,fillcolor=deepskyblue];\n", i, i, ir_op_name[insn->op], ir_type_name[insn->type]); |
100 | 0 | } |
101 | 0 | } |
102 | 0 | } |
103 | 0 | n = ir_operands_count(ctx, insn); |
104 | 0 | for (j = 1, p = insn->ops + 1; j <= n; j++, p++) { |
105 | 0 | ref = *p; |
106 | 0 | if (ref) { |
107 | 0 | switch (IR_OPND_KIND(flags, j)) { |
108 | 0 | case IR_OPND_DATA: |
109 | 0 | if (IR_IS_CONST_REF(ref)) { |
110 | 0 | fprintf(f, "\tc%d -> n%d [color=blue,weight=%d];\n", -ref, i, DATA_WEIGHT); |
111 | 0 | } else if (insn->op == IR_PHI |
112 | 0 | && ctx->ir_base[insn->op1].op == IR_LOOP_BEGIN |
113 | 0 | && ctx->ir_base[ir_insn_op(&ctx->ir_base[insn->op1], j - 1)].op == IR_LOOP_END) { |
114 | 0 | fprintf(f, "\tn%d -> n%d [color=blue,dir=back];\n", i, ref); |
115 | 0 | } else { |
116 | 0 | fprintf(f, "\tn%d -> n%d [color=blue,weight=%d];\n", ref, i, DATA_WEIGHT); |
117 | 0 | } |
118 | 0 | break; |
119 | 0 | case IR_OPND_CONTROL: |
120 | 0 | if (insn->op == IR_LOOP_BEGIN && ctx->ir_base[ref].op == IR_LOOP_END) { |
121 | 0 | fprintf(f, "\tn%d -> n%d [style=bold,color=red,dir=back];\n", i, ref); |
122 | 0 | } else if (insn->op == IR_ENTRY) { |
123 | 0 | fprintf(f, "\tn%d -> n%d [style=bold,color=red,style=dashed,weight=%d];\n", ref, i, CONTROL_WEIGHT); |
124 | 0 | } else { |
125 | 0 | fprintf(f, "\tn%d -> n%d [style=bold,color=red,weight=%d];\n", ref, i, CONTROL_WEIGHT); |
126 | 0 | } |
127 | 0 | break; |
128 | 0 | case IR_OPND_CONTROL_DEP: |
129 | 0 | case IR_OPND_CONTROL_REF: |
130 | 0 | fprintf(f, "\tn%d -> n%d [style=dashed,dir=back,weight=%d];\n", ref, i, REF_WEIGHT); |
131 | 0 | break; |
132 | 0 | } |
133 | 0 | } |
134 | 0 | } |
135 | 0 | n = ir_insn_inputs_to_len(n); |
136 | 0 | i += n; |
137 | 0 | insn += n; |
138 | 0 | } |
139 | 0 | fprintf(f, "}\n"); |
140 | 0 | } |
141 | | |
142 | | void ir_dump_use_lists(const ir_ctx *ctx, FILE *f) |
143 | 0 | { |
144 | 0 | ir_ref i, j, n, *p; |
145 | 0 | ir_use_list *list; |
146 | |
|
147 | 0 | if (ctx->use_lists) { |
148 | 0 | fprintf(f, "{ # Use Lists\n"); |
149 | 0 | for (i = 1, list = &ctx->use_lists[1]; i < ctx->insns_count; i++, list++) { |
150 | 0 | n = list->count; |
151 | 0 | if (n > 0) { |
152 | 0 | p = &ctx->use_edges[list->refs]; |
153 | 0 | fprintf(f, "%05d(%d): [%05d", i, n, *p); |
154 | 0 | p++; |
155 | 0 | for (j = 1; j < n; j++, p++) { |
156 | 0 | fprintf(f, ", %05d", *p); |
157 | 0 | } |
158 | 0 | fprintf(f, "]\n"); |
159 | 0 | } |
160 | 0 | } |
161 | 0 | fprintf(f, "}\n"); |
162 | 0 | } |
163 | 0 | } |
164 | | |
165 | | static void ir_dump_dessa_moves(const ir_ctx *ctx, int b, ir_block *bb, FILE *f) |
166 | 0 | { |
167 | 0 | uint32_t succ; |
168 | 0 | ir_block *succ_bb; |
169 | 0 | ir_use_list *use_list; |
170 | 0 | ir_ref k, i, *p, use_ref, input; |
171 | 0 | ir_insn *use_insn; |
172 | |
|
173 | 0 | IR_ASSERT(bb->successors_count == 1); |
174 | 0 | succ = ctx->cfg_edges[bb->successors]; |
175 | 0 | succ_bb = &ctx->cfg_blocks[succ]; |
176 | 0 | IR_ASSERT(succ_bb->predecessors_count > 1); |
177 | 0 | use_list = &ctx->use_lists[succ_bb->start]; |
178 | 0 | k = ir_phi_input_number(ctx, succ_bb, b); |
179 | |
|
180 | 0 | for (i = use_list->count, p = &ctx->use_edges[use_list->refs]; i > 0; p++, i--) { |
181 | 0 | use_ref = *p; |
182 | 0 | use_insn = &ctx->ir_base[use_ref]; |
183 | 0 | if (use_insn->op == IR_PHI) { |
184 | 0 | input = ir_insn_op(use_insn, k); |
185 | 0 | if (IR_IS_CONST_REF(input)) { |
186 | 0 | fprintf(f, "\t# DESSA MOV c_%d", -input); |
187 | 0 | } else if (ctx->vregs[input] != ctx->vregs[use_ref]) { |
188 | 0 | fprintf(f, "\t# DESSA MOV d_%d {R%d}", input, ctx->vregs[input]); |
189 | 0 | } else { |
190 | 0 | continue; |
191 | 0 | } |
192 | 0 | if (ctx->regs) { |
193 | 0 | int8_t *regs = ctx->regs[use_ref]; |
194 | 0 | int8_t reg = regs[k]; |
195 | 0 | if (reg != IR_REG_NONE) { |
196 | 0 | fprintf(f, " {%%%s%s}", ir_reg_name(IR_REG_NUM(reg), ctx->ir_base[input].type), |
197 | 0 | (reg & (IR_REG_SPILL_LOAD|IR_REG_SPILL_SPECIAL)) ? ":load" : ""); |
198 | 0 | } |
199 | 0 | } |
200 | 0 | fprintf(f, " -> d_%d {R%d}", use_ref, ctx->vregs[use_ref]); |
201 | 0 | if (ctx->regs) { |
202 | 0 | int8_t reg = ctx->regs[use_ref][0]; |
203 | 0 | if (reg != IR_REG_NONE) { |
204 | 0 | fprintf(f, " {%%%s%s}", ir_reg_name(IR_REG_NUM(reg), ctx->ir_base[use_ref].type), |
205 | 0 | (reg & (IR_REG_SPILL_STORE|IR_REG_SPILL_SPECIAL)) ? ":store" : ""); |
206 | 0 | } |
207 | 0 | } |
208 | 0 | fprintf(f, "\n"); |
209 | 0 | } |
210 | 0 | } |
211 | 0 | } |
212 | | |
213 | | static void ir_dump_cfg_block(ir_ctx *ctx, FILE *f, uint32_t b, ir_block *bb) |
214 | 0 | { |
215 | 0 | fprintf(f, "BB%d:\n", b); |
216 | 0 | fprintf(f, "\tstart=%d\n", bb->start); |
217 | 0 | fprintf(f, "\tend=%d\n", bb->end); |
218 | 0 | if (bb->successors_count) { |
219 | 0 | uint32_t i; |
220 | |
|
221 | 0 | fprintf(f, "\tsuccessors(%d) [BB%d", bb->successors_count, ctx->cfg_edges[bb->successors]); |
222 | 0 | for (i = 1; i < bb->successors_count; i++) { |
223 | 0 | fprintf(f, ", BB%d", ctx->cfg_edges[bb->successors + i]); |
224 | 0 | } |
225 | 0 | fprintf(f, "]\n"); |
226 | 0 | } |
227 | 0 | if (bb->predecessors_count) { |
228 | 0 | uint32_t i; |
229 | |
|
230 | 0 | fprintf(f, "\tpredecessors(%d) [BB%d", bb->predecessors_count, ctx->cfg_edges[bb->predecessors]); |
231 | 0 | for (i = 1; i < bb->predecessors_count; i++) { |
232 | 0 | fprintf(f, ", BB%d", ctx->cfg_edges[bb->predecessors + i]); |
233 | 0 | } |
234 | 0 | fprintf(f, "]\n"); |
235 | 0 | } |
236 | 0 | if (bb->dom_parent > 0) { |
237 | 0 | fprintf(f, "\tdom_parent=BB%d\n", bb->dom_parent); |
238 | 0 | } |
239 | 0 | fprintf(f, "\tdom_depth=%d\n", bb->dom_depth); |
240 | 0 | if (bb->dom_child > 0) { |
241 | 0 | int child = bb->dom_child; |
242 | 0 | fprintf(f, "\tdom_children [BB%d", child); |
243 | 0 | child = ctx->cfg_blocks[child].dom_next_child; |
244 | 0 | while (child > 0) { |
245 | 0 | fprintf(f, ", BB%d", child); |
246 | 0 | child = ctx->cfg_blocks[child].dom_next_child; |
247 | 0 | } |
248 | 0 | fprintf(f, "]\n"); |
249 | 0 | } |
250 | 0 | if (bb->flags & IR_BB_ENTRY) { |
251 | 0 | fprintf(f, "\tENTRY\n"); |
252 | 0 | } |
253 | 0 | if (bb->flags & IR_BB_UNREACHABLE) { |
254 | 0 | fprintf(f, "\tUNREACHABLE\n"); |
255 | 0 | } |
256 | 0 | if (bb->flags & IR_BB_LOOP_HEADER) { |
257 | 0 | if (bb->flags & IR_BB_LOOP_WITH_ENTRY) { |
258 | 0 | fprintf(f, "\tLOOP_HEADER, LOOP_WITH_ENTRY\n"); |
259 | 0 | } else { |
260 | 0 | fprintf(f, "\tLOOP_HEADER\n"); |
261 | 0 | } |
262 | 0 | } |
263 | 0 | if (bb->flags & IR_BB_IRREDUCIBLE_LOOP) { |
264 | 0 | fprintf(stderr, "\tIRREDUCIBLE_LOOP\n"); |
265 | 0 | } |
266 | 0 | if (bb->loop_header > 0) { |
267 | 0 | fprintf(f, "\tloop_header=BB%d\n", bb->loop_header); |
268 | 0 | } |
269 | 0 | if (bb->loop_depth != 0) { |
270 | 0 | fprintf(f, "\tloop_depth=%d\n", bb->loop_depth); |
271 | 0 | } |
272 | 0 | if (bb->flags & IR_BB_OSR_ENTRY_LOADS) { |
273 | 0 | ir_list *list = (ir_list*)ctx->osr_entry_loads; |
274 | 0 | uint32_t pos = 0, i, count; |
275 | |
|
276 | 0 | IR_ASSERT(list); |
277 | 0 | while (1) { |
278 | 0 | i = ir_list_at(list, pos); |
279 | 0 | if (b == i) { |
280 | 0 | break; |
281 | 0 | } |
282 | 0 | IR_ASSERT(i != 0); /* end marker */ |
283 | 0 | pos++; |
284 | 0 | count = ir_list_at(list, pos); |
285 | 0 | pos += count + 1; |
286 | 0 | } |
287 | 0 | pos++; |
288 | 0 | count = ir_list_at(list, pos); |
289 | 0 | pos++; |
290 | |
|
291 | 0 | for (i = 0; i < count; i++, pos++) { |
292 | 0 | ir_ref ref = ir_list_at(list, pos); |
293 | 0 | fprintf(f, "\tOSR_ENTRY_LOAD=d_%d\n", ref); |
294 | 0 | } |
295 | 0 | } |
296 | 0 | if (bb->flags & IR_BB_DESSA_MOVES) { |
297 | 0 | ir_dump_dessa_moves(ctx, b, bb, f); |
298 | 0 | } |
299 | 0 | } |
300 | | |
301 | | void ir_dump_cfg(ir_ctx *ctx, FILE *f) |
302 | 0 | { |
303 | 0 | if (ctx->cfg_blocks) { |
304 | 0 | uint32_t b, i, bb_count = ctx->cfg_blocks_count; |
305 | 0 | ir_block *bb = ctx->cfg_blocks + 1; |
306 | |
|
307 | 0 | fprintf(f, "{ # CFG\n"); |
308 | 0 | if (ctx->cfg_schedule) { |
309 | 0 | for (i = 1; i <= bb_count; i++) { |
310 | 0 | b = ctx->cfg_schedule[i]; |
311 | 0 | bb = &ctx->cfg_blocks[b]; |
312 | 0 | ir_dump_cfg_block(ctx, f, b, bb); |
313 | 0 | } |
314 | 0 | } else { |
315 | 0 | for (b = 1; b <= bb_count; b++, bb++) { |
316 | 0 | ir_dump_cfg_block(ctx, f, b, bb); |
317 | 0 | } |
318 | 0 | } |
319 | 0 | fprintf(f, "}\n"); |
320 | 0 | } |
321 | 0 | } |
322 | | |
323 | | void ir_dump_cfg_map(const ir_ctx *ctx, FILE *f) |
324 | 0 | { |
325 | 0 | ir_ref i; |
326 | 0 | uint32_t *_blocks = ctx->cfg_map; |
327 | |
|
328 | 0 | if (_blocks) { |
329 | 0 | fprintf(f, "{ # CFG map (insn -> bb)\n"); |
330 | 0 | for (i = IR_UNUSED + 1; i < ctx->insns_count; i++) { |
331 | 0 | fprintf(f, "%d -> %d\n", i, _blocks[i]); |
332 | 0 | } |
333 | 0 | fprintf(f, "}\n"); |
334 | 0 | } |
335 | 0 | } |
336 | | |
337 | | void ir_dump_live_ranges(const ir_ctx *ctx, FILE *f) |
338 | 0 | { |
339 | 0 | ir_ref i, j, n; |
340 | |
|
341 | 0 | if (!ctx->live_intervals) { |
342 | 0 | return; |
343 | 0 | } |
344 | 0 | fprintf(f, "{ # LIVE-RANGES (vregs_count=%d)\n", ctx->vregs_count); |
345 | 0 | for (i = 0; i <= ctx->vregs_count; i++) { |
346 | 0 | ir_live_interval *ival = ctx->live_intervals[i]; |
347 | |
|
348 | 0 | if (ival) { |
349 | 0 | ir_live_range *p; |
350 | 0 | ir_use_pos *use_pos; |
351 | |
|
352 | 0 | if (i == 0) { |
353 | 0 | fprintf(f, "TMP"); |
354 | 0 | } else { |
355 | 0 | for (j = 1; j < ctx->insns_count; j++) { |
356 | 0 | if (ctx->vregs[j] == (uint32_t)i) { |
357 | 0 | break; |
358 | 0 | } |
359 | 0 | } |
360 | 0 | fprintf(f, "R%d (d_%d", i, j); |
361 | 0 | for (j++; j < ctx->insns_count; j++) { |
362 | 0 | if (ctx->vregs[j] == (uint32_t)i) { |
363 | 0 | fprintf(f, ", d_%d", j); |
364 | 0 | } |
365 | 0 | } |
366 | 0 | fprintf(f, ")"); |
367 | 0 | if (ival->stack_spill_pos != -1) { |
368 | 0 | if (ival->flags & IR_LIVE_INTERVAL_SPILL_SPECIAL) { |
369 | 0 | IR_ASSERT(ctx->spill_base >= 0); |
370 | 0 | fprintf(f, " [SPILL=0x%x(%%%s)]", ival->stack_spill_pos, ir_reg_name(ctx->spill_base, IR_ADDR)); |
371 | 0 | } else { |
372 | 0 | fprintf(f, " [SPILL=0x%x]", ival->stack_spill_pos); |
373 | 0 | } |
374 | 0 | } |
375 | 0 | } |
376 | 0 | if (ival->next) { |
377 | 0 | fprintf(f, "\n\t"); |
378 | 0 | } else if (ival->reg != IR_REG_NONE) { |
379 | 0 | fprintf(f, " "); |
380 | 0 | } |
381 | 0 | do { |
382 | 0 | if (ival->reg != IR_REG_NONE) { |
383 | 0 | fprintf(f, "[%%%s]", ir_reg_name(ival->reg, ival->type)); |
384 | 0 | } |
385 | 0 | p = &ival->range; |
386 | 0 | fprintf(f, ": [%d.%d-%d.%d)", |
387 | 0 | IR_LIVE_POS_TO_REF(p->start), IR_LIVE_POS_TO_SUB_REF(p->start), |
388 | 0 | IR_LIVE_POS_TO_REF(p->end), IR_LIVE_POS_TO_SUB_REF(p->end)); |
389 | 0 | if (i == 0) { |
390 | | /* This is a TMP register */ |
391 | 0 | if (ival->tmp_ref == IR_LIVE_POS_TO_REF(p->start)) { |
392 | 0 | fprintf(f, "/%d", ival->tmp_op_num); |
393 | 0 | } else { |
394 | 0 | fprintf(f, "/%d.%d", ival->tmp_ref, ival->tmp_op_num); |
395 | 0 | } |
396 | 0 | } else { |
397 | 0 | p = p->next; |
398 | 0 | while (p) { |
399 | 0 | fprintf(f, ", [%d.%d-%d.%d)", |
400 | 0 | IR_LIVE_POS_TO_REF(p->start), IR_LIVE_POS_TO_SUB_REF(p->start), |
401 | 0 | IR_LIVE_POS_TO_REF(p->end), IR_LIVE_POS_TO_SUB_REF(p->end)); |
402 | 0 | p = p->next; |
403 | 0 | } |
404 | 0 | } |
405 | 0 | use_pos = ival->use_pos; |
406 | 0 | while (use_pos) { |
407 | 0 | if (use_pos->flags & IR_PHI_USE) { |
408 | 0 | IR_ASSERT(use_pos->op_num > 0); |
409 | 0 | fprintf(f, ", PHI_USE(%d.%d, phi=d_%d/%d)", |
410 | 0 | IR_LIVE_POS_TO_REF(use_pos->pos), IR_LIVE_POS_TO_SUB_REF(use_pos->pos), |
411 | 0 | -use_pos->hint_ref, use_pos->op_num); |
412 | 0 | } else if (use_pos->flags & IR_FUSED_USE) { |
413 | 0 | fprintf(f, ", USE(%d.%d/%d.%d", |
414 | 0 | IR_LIVE_POS_TO_REF(use_pos->pos), IR_LIVE_POS_TO_SUB_REF(use_pos->pos), |
415 | 0 | -use_pos->hint_ref, use_pos->op_num); |
416 | 0 | if (use_pos->hint >= 0) { |
417 | 0 | fprintf(f, ", hint=%%%s", ir_reg_name(use_pos->hint, ival->type)); |
418 | 0 | } |
419 | 0 | fprintf(f, ")"); |
420 | 0 | if (use_pos->flags & IR_USE_MUST_BE_IN_REG) { |
421 | 0 | fprintf(f, "!"); |
422 | 0 | } |
423 | 0 | } else { |
424 | 0 | if (!use_pos->op_num) { |
425 | 0 | fprintf(f, ", DEF(%d.%d", |
426 | 0 | IR_LIVE_POS_TO_REF(use_pos->pos), IR_LIVE_POS_TO_SUB_REF(use_pos->pos)); |
427 | 0 | } else { |
428 | 0 | fprintf(f, ", USE(%d.%d/%d", |
429 | 0 | IR_LIVE_POS_TO_REF(use_pos->pos), IR_LIVE_POS_TO_SUB_REF(use_pos->pos), |
430 | 0 | use_pos->op_num); |
431 | 0 | } |
432 | 0 | if (use_pos->hint >= 0) { |
433 | 0 | fprintf(f, ", hint=%%%s", ir_reg_name(use_pos->hint, ival->type)); |
434 | 0 | } |
435 | 0 | if (use_pos->hint_ref) { |
436 | 0 | fprintf(f, ", hint=R%d", ctx->vregs[use_pos->hint_ref]); |
437 | 0 | } |
438 | 0 | fprintf(f, ")"); |
439 | 0 | if (use_pos->flags & IR_USE_MUST_BE_IN_REG) { |
440 | 0 | fprintf(f, "!"); |
441 | 0 | } |
442 | 0 | } |
443 | 0 | use_pos = use_pos->next; |
444 | 0 | } |
445 | 0 | if (ival->next) { |
446 | 0 | fprintf(f, "\n\t"); |
447 | 0 | } |
448 | 0 | ival = ival->next; |
449 | 0 | } while (ival); |
450 | 0 | fprintf(f, "\n"); |
451 | 0 | } |
452 | 0 | } |
453 | 0 | #if 1 |
454 | 0 | n = ctx->vregs_count + ir_regs_number() + 2; |
455 | 0 | for (i = ctx->vregs_count + 1; i <= n; i++) { |
456 | 0 | ir_live_interval *ival = ctx->live_intervals[i]; |
457 | |
|
458 | 0 | if (ival) { |
459 | 0 | ir_live_range *p = &ival->range; |
460 | 0 | fprintf(f, "[%%%s] : [%d.%d-%d.%d)", |
461 | 0 | ir_reg_name(ival->reg, ival->type), |
462 | 0 | IR_LIVE_POS_TO_REF(p->start), IR_LIVE_POS_TO_SUB_REF(p->start), |
463 | 0 | IR_LIVE_POS_TO_REF(p->end), IR_LIVE_POS_TO_SUB_REF(p->end)); |
464 | 0 | p = p->next; |
465 | 0 | while (p) { |
466 | 0 | fprintf(f, ", [%d.%d-%d.%d)", |
467 | 0 | IR_LIVE_POS_TO_REF(p->start), IR_LIVE_POS_TO_SUB_REF(p->start), |
468 | 0 | IR_LIVE_POS_TO_REF(p->end), IR_LIVE_POS_TO_SUB_REF(p->end)); |
469 | 0 | p = p->next; |
470 | 0 | } |
471 | 0 | fprintf(f, "\n"); |
472 | 0 | } |
473 | 0 | } |
474 | 0 | #endif |
475 | 0 | fprintf(f, "}\n"); |
476 | 0 | } |
477 | | |
478 | | void ir_dump_codegen(const ir_ctx *ctx, FILE *f) |
479 | 0 | { |
480 | 0 | ir_ref i, j, n, ref, *p; |
481 | 0 | ir_insn *insn; |
482 | 0 | uint32_t flags, _b, b; |
483 | 0 | ir_block *bb; |
484 | 0 | bool first; |
485 | |
|
486 | 0 | fprintf(f, "{\n"); |
487 | 0 | for (i = IR_UNUSED + 1, insn = ctx->ir_base - i; i < ctx->consts_count; i++, insn--) { |
488 | 0 | fprintf(f, "\t%s c_%d = ", ir_type_cname[insn->type], i); |
489 | 0 | if (insn->op == IR_FUNC) { |
490 | 0 | fprintf(f, "func %s", ir_get_str(ctx, insn->val.name)); |
491 | 0 | ir_print_proto(ctx, insn->proto, f); |
492 | 0 | } else if (insn->op == IR_SYM) { |
493 | 0 | fprintf(f, "sym(%s)", ir_get_str(ctx, insn->val.name)); |
494 | 0 | } else if (insn->op == IR_FUNC_ADDR) { |
495 | 0 | fprintf(f, "func *"); |
496 | 0 | ir_print_const(ctx, insn, f, true); |
497 | 0 | ir_print_proto(ctx, insn->proto, f); |
498 | 0 | } else { |
499 | 0 | ir_print_const(ctx, insn, f, true); |
500 | 0 | } |
501 | 0 | fprintf(f, ";\n"); |
502 | 0 | } |
503 | |
|
504 | 0 | for (_b = 1; _b <= ctx->cfg_blocks_count; _b++) { |
505 | 0 | if (ctx->cfg_schedule) { |
506 | 0 | b = ctx->cfg_schedule[_b]; |
507 | 0 | } else { |
508 | 0 | b = _b; |
509 | 0 | } |
510 | 0 | bb = &ctx->cfg_blocks[b]; |
511 | 0 | if ((bb->flags & (IR_BB_START|IR_BB_ENTRY|IR_BB_EMPTY)) == IR_BB_EMPTY) { |
512 | 0 | continue; |
513 | 0 | } |
514 | | |
515 | 0 | fprintf(f, "#BB%d: end=l_%d", b, bb->end); |
516 | 0 | if (bb->flags & IR_BB_UNREACHABLE) { |
517 | 0 | fprintf(f, ", U"); |
518 | 0 | } |
519 | 0 | if (bb->dom_parent > 0) { |
520 | 0 | fprintf(f, ", idom=BB%d(%d)", bb->dom_parent, bb->dom_depth); |
521 | 0 | } |
522 | 0 | if (bb->loop_depth != 0) { |
523 | 0 | if (bb->flags & IR_BB_LOOP_HEADER) { |
524 | 0 | if (bb->loop_header > 0) { |
525 | 0 | fprintf(f, ", loop=HDR,BB%d(%d)", bb->loop_header, bb->loop_depth); |
526 | 0 | } else { |
527 | 0 | IR_ASSERT(bb->loop_depth == 1); |
528 | 0 | fprintf(f, ", loop=HDR(%d)", bb->loop_depth); |
529 | 0 | } |
530 | 0 | } else { |
531 | 0 | IR_ASSERT(bb->loop_header > 0); |
532 | 0 | fprintf(f, ", loop=BB%d(%d)", bb->loop_header, bb->loop_depth); |
533 | 0 | } |
534 | 0 | } |
535 | 0 | if (bb->predecessors_count) { |
536 | 0 | uint32_t i; |
537 | |
|
538 | 0 | fprintf(f, ", pred(%d)=[BB%d", bb->predecessors_count, ctx->cfg_edges[bb->predecessors]); |
539 | 0 | for (i = 1; i < bb->predecessors_count; i++) { |
540 | 0 | fprintf(f, ", BB%d", ctx->cfg_edges[bb->predecessors + i]); |
541 | 0 | } |
542 | 0 | fprintf(f, "]"); |
543 | 0 | } |
544 | 0 | if (bb->successors_count) { |
545 | 0 | uint32_t i; |
546 | |
|
547 | 0 | fprintf(f, ", succ(%d)=[BB%d", bb->successors_count, ctx->cfg_edges[bb->successors]); |
548 | 0 | for (i = 1; i < bb->successors_count; i++) { |
549 | 0 | fprintf(f, ", BB%d", ctx->cfg_edges[bb->successors + i]); |
550 | 0 | } |
551 | 0 | fprintf(f, "]"); |
552 | 0 | } |
553 | 0 | fprintf(f, "\n"); |
554 | |
|
555 | 0 | for (i = bb->start, insn = ctx->ir_base + i; i <= bb->end;) { |
556 | 0 | flags = ir_op_flags[insn->op]; |
557 | 0 | if (flags & IR_OP_FLAG_CONTROL) { |
558 | 0 | if (!(flags & IR_OP_FLAG_MEM) || insn->type == IR_VOID) { |
559 | 0 | fprintf(f, "\tl_%d = ", i); |
560 | 0 | } else { |
561 | 0 | fprintf(f, "\t%s d_%d", ir_type_cname[insn->type], i); |
562 | 0 | if (ctx->vregs && ctx->vregs[i]) { |
563 | 0 | fprintf(f, " {R%d}", ctx->vregs[i]); |
564 | 0 | } |
565 | 0 | if (ctx->regs) { |
566 | 0 | int8_t reg = ctx->regs[i][0]; |
567 | 0 | if (reg != IR_REG_NONE) { |
568 | 0 | fprintf(f, " {%%%s%s}", ir_reg_name(IR_REG_NUM(reg), insn->type), |
569 | 0 | (reg & (IR_REG_SPILL_STORE|IR_REG_SPILL_SPECIAL)) ? ":store" : ""); |
570 | 0 | } |
571 | 0 | } |
572 | 0 | fprintf(f, ", l_%d = ", i); |
573 | 0 | } |
574 | 0 | } else { |
575 | 0 | fprintf(f, "\t"); |
576 | 0 | if (flags & IR_OP_FLAG_DATA) { |
577 | 0 | fprintf(f, "%s d_%d", ir_type_cname[insn->type], i); |
578 | 0 | if (ctx->vregs && ctx->vregs[i]) { |
579 | 0 | fprintf(f, " {R%d}", ctx->vregs[i]); |
580 | 0 | } |
581 | 0 | if (ctx->regs) { |
582 | 0 | int8_t reg = ctx->regs[i][0]; |
583 | 0 | if (reg != IR_REG_NONE) { |
584 | 0 | fprintf(f, " {%%%s%s}", ir_reg_name(IR_REG_NUM(reg), insn->type), |
585 | 0 | (reg & (IR_REG_SPILL_STORE|IR_REG_SPILL_SPECIAL)) ? ":store" : ""); |
586 | 0 | } |
587 | 0 | } |
588 | 0 | fprintf(f, " = "); |
589 | 0 | } |
590 | 0 | } |
591 | 0 | fprintf(f, "%s", ir_op_name[insn->op]); |
592 | 0 | n = ir_operands_count(ctx, insn); |
593 | 0 | if ((insn->op == IR_MERGE || insn->op == IR_LOOP_BEGIN) && n != 2) { |
594 | 0 | fprintf(f, "/%d", n); |
595 | 0 | } else if ((insn->op == IR_CALL || insn->op == IR_TAILCALL) && n != 2) { |
596 | 0 | fprintf(f, "/%d", n - 2); |
597 | 0 | } else if (insn->op == IR_PHI && n != 3) { |
598 | 0 | fprintf(f, "/%d", n - 1); |
599 | 0 | } else if (insn->op == IR_SNAPSHOT) { |
600 | 0 | fprintf(f, "/%d", n - 1); |
601 | 0 | } |
602 | 0 | first = 1; |
603 | 0 | for (j = 1, p = insn->ops + 1; j <= n; j++, p++) { |
604 | 0 | uint32_t opnd_kind = IR_OPND_KIND(flags, j); |
605 | |
|
606 | 0 | ref = *p; |
607 | 0 | if (ref) { |
608 | 0 | switch (opnd_kind) { |
609 | 0 | case IR_OPND_DATA: |
610 | 0 | if (IR_IS_CONST_REF(ref)) { |
611 | 0 | fprintf(f, "%sc_%d", first ? "(" : ", ", -ref); |
612 | 0 | } else { |
613 | 0 | fprintf(f, "%sd_%d", first ? "(" : ", ", ref); |
614 | 0 | } |
615 | 0 | if (ctx->vregs && ref > 0 && ctx->vregs[ref]) { |
616 | 0 | fprintf(f, " {R%d}", ctx->vregs[ref]); |
617 | 0 | } |
618 | 0 | if (ctx->regs) { |
619 | 0 | int8_t *regs = ctx->regs[i]; |
620 | 0 | int8_t reg = regs[j]; |
621 | 0 | if (reg != IR_REG_NONE) { |
622 | 0 | fprintf(f, " {%%%s%s}", ir_reg_name(IR_REG_NUM(reg), ctx->ir_base[ref].type), |
623 | 0 | (reg & (IR_REG_SPILL_LOAD|IR_REG_SPILL_SPECIAL)) ? ":load" : ""); |
624 | 0 | } |
625 | 0 | } |
626 | 0 | first = 0; |
627 | 0 | break; |
628 | 0 | case IR_OPND_CONTROL: |
629 | 0 | case IR_OPND_CONTROL_DEP: |
630 | 0 | case IR_OPND_CONTROL_REF: |
631 | 0 | fprintf(f, "%sl_%d", first ? "(" : ", ", ref); |
632 | 0 | first = 0; |
633 | 0 | break; |
634 | 0 | case IR_OPND_STR: |
635 | 0 | fprintf(f, "%s\"%s\"", first ? "(" : ", ", ir_get_str(ctx, ref)); |
636 | 0 | first = 0; |
637 | 0 | break; |
638 | 0 | case IR_OPND_PROTO: |
639 | 0 | fprintf(f, "%sfunc ", first ? "(" : ", "); |
640 | 0 | ir_print_proto(ctx, ref, f); |
641 | 0 | break; |
642 | 0 | case IR_OPND_PROB: |
643 | 0 | if (ref == 0) { |
644 | 0 | break; |
645 | 0 | } |
646 | 0 | IR_FALLTHROUGH; |
647 | 0 | case IR_OPND_NUM: |
648 | 0 | fprintf(f, "%s%d", first ? "(" : ", ", ref); |
649 | 0 | first = 0; |
650 | 0 | break; |
651 | 0 | } |
652 | 0 | } else if (opnd_kind == IR_OPND_NUM) { |
653 | 0 | fprintf(f, "%s%d", first ? "(" : ", ", ref); |
654 | 0 | first = 0; |
655 | 0 | } else if (j != n && |
656 | 0 | (IR_IS_REF_OPND_KIND(opnd_kind) || (opnd_kind == IR_OPND_UNUSED && p[n-j]))) { |
657 | 0 | fprintf(f, "%snull", first ? "(" : ", "); |
658 | 0 | first = 0; |
659 | 0 | } |
660 | 0 | } |
661 | 0 | if (first) { |
662 | 0 | fprintf(f, ";"); |
663 | 0 | } else if (ctx->value_params |
664 | 0 | && insn->op == IR_PARAM |
665 | 0 | && ctx->value_params[insn->op3 - 1].align) { |
666 | 0 | fprintf(f, ") ByVal(%d, %d);", |
667 | 0 | ctx->value_params[insn->op3 - 1].size, |
668 | 0 | ctx->value_params[insn->op3 - 1].align); |
669 | 0 | } else { |
670 | 0 | fprintf(f, ");"); |
671 | 0 | } |
672 | 0 | if (((flags & IR_OP_FLAG_DATA) || ((flags & IR_OP_FLAG_MEM) && insn->type != IR_VOID)) && ctx->binding) { |
673 | 0 | ir_ref var = ir_binding_find(ctx, i); |
674 | 0 | if (var) { |
675 | 0 | IR_ASSERT(var < 0); |
676 | 0 | fprintf(f, " # BIND(0x%x);", -var); |
677 | 0 | } |
678 | 0 | } |
679 | 0 | if (ctx->rules) { |
680 | 0 | uint32_t rule = ctx->rules[i]; |
681 | 0 | uint32_t id = rule & IR_RULE_MASK; |
682 | |
|
683 | 0 | if (id < IR_LAST_OP) { |
684 | 0 | fprintf(f, " # RULE(%s", ir_op_name[id]); |
685 | 0 | } else { |
686 | 0 | IR_ASSERT(id > IR_LAST_OP /*&& id < IR_LAST_RULE*/); |
687 | 0 | fprintf(f, " # RULE(%s", ir_rule_name[id - IR_LAST_OP]); |
688 | 0 | } |
689 | 0 | if (rule & IR_FUSED) { |
690 | 0 | fprintf(f, ":FUSED"); |
691 | 0 | } |
692 | 0 | if (rule & IR_SKIPPED) { |
693 | 0 | fprintf(f, ":SKIPPED"); |
694 | 0 | } |
695 | 0 | if (rule & IR_SIMPLE) { |
696 | 0 | fprintf(f, ":SIMPLE"); |
697 | 0 | } |
698 | 0 | fprintf(f, ")"); |
699 | 0 | } |
700 | 0 | fprintf(f, "\n"); |
701 | 0 | n = ir_insn_inputs_to_len(n); |
702 | 0 | i += n; |
703 | 0 | insn += n; |
704 | 0 | } |
705 | | |
706 | 0 | if (bb->flags & IR_BB_DESSA_MOVES) { |
707 | 0 | ir_dump_dessa_moves(ctx, b, bb, f); |
708 | 0 | } |
709 | |
|
710 | 0 | insn = &ctx->ir_base[bb->end]; |
711 | 0 | if (insn->op == IR_END || insn->op == IR_LOOP_END) { |
712 | 0 | uint32_t succ; |
713 | |
|
714 | 0 | if (bb->successors_count == 1) { |
715 | 0 | succ = ctx->cfg_edges[bb->successors]; |
716 | 0 | } else { |
717 | | /* END may have a fake control edge to ENTRY */ |
718 | 0 | IR_ASSERT(bb->successors_count == 2); |
719 | 0 | succ = ctx->cfg_edges[bb->successors]; |
720 | 0 | if (ctx->ir_base[ctx->cfg_blocks[succ].start].op == IR_ENTRY) { |
721 | 0 | succ = ctx->cfg_edges[bb->successors + 1]; |
722 | 0 | #ifdef IR_DEBUG |
723 | 0 | } else { |
724 | 0 | uint32_t fake_succ = ctx->cfg_edges[bb->successors + 1]; |
725 | 0 | IR_ASSERT(ctx->ir_base[ctx->cfg_blocks[fake_succ].start].op == IR_ENTRY); |
726 | 0 | #endif |
727 | 0 | } |
728 | 0 | } |
729 | 0 | succ = ir_skip_empty_target_blocks(ctx, succ); |
730 | 0 | if (ctx->cfg_schedule) { |
731 | 0 | if (_b == ctx->cfg_blocks_count || succ != ctx->cfg_schedule[_b + 1]) { |
732 | 0 | fprintf(f, "\t# GOTO BB%d\n", succ); |
733 | 0 | } |
734 | 0 | } else { |
735 | 0 | if (succ != b + 1) { |
736 | 0 | fprintf(f, "\t# GOTO BB%d\n", succ); |
737 | 0 | } |
738 | 0 | } |
739 | 0 | } else if (insn->op == IR_IF) { |
740 | 0 | uint32_t true_block, false_block; |
741 | |
|
742 | 0 | ir_get_true_false_blocks(ctx, b, &true_block, &false_block); |
743 | 0 | fprintf(f, "\t# IF_TRUE BB%d, IF_FALSE BB%d\n", true_block, false_block); |
744 | 0 | } else if (insn->op == IR_SWITCH) { |
745 | 0 | fprintf(f, "\t# SWITCH ...\n"); |
746 | 0 | } |
747 | 0 | } |
748 | 0 | fprintf(f, "}\n"); |
749 | 0 | } |