Skip to content

Register Allocation 09.4 pass config hooks

Mugundan edited this page Nov 27, 2026 · 1 revision

Pass Pipeline Hooks

Relevant source files

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

Your `PassConfig` (derived from `TargetPassConfig`) decides which passes run around register allocation. Most targets need no change to the allocation chain itself: they add a few target passes before or after it through virtual hooks.

Purpose and Scope

This page lists the hooks, where each runs, and common reasons to use them. The full pass order is on The Register Allocation Pipeline.

The Hooks

Hook (TargetPassConfig.h) Line Runs Typical use
addMachineSSAOptimization() 365 Before the allocation chain, on SSA MIR Target SSA peepholes (AArch64: PTRUE coalescing)
addPreRegAlloc() 379 Just before the allocation chain Passes that shape register pressure or constraints (AArch64: dead register definitions to wzr/xzr, AdvSIMD scalar, machine pipeliner)
createTargetRegisterAllocator(bool Optimized) 383 Chooses the allocator when no -regalloc= is given Pick a different default
addFastRegAlloc() 387 The whole -O0 chain Rarely overridden
addOptimizedRegAlloc() 392 The whole optimised chain Rarely overridden; insert passes with insertPass instead
addPreRewrite() 406 After the allocator, before VirtRegRewriter Work that needs VirtRegMap (assignments known, operands still virtual)
addPostFastRegAllocRewrite() 412 After the Fast allocator Same idea for -O0
addPostRewrite() 416 After the rewriter and stack slot coloring Expand pseudos that depend on the chosen registers, before copy propagation
addPostRegAlloc() 420 After the chain, before prologue/epilogue insertion Post-RA cleanups (AArch64: redundant copy elimination, A57 FP load balancing)
addRegAssignAndRewriteFast() 470 Wraps Fast allocator Run several allocators in sequence at -O0
addRegAssignAndRewriteOptimized() 471 Wraps allocator + rewriter Insert a pass right before allocation, or run several allocators

Sources: llvm/include/llvm/CodeGen/TargetPassConfig.h:365-471, llvm/lib/Target/AArch64/AArch64TargetMachine.cpp:817-886

Pattern 1: Add a Pass Right Before the Allocator

AArch64 does exactly this, and calls the base version so the standard allocator and rewriter still run:

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

The pass runs after coalescing and scheduling, so it sees the final live intervals and can make last adjustments.

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

Pattern 2: Allocate Register Classes Separately

Pass a filter to the allocator so each run handles only some classes. AMDGPU defines onlyAllocateSGPRs and onlyAllocateVGPRs and creates allocators with them:

  return createGreedyRegisterAllocator(onlyAllocateSGPRs);
  ...
  return createBasicRegisterAllocator(onlyAllocateVGPRs);

A filter has the shape of RegAllocFilterFunc and returns whether a register should be allocated by that run (see framework). Only the last run's rewriter should clear virtual registers.

Sources: llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp:214-365

Pattern 3: Change the Default Allocator

// ToyTargetMachine.cpp (illustrative, not LLVM source)
FunctionPass *ToyPassConfig::createTargetRegisterAllocator(bool Optimized) {
  if (Optimized)
    return createGreedyRegisterAllocator();
  return createFastRegisterAllocator();
}

That is what the base class already does (TargetPassConfig.cpp:1413-1418), so override it only if you really want another default. -regalloc= on the command line always wins.

Sources: llvm/lib/CodeGen/TargetPassConfig.cpp:1413-1438

Rules for Passes You Insert

Position You may You must
Before the chain (addPreRegAlloc) Change instructions freely; MIR is SSA Keep SSA form
Between LiveIntervals and the rewriter Change instructions Keep SlotIndexes and LiveIntervals up to date (liveness), and declare them preserved
After the rewriter Use physical registers only Keep kill flags and block live-ins correct; create no virtual registers (unless the scavenger runs later)

Run llc -verify-machineinstrs while developing: it checks these invariants after every pass.

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