Coverage Report

Created: 2026-01-18 06:49

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