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