Skip to content

Register Allocation 07.3 splitkit

Mugundan edited this page Nov 27, 2026 · 1 revision

Live Range Splitting

Relevant source files

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

SplitKit is the library Greedy uses to cut a live interval into pieces. Greedy decides *where* to cut; SplitKit does the cutting: it creates the new virtual registers, inserts the `COPY`s at the cut points, and rebuilds the live intervals.

Purpose and Scope

This page explains the two SplitKit classes, the "interval" vocabulary used by SplitEditor, and how one Greedy strategy drives it. Which strategy Greedy picks is on Greedy Register Allocator.

Two Classes

Class Role
SplitAnalysis Read-only. For the interval being split, finds every use (analyzeUses) and, for each block it is live in, a BlockInfo: first and last use in the block, and whether the value is live in and live out (calcLiveBlockInfo)
SplitEditor Makes the change. Lets the caller open new intervals, say which parts of the original belong to which, and then finish() rewrites everything

Sources: llvm/lib/CodeGen/SplitKit.h:96-121, llvm/lib/CodeGen/SplitKit.h:263, llvm/lib/CodeGen/SplitKit.cpp:167-199

The Editing Vocabulary

SplitEditor thinks in terms of numbered intervals. Interval 0 is the complement: everything not explicitly given to another interval. The caller opens new intervals and marks the regions they cover:

Call Meaning
openIntv() Create a new interval and make it current; returns its number
selectIntv(Idx) Make an existing interval current
enterIntvBefore(Idx) The current interval starts just before Idx; a copy from the complement is inserted there
enterIntvAtEnd(MBB) The current interval starts at the end of MBB (for a value live out of the block)
useIntv(MBB) / useIntv(Start, End) The current interval covers this whole block, or this range
leaveIntvAfter(Idx) The current interval ends just after Idx; a copy back to the complement is inserted
leaveIntvBefore(Idx) The current interval ends just before Idx
finish(LRMap) Create the new registers, insert the copies, rewrite every operand (rewriteAssigned), recompute the live intervals

High-level helpers wrap the common patterns: splitSingleBlock(BI) isolates the uses in one block, and splitLiveThroughBlock handles a block the value passes through.

Sources: llvm/lib/CodeGen/SplitKit.cpp:690-822, llvm/lib/CodeGen/SplitKit.cpp:1361, llvm/lib/CodeGen/SplitKit.cpp:1545, llvm/lib/CodeGen/SplitKit.cpp:1650-1676

A Picture

An interval %v that is live through a loop containing a call, used before and after the loop:

           entry      loop (contains call)      exit
%v         |=====================================|

Greedy finds that every caller-saved register is clobbered inside the loop, but w8 is free outside it. A region split could produce (illustrative, not llc output):

entry:   %v1 = ...            ; %v1 in w8
         %v2 = COPY %v1       ; leave interval at loop entry
loop:    ... uses of %v2 ...  ; %v2 needs a callee-saved reg, or is spilled
exit:    %v3 = COPY %v2       ; enter interval at loop exit
         ... uses of %v3 ...  ; %v3 in w8 again

%v1 and %v3 can share w8. The copies are on the loop boundary, which runs once, not inside the loop, which runs many times. That choice of cut points is what region splitting optimises.

How Region Splitting Chooses Cut Points

  1. EdgeBundles groups CFG edges into bundles: sets of block boundaries that must agree on whether the value is in a register or in memory.
  2. For one candidate physical register, addSplitConstraints records, per block, whether it prefers the value in a register on entry and exit (no interference there) or in memory (interference), weighted by block frequency. addThroughConstraints handles blocks the value only passes through.
  3. SpillPlacement solves for the bundle assignment with the lowest total cost (a min-cut style computation), and growRegion extends the region through neighbouring live-through blocks while that pays.
  4. calculateRegionSplitCost compares candidates, and splitAroundRegion uses SplitEditor to create the interval for the region in the best register, leaving the rest in the complement.

The result is that values stay in registers where they are used heavily and pay copies only on cold edges.

Sources: llvm/lib/CodeGen/RegAllocGreedy.cpp:739-1206, llvm/lib/CodeGen/RegAllocGreedy.cpp:1239-1334, llvm/lib/CodeGen/EdgeBundles.cpp

The Complement and Spill Modes

After the region intervals are cut out, interval 0 (the complement) holds everything else. If it is later spilled, how its copies overlap the new intervals matters. -split-spill-mode chooses:

Mode Behaviour (from SplitKit.h)
default (SM_Partition) Make the complement not overlap the other intervals
size (SM_Size) Let intervals overlap to minimise the number of inserted copies
speed (SM_Speed) Like size, but minimise the expected execution frequency of the copies

Sources: llvm/lib/CodeGen/SplitKit.h:281-311, llvm/lib/CodeGen/RegAllocGreedy.cpp:86-90

What Happens to the Pieces

finish returns the new registers. Greedy sets their stages (Greedy: Stages): pieces that should be spilled rather than split again get RS_Spill, pieces that made no progress get RS_Split2, and the rest start again at RS_New. All of them go back on the queue. Each piece records its original register (VirtRegMap::setIsSplitFromReg), so if several pieces are spilled they share one stack slot (The Inline Spiller).

Copies between pieces that end up in the same physical register become identity copies and are deleted by the rewriter.

Target Involvement

SplitKit inserts plain COPYs, which the target turns into moves with copyPhysReg after allocation. Two hooks influence splitting directly:

Hook Effect
TargetRegisterInfo::shouldRegionSplitForVirtReg Return false to skip region splitting for a register (compile time on huge intervals)
TargetRegisterInfo::getLargestLegalSuperClass Lets a split piece use a larger class than the original when its uses allow it

Sources: llvm/include/llvm/CodeGen/TargetRegisterInfo.h:740, llvm/include/llvm/CodeGen/TargetRegisterInfo.h:1051

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