Skip to content

Register allocation ties in GCC 3.2

Two locals with overlapping register preferences can produce the same instructions with different register numbers. variable. build/gccsrc/src/gcc-3.2.3/gcc/ holds the compiler sources, and the two decisions that produce those differences are narrow enough to write down. Read them before spending compiles on a shape that cannot move the tie.

1. Two variables share one register when the second is a copy of the first

local-alloc.c combine_regs ties two pseudo registers into one quantity when all of these hold:

  • the destination has no quantity yet (reg_qty[sreg] >= -1 fails);
  • the source dies in this instruction (find_regno_note (insn, REG_DEAD, ureg) or already_dead);
  • the two machine modes are tieable and the register classes are compatible.

That is the ordinary T x = y; where y is used for the last time. So when the original kept two variables in separate registers and this build shares one, the original did not reach the second value through a copy from a dying variable. Ways to stop the tie, in the order that has worked:

  • give the second variable its own initialising expression instead of a copy (Type x = Compute(); rather than Type tmp = Compute(); Type x = tmp;);
  • keep the first variable alive past the copy by using it again afterwards;
  • take the value from a call result or a member read rather than a local.

The reverse also matters: when the original shares a register and this build does not, the original did write the plain copy, and a named temporary that survives is what broke it.

2. Which variable gets the register first

global.c allocno_compare sorts allocnos by

priority = floor_log2 (n_refs) * freq / live_length * size

descending, and breaks exact ties by allocno number, which follows pseudo register number, which follows declaration order. So:

  • a variable referenced more often wins a register before one referenced less;
  • a shorter live range wins;
  • when everything else is equal, the earlier declared variable wins.

This is why declaration order keeps deciding matches, and why a variable that is only read once in a long function is the one that ends up spilled. Moving a declaration earlier, adding a legitimate extra use, or shortening its live range are the only levers; no compiler flag reaches it.

3. Consequences for the observable shapes

  • The original sometimes saves more callee-saved registers than this build for the same source, because it had more distinct allocnos (fewer local-alloc ties). A frame that is 0x10 or 0x20 larger in the original with an extra saved register is this, not a missing local.
  • An FP value reloaded once per use in the original means its allocno lost the sort, so it was spilled; the same source here keeping it in $f2x means the pseudo numbering differed.
  • -fno-gcse, -fno-schedule-insns*, -fno-strength-reduce, -fno-expensive-optimizations and the older compiler do not move any of this, which is why those flags score worse or unchanged on these drafts.

What this does not explain

A tie is not always the whole difference. If the frame size differs by more than the saved registers account for, look for a missing temporary first (an unshared const TIMETYPE result, a class-sized copy, a debug buffer), because those change the stack layout and every offset after them.

4. The pre-reload scheduler sorts by INSN_REG_WEIGHT

sched1 (haifa-sched.c rank_for_schedule) dequeues ready instructions by, in order, INSN_PRIORITY, then INSN_REG_WEIGHT, then INSN_LUID (age). INSN_REG_WEIGHT counts a pseudo register as dead weight in the instruction that kills it, so an instruction whose only source is a dying parameter sorts before an instruction with no register pressure at all.

Correction, 14 November 2026: the ZMessageResolver example below was not a scheduler tie. The debug record types m_MessageName as const char *const; making the field const and setting it in the constructor's initialiser list gives the original store order, and the constructor is exact. Check field types against the record before reaching for this section.

5. The result declaration funnels every return through one pseudo

GCC 3.2 builds return expr as an INIT_EXPR into the function's RESULT_DECL, which is a single pseudo for the whole function. When that pseudo's live range spans a call, it cannot take $v0 (call-clobbered), so it lands in a callee-saved register and every return site ends with a copy into $v0 in the epilogue. When the range does not span a call, GCC allocates it $v0 directly and each return site writes the value where the reference writes it.

Two functions in item.si.cpp show the difference plainly: Item__Getforceunpickablebyhm (ob_0026ab50) and Item__Iscontainercontainsitem (ob_0026a65c) both have the reference storing each byte straight to $v0 at four return sites, and both reconstructions funnel through $13 plus an epilogue copy. ob_0026ab50's blocker is precise: the null-test zero is materialised in the beqz delay slot, which extends the result pseudo's range across the GetObjectId call.

How to tell this class apart from an allocno tie: the reference and the candidate disagree about $v0 itself, and the candidate has one extra copy in the epilogue that the reference does not have. An allocno tie swaps two non-$v0 registers and leaves the epilogue identical.

Both were matched on 12 November 2026, and the cause is the number of return statements and how they nest. One or two returns leave the result in $v0; three or more separate ones funnel through a temporary. Item__Getforceunpickablebyhm is if (pItem) { if (A) return x; if (B) return y; } return 0; and Item__Iscontainercontainsitem tests both types in one statement, if (!IsGeomType<A>(a) || !IsGeomType<B>(b)) return 0;. The line table gives the structure: a first branch that targets a block after the epilogue is an enclosing if, and a return line number that appears once for several paths is one statement. Item__Containweapons carried a per-function -fno-cprop-registers for the same reason and is exact without it as if (!pItem) return 0; if (IsGeomType<C>(pItem)) { ...; if (!pList) return 0; for (...) ... } return 0;.