Skip to content

Register Allocation 06.1 regalloc fast

Mugundan edited this page Nov 27, 2026 · 1 revision

Fast Register Allocator

Relevant source files

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

The Fast allocator is the default at `-O0`. It makes one pass over each basic block, from the last instruction to the first, assigning physical registers as it meets uses and definitions. It never computes live intervals. Any value that may cross a block boundary lives in a stack slot between blocks. The result is quick to produce and easy to debug, but slow to run.

Purpose and Scope

This page explains the Fast algorithm, what its output looks like, and why a new backend usually gets it working first. It is the simplest allocator to read and a good first step before Greedy.

Why It Exists

  • Compile time. -O0 builds want speed. Fast is roughly linear in the size of the function and needs no LiveIntervals, no coalescer and no scheduler: the pipeline is just PHI elimination, two-address lowering and Fast (see Pipeline).
  • Debuggability. Because values are stored to their stack slot between blocks, a debugger can usually find every variable in memory.
  • Bring-up. A new backend can run -O0 code as soon as it can copy, spill and reload a register. Liveness, coalescing and splitting hooks are not needed yet.

The Algorithm

flowchart TD
  A["runOnMachineFunction"] --> B["for each block: allocateBasicBlock"]
  B --> C["mark live-out physregs as pre-assigned"]
  C --> D["walk instructions in reverse<br/>allocateInstruction"]
  D --> E["defs: free the register;<br/>if the value may live out, store it to its slot after the def"]
  D --> F["uses: if no register yet, pick one now<br/>(this is the value's last use, seen first)"]
  D --> G["at the block top: reloadAtBegin<br/>load every value still live-in from its slot"]
  G --> H["erase coalesced copies"]
Loading

Walking backwards is the key idea. Coming from the bottom, the first time the allocator sees a virtual register is at its last use, so it knows exactly when the register becomes free: at its definition, further up. No liveness analysis is needed within the block.

  // Traverse block in reverse order allocating instructions one by one.
  for (MachineInstr &MI : reverse(MBB)) {
    ...
    allocateInstruction(MI);
    ...
  }
  ...
  // Spill all physical registers holding virtual registers now.
  LLVM_DEBUG(dbgs() << "Loading live registers at begin of block.\n");
  reloadAtBegin(MBB);

For each instruction, allocateInstruction:

  1. handles definitions first: the register that holds the value becomes free above this point (defineVirtReg, definePhysReg);
  2. handles uses: a virtual register that has no register yet gets one now (useVirtReg, allocVirtReg), choosing a free register from the class order, or displacing another value and reloading it later;
  3. respects regmasks on calls: every clobbered register must be free across the call, so values live across a call are moved out of caller-saved registers.

Sources: llvm/lib/CodeGen/RegAllocFast.cpp:1794-1853, llvm/lib/CodeGen/RegAllocFast.cpp:1467

Values That Cross Blocks

Fast keeps no state between blocks. Every value that might be read in another block is stored to a stack slot right after its definition and reloaded at the top of each block that uses it.

The decision is made by mayLiveOut. It is conservative and cheap: a register is treated as crossing blocks if any of its first 8 uses is in another block, or if the block loops back to itself and the use is not dominated by the definition:

  // See if the first \p Limit uses of the register are all in the current
  // block.
  static const unsigned Limit = 8;
  unsigned C = 0;
  for (const MachineInstr &UseInst : MRI->use_nodbg_instructions(VirtReg)) {
    if (UseInst.getParent() != MBB || ++C >= Limit) {
      MayLiveAcrossBlocks.set(VirtReg.virtRegIndex());
      ...

defineVirtReg then places the store just after the definition:

  if (LRI->Reloaded || LRI->LiveOut) {
    if (!MI.isImplicitDef()) {
      MachineBasicBlock::iterator SpillBefore =
          std::next((MachineBasicBlock::iterator)MI.getIterator());
      ...
      spill(SpillBefore, VirtReg, PhysReg, Kill, LRI->LiveOut);

and reloadAtBegin loads, at the start of the block, every virtual register still recorded as live when the backwards walk reaches the top.

This is why -O0 code is full of str/ldr to the stack around branches. It is the expected behaviour, not a bug.

Sources: llvm/lib/CodeGen/RegAllocFast.cpp:530-598, llvm/lib/CodeGen/RegAllocFast.cpp:703-741, llvm/lib/CodeGen/RegAllocFast.cpp:1093-1158

Copies and Hints

There is no coalescer at -O0, but Fast does cheap coalescing on its own. When choosing a register for a value defined by a full COPY, it follows the copy chain (traceCopies, traceCopyChain) and prefers the register on the other side. If both sides end up in the same register, the copy is added to Coalesced and erased at the end of the block.

Example

The first example at -O0 (llc -mtriple=aarch64 -O0 -stop-before=regallocfast). There is no combiner at this level, so the multiply and add stay separate:

bb.1 (%ir-block.0):
  liveins: $w0, $w1, $w2
  %0:gpr32 = COPY $w0
  %1:gpr32 = COPY $w1
  %2:gpr32 = COPY $w2
  %3:gpr32 = MADDWrrr %0, %1, $wzr
  %4:gpr32 = ADDWrr %3, %2
  $w0 = COPY %4
  RET_ReallyLR implicit $w0

After -stop-after=regallocfast:

bb.1 (%ir-block.0):
  liveins: $w0, $w1, $w2
  renamable $w8 = MADDWrrr killed renamable $w0, killed renamable $w1, $wzr
  renamable $w0 = ADDWrr killed renamable $w8, killed renamable $w2
  RET_ReallyLR implicit killed $w0

Reading it bottom-up, as the allocator did:

  • $w0 = COPY %4: %4 should be $w0, so %4 is given $w0 and the copy is coalesced away.
  • %4 = ADDWrr %3, %2: %3 needs a register. It is defined by MADDWrrr, not by a copy, so it has no hint, and Fast takes the first free register in the allocation order, which on AArch64 starts at w8 (see Allocation order). %2 traces back through its copy to $w2.
  • %0, %1 trace back to $w0 and $w1, so all three argument copies vanish.

The function is a single block, so there are no slot stores. Add a branch and you will see them.

Using Fast on a New Backend

Fast needs only:

Hook Why
Register classes and getReservedRegs To know what it may use
getCalleeSavedRegs, getCallPreservedMask Regmasks on calls
TargetInstrInfo::copyPhysReg Turning COPY into a real move
TargetInstrInfo::storeRegToStackSlot, loadRegFromStackSlot Spills and reloads
TargetRegisterInfo::eliminateFrameIndex Turning the slot frame index into an address, later in prologue/epilogue insertion

So the usual bring-up order is: get -O0 working with Fast first, then turn on -O2 and Greedy. See the checklist.

Sources: llvm/lib/CodeGen/RegAllocFast.cpp:891-932, llvm/include/llvm/CodeGen/TargetInstrInfo.h:1087, llvm/include/llvm/CodeGen/TargetInstrInfo.h:1199-1219

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