From a4e5812454e3453e51998ab8928e7319b87b1249 Mon Sep 17 00:00:00 2001 From: SensanaMMZ <305674455+SensanaMMZ@users.noreply.github.com> Date: Mon, 20 Jul 2026 11:21:01 -0400 Subject: [PATCH] Add opt-in register-allocation tracing (AGBCC_TRACE_ALLOC) Emits one line per hard-register assignment in local and global allocation, with the priority inputs (size, calls_crossed, live_length, n_refs). Gated on the AGBCC_TRACE_ALLOC environment variable, so it is inert and does not affect codegen unless explicitly enabled -- useful for understanding why the allocator placed a value in a particular register when matching decompilations. --- gcc/global.c | 12 ++++++++++++ gcc/local-alloc.c | 10 +++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/gcc/global.c b/gcc/global.c index d6a05263..46608b9b 100755 --- a/gcc/global.c +++ b/gcc/global.c @@ -1187,12 +1187,24 @@ find_reg (allocno, losers, alt_regs_p, accept_call_clobbered, retrying) /* Did we find a register? */ + if (best_reg < 0 && getenv ("AGBCC_TRACE_ALLOC")) + fprintf (stderr, "[galloc] pseudo %d -> SPILL (allocno %d, calls_crossed %d)\n", + allocno_reg[allocno], allocno, (int) allocno_calls_crossed[allocno]); + if (best_reg >= 0) { register int lim, j; HARD_REG_SET this_reg; /* Yes. Record it as the hard register of this pseudo-reg. */ + if (getenv ("AGBCC_TRACE_ALLOC")) + fprintf (stderr, + "[galloc] pseudo %d -> r%d (allocno %d, size %d, calls_crossed %d, live_length %d, n_refs %d)\n", + allocno_reg[allocno], best_reg, allocno, + (int) allocno_size[allocno], + (int) allocno_calls_crossed[allocno], + (int) allocno_live_length[allocno], + (int) allocno_n_refs[allocno]); reg_renumber[allocno_reg[allocno]] = best_reg; /* Also of any pseudo-regs that share with it. */ if (reg_may_share[allocno_reg[allocno]]) diff --git a/gcc/local-alloc.c b/gcc/local-alloc.c index d4b45f01..6bfe23b7 100755 --- a/gcc/local-alloc.c +++ b/gcc/local-alloc.c @@ -1412,7 +1412,15 @@ block_alloc (b) if (qty_phys_reg[q] >= 0) { for (i = qty_first_reg[q]; i >= 0; i = reg_next_in_qty[i]) - reg_renumber[i] = qty_phys_reg[q] + reg_offset[i]; + { + if (getenv ("AGBCC_TRACE_ALLOC")) + fprintf (stderr, + "[lalloc] pseudo %d -> r%d (qty %d, births %d, deaths %d, n_calls_crossed %d)\n", + i, qty_phys_reg[q] + reg_offset[i], q, + (int) qty_birth[q], (int) qty_death[q], + (int) qty_n_calls_crossed[q]); + reg_renumber[i] = qty_phys_reg[q] + reg_offset[i]; + } } }