Skip to content

Register Allocation 01.1 pipeline

Mugundan edited this page Nov 27, 2026 · 1 revision

The Register Allocation Pipeline

Relevant source files

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

Register allocation is not one pass. It is a chain of passes that first reshape the code into a form the allocator can handle, then assign registers, then clean up. `TargetPassConfig` builds one of two chains: a short one for `-O0` and a long one when optimising.

Purpose and Scope

This page lists the exact passes in both chains, explains what each one is for in one or two sentences, and shows how -regalloc= selects the chain. Each preparation pass has its own page later.

Which Chain Runs

TargetPassConfig::getOptimizeRegAlloc decides:

  • If -regalloc= was given on the command line, the optimised chain runs for every allocator except fast.
  • Otherwise the optimised chain runs whenever the optimisation level is not -O0.
bool TargetPassConfig::getOptimizeRegAlloc() const {
  // An explicit -regalloc choice implies its pipeline: only the fast
  // allocator uses the unoptimized one.
  ...
  RegisterRegAlloc::FunctionPassCtor Ctor = RegisterRegAlloc::getDefault();
  if (Ctor != (RegisterRegAlloc::FunctionPassCtor)&useDefaultRegisterAllocator)
    return Ctor !=
           (RegisterRegAlloc::FunctionPassCtor)&createFastRegisterAllocator;
  return getOptLevel() != CodeGenOptLevel::None;
}

So llc -O2 -regalloc=fast uses the unoptimised chain (no coalescer, no scheduler), and llc -O0 -regalloc=greedy uses the optimised one. The unoptimised chain only accepts the Fast allocator; addRegAssignAndRewriteFast reports a fatal usage error for anything else.

Sources: llvm/lib/CodeGen/TargetPassConfig.cpp:1393-1403, llvm/lib/CodeGen/TargetPassConfig.cpp:1445-1457

The Unoptimised Chain (-O0)

void TargetPassConfig::addFastRegAlloc() {
  addPass(&PHIEliminationID);
  addPass(&TwoAddressInstructionPassID);

  addRegAssignAndRewriteFast();
}

Three passes: remove PHIs, make two-address instructions explicit, then let the Fast allocator assign and rewrite in one go. The Fast allocator writes physical registers directly into the instructions, so there is no separate rewriter.

Observed with llc -mtriple=aarch64 -O0 -debug-pass=Structure (filtered):

Eliminate PHI nodes for register allocation
Two-Address instruction pass
Fast Register Allocator
Prologue/Epilogue Insertion & Frame Finalization

Sources: llvm/lib/CodeGen/TargetPassConfig.cpp:1483-1488

The Optimised Chain (-O1 and above)

void TargetPassConfig::addOptimizedRegAlloc() {
  addPass(&DetectDeadLanesID);
  addPass(&InitUndefID);
  addPass(&ProcessImplicitDefsID);
  addPass(&UnreachableMachineBlockElimID);
  addPass(&LiveVariablesID);
  addPass(&MachineLoopInfoID);
  addPass(&PHIEliminationID);
  if (EarlyLiveIntervals)
    addPass(&LiveIntervalsID);
  addPass(&TwoAddressInstructionPassID);
  addPass(&RegisterCoalescerID);
  addPass(&RenameIndependentSubregsID);
  addPass(&MachineSchedulerID);
  if (addRegAssignAndRewriteOptimized()) {
    addPass(&StackSlotColoringID);
    addPostRewrite();
    addPass(&MachineCopyPropagationID);
    addPass(&MachineLICMID);
  }
}

(Comments removed; see the source for them.) addRegAssignAndRewriteOptimized adds the allocator, then the target's addPreRewrite() hook, then VirtRegRewriter, then a scoring pass that does nothing unless an ML eviction policy is being trained.

Sources: llvm/lib/CodeGen/TargetPassConfig.cpp:1459-1473, llvm/lib/CodeGen/TargetPassConfig.cpp:1493-1549

What Each Pass Does

# Pass (-debug-pass name) Command-line name Purpose
1 Detect Dead Lanes detect-dead-lanes Finds sub-register lanes that are never read and marks them dead or undef, so they do not create false liveness
2 Init Undef Pass init-undef An undef input does not stop the allocator from giving it the same register as an early-clobber result, so this pass replaces such undefined inputs with a temporary pseudo definition. It also rewrites NoReg tied operands back to IMPLICIT_DEF before two-address lowering
3 Process Implicit Definitions processimpdefs Removes IMPLICIT_DEF instructions and marks their uses undef
4 Remove unreachable machine basic blocks unreachable-mbb-elimination Required before LiveVariables
5 Live Variable Analysis livevars Computes kill flags on SSA MIR; used by the next two passes
6 Eliminate PHI nodes phi-node-elimination Replaces each PHI with COPYs in the predecessor blocks. See PHI Elimination and Two-Address Lowering
7 Two-Address instruction pass twoaddressinstruction For instructions whose result must use the same register as an operand, inserts a COPY so that holds. Ends SSA form
8 Slot index numbering, Live Interval Analysis slotindexes, liveintervals Number the instructions and compute live intervals (required by the coalescer, so they run here)
9 Register Coalescer register-coalescer Removes COPYs by merging the two registers into one. See Register Coalescer
10 Rename Disconnected Subregister Components rename-independent-subregs Splits a register whose sub-register parts are independent into separate registers
11 Machine Instruction Scheduler machine-scheduler Pre-RA scheduling; it tracks register pressure so it can avoid creating spills
12 (analyses) livedebugvars, livestacks, virtregmap, liveregmatrix, edge-bundles, spill-code-placement Required by Greedy; built just before it
13 Greedy Register Allocator greedy Assigns, evicts, splits and spills. See Greedy Register Allocator
14 Virtual Register Rewriter virtregrewriter Replaces each virtual register in the instructions with its physical register. See Rewriting and Post-RA Passes
15 Stack Slot Coloring stack-slot-coloring Lets spill slots whose live ranges do not overlap share memory
16 Machine Copy Propagation machine-cp Forwards copies that the coalescer could not remove
17 Machine Loop Invariant Code Motion machinelicm Post-RA: hoists reloads and rematerialised constants out of loops

Observed on AArch64 at -O2

From llc -mtriple=aarch64 -O2 -debug-pass=Structure on a trivial function, filtered to the passes around allocation:

AArch64 PTRUE Coalescing
Detect Dead Lanes
Init Undef Pass
Process Implicit Definitions
Remove unreachable machine basic blocks
Live Variable Analysis
Eliminate PHI nodes for register allocation
Two-Address instruction pass
Slot index numbering
Live Interval Analysis
Register Coalescer
Rename Disconnected Subregister Components
Machine Instruction Scheduler
AArch64 Post Coalescer pass
Debug Variable Analysis
Live Stack Slot Analysis
Virtual Register Map
Live Register Matrix
Spill Code Placement Analysis
Greedy Register Allocator
Virtual Register Rewriter
Register Allocation Pass Scoring
Stack Slot Coloring
Machine Copy Propagation Pass
Machine Loop Invariant Code Motion
Prologue/Epilogue Insertion & Frame Finalization

Two AArch64 passes appear. AArch64 PTRUE Coalescing comes from addMachineSSAOptimization, earlier in the pipeline. AArch64 Post Coalescer pass sits right before the allocator because AArch64 overrides addRegAssignAndRewriteOptimized to add it first:

bool AArch64PassConfig::addRegAssignAndRewriteOptimized() {
  addPass(createAArch64PostCoalescerPass());
  return TargetPassConfig::addRegAssignAndRewriteOptimized();
}

This pattern (override a hook, add your pass, call the base version) is the usual way for a target to insert work around allocation. See Pass Pipeline Hooks.

Sources: llvm/lib/Target/AArch64/AArch64TargetMachine.cpp:817-829, llvm/lib/Target/AArch64/AArch64TargetMachine.cpp:970-973

Where the Target Can Plug In

flowchart TD
  A["addPreRegAlloc()"] --> B["addOptimizedRegAlloc()<br/>or addFastRegAlloc()"]
  B --> C["addRegAssignAndRewriteOptimized()<br/>allocator pass"]
  C --> D["addPreRewrite()"]
  D --> E["VirtRegRewriter"]
  E --> F["StackSlotColoring"]
  F --> G["addPostRewrite()"]
  G --> H["MachineCopyPropagation, MachineLICM"]
  H --> I["addPostRegAlloc()"]
  I --> J["Prologue/Epilogue insertion"]
Loading
Hook When it runs AArch64 uses it for
addPreRegAlloc Before the whole allocation chain Dead register definitions to wzr/xzr, AdvSIMD scalar, machine pipeliner
addRegAssignAndRewriteOptimized Wraps allocator plus rewriter The Post Coalescer pass
addPreRewrite After the allocator, before the rewriter (not used)
addPostRewrite After the rewriter A sub-register mitigation pass, when enabled
addPostRegAlloc After the chain, before prologue/epilogue Redundant copy elimination, A57 FP load balancing
addPostFastRegAllocRewrite After the Fast allocator (not used)

Sources: llvm/lib/Target/AArch64/AArch64TargetMachine.cpp:852-886, llvm/lib/CodeGen/TargetPassConfig.cpp:1157-1184, llvm/lib/CodeGen/InitUndef.cpp:9-37

What Changes Along the Way

After MIR property
Instruction selection SSA, virtual registers, PHIs, IMPLICIT_DEFs
PHI elimination No PHIs; extra COPYs at the end of predecessors
Two-address Not SSA any more: a register can be defined more than once (TiedOpsRewritten)
Coalescer Most COPYs gone; fewer, longer virtual registers
Allocator VirtRegMap says where each register lives; instructions still name virtual registers; spill code inserted
Rewriter Only physical registers; NoVRegs property set

The Coalescer page shows one loop at each of these points.

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