Repository navigation
Register Allocation 07 spilling splitting
Relevant source files
The following files were used as context for generating this wiki page:
This page introduces the three tools and the vocabulary. The details are on Spill Weights, The Inline Spiller and Live Range Splitting.
Spilling interval %v means:
- give
%va stack slot (a frame index, printed%stack.N); - after every definition of
%v, insert a store to the slot; - before every use, insert a load from the slot into a brand-new, tiny virtual register;
- delete
%v. The tiny registers live for one instruction each, so they almost always fit.
Real output from the 24-load test (-stop-after=virtregrewriter, excerpt):
STRWui killed renamable $w8, %stack.11, 0 :: (store (s32) into %stack.11)
STRWui killed renamable $w8, %stack.2, 0 :: (store (s32) into %stack.2)
...
BL @g, csr_aarch64_aapcs, implicit-def dead $lr, implicit $sp, implicit-def $sp
renamable $w9 = LDRWui %stack.11, 0 :: (load (s32) from %stack.11)
renamable $w12 = LDRWui %stack.12, 0 :: (load (s32) from %stack.12)
Each loaded value was stored right after its load from %p, and is reloaded after the call just before the add that uses it. The stack section lists the slots as type: spill-slot with offset: 0. Real offsets are assigned later by prologue/epilogue insertion.
Both show up as stores and loads around sp, and the assembly comments can say "Spill" for both. Tell them apart like this:
| Allocator spill | Callee-saved save | |
|---|---|---|
| Inserted by |
InlineSpiller (Greedy, Basic, PBQP) or Fast |
Prologue/epilogue insertion |
| Where | Next to the definition and the uses of a value | Function entry and every return |
| In MIR after the rewriter |
STRWui ... %stack.N (type spill-slot) |
Not there yet; appears after prologepilog
|
| Assembly comment (AArch64) |
4-byte Spill, 4-byte Reload (or Folded when the load/store optimiser pairs them) |
16-byte Folded Spill on stp x.., x.. at entry |
Reported by -pass-remarks-missed=regalloc
|
Yes | No |
In the 24-load test, the remark says 13 spills and 13 reloads, which matches exactly the 13 STRWui/LDRWui pairs. The six stp/ldp pairs of x19–x30 are callee-saved saves.
If a value is cheap to recompute and its inputs are always available, for example a constant mov w24, #11, the spiller recomputes it before each use instead of storing and loading it. No stack slot is needed. That is what happened in the first spill experiment on the walkthroughs page: constants stayed as MOVi32imm and nothing went to memory. Rematerialisation needs the target to mark such instructions isReMaterializable (see TargetInstrInfo Hooks).
Some instructions can read an operand directly from memory (x86 add eax, [rsp+8]). Instead of a load plus the instruction, the spiller asks the target to fold the stack slot into the instruction (foldMemoryOperandImpl). AArch64 is a load/store architecture and folds very little; x86 folds a lot.
Spilling a long interval everywhere is wasteful if it only conflicts in one place, such as across one call or inside one hot loop. Splitting replaces %v with several smaller registers connected by COPYs:
before: %v live from A to Z
after: %v1 live A..M, %v2 = COPY %v1 at M, %v2 live M..Z
Each piece is allocated separately. One piece may get a register, another may get a different register, and only a piece that still does not fit is spilled. Copies between pieces that end up in the same register are removed by the rewriter. Greedy is the only allocator that splits.
flowchart TD
G["Greedy selectOrSplit"] -->|"split"| SK["SplitAnalysis + SplitEditor<br/>(SplitKit.cpp)"]
G -->|"spill"| IS["InlineSpiller"]
SK --> LRE["LiveRangeEdit<br/>creates vregs, updates LiveIntervals"]
IS --> LRE
IS -->|"remat?"| RM["LiveRangeEdit::rematerializeAt"]
IS -->|"fold?"| FO["TII::foldMemoryOperand"]
IS -->|"store/load"| TII["TII::storeRegToStackSlot<br/>TII::loadRegFromStackSlot"]
W["VirtRegAuxInfo<br/>spill weights + hints"] --> G
LiveRangeEdit is the shared helper. It creates new virtual registers derived from an original (createFrom), keeps LiveIntervals correct, deletes dead definitions, and tells the allocator about changes through callbacks (LRE_CanEraseVirtReg, LRE_WillShrinkVirtReg, LRE_DidCloneVirtReg).
Sources: llvm/lib/CodeGen/LiveRangeEdit.cpp:55-426, llvm/lib/CodeGen/RegAllocGreedy.cpp:370-412
- 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