Repository navigation
Register Allocation 07.3 splitkit
Relevant source files
The following files were used as context for generating this wiki page:
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.
| 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
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
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.
-
EdgeBundlesgroups CFG edges into bundles: sets of block boundaries that must agree on whether the value is in a register or in memory. - For one candidate physical register,
addSplitConstraintsrecords, 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.addThroughConstraintshandles blocks the value only passes through. -
SpillPlacementsolves for the bundle assignment with the lowest total cost (a min-cut style computation), andgrowRegionextends the region through neighbouring live-through blocks while that pays. -
calculateRegionSplitCostcompares candidates, andsplitAroundRegionusesSplitEditorto 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
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
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.
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
- Computer Architecture Overview
- Fundamentals: CPU, Memory and Instructions
- Pipelining
- Branches and Remedies
- Instruction-Level Parallelism
- Memory and Ordering
- Caches and Coherence
- Multicore
- Multithreading in Hardware
- SIMD and GPUs
- Glossary and Summary
- Hexagon Overview and Processor Properties
- ISA Fundamentals: Packets, Slots and Threads
- Registers
- Instruction Encoding and Mnemonics
- Data, Memory and Control Instructions
- Floating Point, HVX and Alignment
- Caches, MMU and Glossary
- Operating Systems Overview
- Processes
- Threads
- CPU Scheduling
- Synchronization
- Deadlock
- Memory Management
- Virtual Memory
- Files, Storage and I/O
- Kernel and System Calls
- Interview Q&A and Glossary
- C++ for LLVM Development: Overview
- C++ Basics
- Classes and RAII
- Ownership and Move Semantics
- Templates and Lambdas
- LLVM Data Structures (ADT)
- LLVM-Style RTTI: isa, cast, dyn_cast
- Errors, Output and Assertions
- Iterators and Ranges
- Building, Debugging and Exercises
- LLVM IR Overview
- Structure: Modules, Functions, Blocks and Values
- Type System
- Arithmetic, Bitwise and Cast Instructions
- Compare and Select
- Memory: alloca, load, store and getelementptr
- Control Flow: br, switch, ret and call
- SSA and Phi
- Control Flow Graph and Loops
- Dominance, Dominator Tree and Dominance Frontier
- Data-flow Analysis
- Writing an IR Pass
- Common Passes
- Exercises, Tooling and Glossary
- SelectionDAG Overview
- Pipeline, Phases and Traversal Orders
- Core Data Structures
- DAG Construction
- DAG Combiner
- Legalization
- Type Legalization
- Operation and Vector Legalization
- Instruction Selection
- TableGen Pattern Language
- Matcher Interpreter and Folding
- AArch64 Instruction Selection
- Scheduling and MIR Emission
- PHI Node Resolution
- AArch64 Walkthroughs
- Arithmetic and Data Movement
- Memory Instructions
- Control Flow and Calls
- End-to-End Example
- Tooling, Debugging and Extending
- Selector Comparison and Glossary
- Assignments: Build the Toy Backend
- Stage 1: Build Setup
- Stage 2: Target Registration
- Stage 3: TableGen Skeleton
- Stage 4: MC Layer for Assembly Output
- Stage 5: TargetMachine and Subtarget
- Stage 6: TargetLowering, Arguments and Returns
- Stage 7: Instruction Selection and the First Function
- Stage 8: Registers, Copies and Spills
- Stage 9: Frame Lowering
- Stage 10: Memory, Globals and Control Flow
- Stage 11: Calls
- Stage 12: Scheduling Model
- Stage 13: Object Files
- Stage 14: Capstone, a Complete Toy Backend
- Target Bring-up Overview
- Directory Layout, CMake and Registration
- The Target .td File
- TargetMachine and Subtarget
- TargetLowering Setup
- Calling Conventions
- Instructions and Instruction Selection
- Registers and Allocation Hooks
- Frame Lowering
- AsmPrinter and the MC Layer
- Scheduling
- A Minimal Toy Target
- Bring-up Checklist and AArch64 File Map
- Tooling and Glossary
- Calling Conventions Overview
- The CallingConv.td Language
- CCState and CCValAssign
- LowerFormalArguments
- LowerCall
- LowerReturn and CanLowerReturn
- Varargs, byval, sret and swifterror
- Tail Calls and Sibling Calls
- Custom Backend Guide and Checklist
- AArch64 Walkthroughs
- Tooling and Glossary
- Frame Lowering Overview
- MachineFrameInfo
- TargetFrameLowering Hooks
- The PEI Pass
- Callee-Saved Registers and Stack Layout
- Frame Index Elimination and the Register Scavenger
- Shrink Wrapping
- CFI and Unwind Information
- Custom Backend Guide and Checklist
- AArch64 Walkthroughs
- Tooling and Glossary
- MC Layer Overview
- Core Data Structures
- Streamers
- Assembler and Layout
- Fixups and Relocations
- Writing an MC Layer for a Custom Backend
- MCAsmInfo and the Instruction Printer
- Code Emitter
- AsmBackend and Object Writer
- Assembly Parser and Disassembler
- Bring-up Checklist and Common Failures
- Tooling and Glossary
- Register Allocation Overview
- The Register Allocation Pipeline
- Registers, Virtual Registers and Register Classes
- Liveness: Slot Indexes and Live Intervals
- Preparing Code for Allocation
- PHI Elimination and Two-Address Lowering
- Register Coalescer
- Allocation Framework
- The Register Allocators
- Fast Register Allocator
- Basic Register Allocator
- Greedy Register Allocator
- PBQP Register Allocator
- Spilling and Splitting
- Spill Weights and Copy Hints
- The Inline Spiller
- Live Range Splitting
- Rewriting and Post-RA Passes
- Writing Register Allocation Support for a Custom Backend
- Register Definitions in TableGen
- TargetRegisterInfo Hooks
- TargetInstrInfo Hooks
- Pass Pipeline Hooks
- Bring-up Checklist and Common Failures
- AArch64 Walkthroughs
- Tooling and Debugging
- Glossary and Where Do I Look