Coverage Report

Created: 2026-02-14 06:52

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