Skip to content

Register Allocation 06.2 regalloc basic

Mugundan edited this page Nov 27, 2026 · 1 revision

Basic Register Allocator

Relevant source files

The following files were used as context for generating this wiki page:

The Basic allocator is the smallest complete user of the [allocation framework](#framework). It allocates whole live intervals in order of spill weight, heaviest first. When a register is not free, it spills either the cheaper interfering intervals or the current one. It never splits. It is not used by default anywhere, but it is the best code to read before Greedy.

Purpose and Scope

This page walks through the whole of RABasic::selectOrSplit, about fifty lines, and explains what Greedy adds on top.

Queue Order

Basic's queue is a std::priority_queue ordered by spill weight, with the register number as a tie-break:

struct CompSpillWeight {
  bool operator()(const LiveInterval *A, const LiveInterval *B) const {
    return std::tuple(A->weight(), A->reg()) <
           std::tuple(B->weight(), B->reg());
  }
};

The most expensive-to-spill intervals are allocated first, so they get the first choice of registers. Spill weight is roughly "how often this register is used, scaled by how often each use's block runs (block frequency), divided by its length".

Sources: llvm/lib/CodeGen/RegAllocBasic.h:27-50

selectOrSplit, Step by Step

flowchart TD
  A["dequeue heaviest interval V"] --> B["for each P in AllocationOrder"]
  B -->|"IK_Free"| C["return P"]
  B -->|"IK_VirtReg"| D["remember P as spill candidate"]
  B -->|"IK_RegMask / IK_RegUnit"| B
  D --> E["for each candidate P:<br/>are all interferences spillable<br/>and lighter than V?"]
  E -->|"yes"| F["unassign and spill them;<br/>return P"]
  E -->|"no candidate works"| G{"V spillable?"}
  G -->|"yes"| H["spill V; return 0"]
  G -->|"no"| I["return ~0u (error)"]
Loading

1. Look for a free register

  auto Order =
      AllocationOrder::create(VirtReg.reg(), *VRM, RegClassInfo, Matrix);
  for (MCRegister PhysReg : Order) {
    switch (Matrix->checkInterference(VirtReg, PhysReg)) {
    case LiveRegMatrix::IK_Free:
      // PhysReg is available, allocate it.
      return PhysReg;

    case LiveRegMatrix::IK_VirtReg:
      // Only virtual registers in the way, we may be able to spill them.
      PhysRegSpillCands.push_back(PhysReg);
      continue;

    default:
      // RegMask or RegUnit interference.
      continue;
    }
  }

Hints come first in Order, so if the hinted register is free it wins. Registers blocked by calls (IK_RegMask) or fixed physical liveness (IK_RegUnit) are skipped for good. Registers blocked only by other virtual registers are remembered.

2. Try to spill something cheaper

For each remembered register, spillInterferences collects every virtual register occupying any of its units. It gives up on that register if any of them is not spillable, or is heavier than the current interval:

  for (MCRegUnit Unit : TRI->regunits(PhysReg)) {
    LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, Unit);
    for (const auto *Intf : reverse(Q.interferingVRegs())) {
      if (!Intf->isSpillable() || Intf->weight() > VirtReg.weight())
        return false;
      Intfs.push_back(Intf);
    }
  }

Otherwise it unassigns each one from the matrix and calls spiller().spill(LRE). The spiller creates small new intervals around each use and reload. They go into SplitVRegs and the base loop puts them back in the queue.

Because the queue pops the heaviest interval first, every interval assigned earlier was at least as heavy as the current one at the time it was dequeued. Step 2 therefore fires mostly for intervals that join the queue later, such as the short reload intervals the spiller creates. They are tiny, so their weight (uses divided by length) is high, and they can push aside a long, lightly used interval that was assigned earlier.

3. Spill the current interval

If nothing could be freed, the current interval is spilled itself and 0 is returned, meaning "nothing to assign this round". If it is not spillable (for example it is already a tiny reload interval), ~0u is returned and RegAllocBase reports that it ran out of registers.

Sources: llvm/lib/CodeGen/RegAllocBasic.cpp:121-218

What Basic Does Not Do

Missing feature Effect Greedy's answer
Splitting A long interval that conflicts in one small place is spilled everywhere Region, block and local splitting
Eviction for reassignment An interfering interval is spilled, never moved to another free register Evicted intervals are requeued and may find another register
Callee-saved cost No special treatment of the first use of a callee-saved register CSRCost, tryAssignCSRFirstTime
Hint repair Broken copy hints stay broken tryHintsRecoloring

On the 24-load spill test from the walkthroughs, Basic and Greedy happen to produce code of the same size (32 spill/reload/save lines, 83 instructions). The difference shows up in larger functions with loops, where splitting matters.

When to Use It

  • As a reference while reading Greedy.
  • To check whether a miscompile is caused by Greedy's heuristics: llc -regalloc=basic. If the bug disappears, the problem is likely in Greedy-specific logic (eviction, splitting).
  • As a simple template if you ever write your own allocator on top of RegAllocBase.

Start here

Computer Organization & Architecture

Hexagon ISA

Operating Systems

C++ for LLVM Development

LLVM IR

SelectionDAG

Backend Assignments

Target Bring-up

Calling Conventions

Frame Lowering

MC Layer

Register Allocation

Scheduling

Clone this wiki locally