Repository navigation
Register Allocation 10 aarch64 walkthroughs
Relevant source files
The following files were used as context for generating this wiki page:
Each example shows one idea from earlier pages in practice. Read them in order; each says what to look for.
| # | Example | Shows |
|---|---|---|
| 1 | Straight-line arithmetic | Hints and identity copies |
| 2 | Value live across a call | Register masks and callee-saved registers |
| 3 | Simple loop | PHI elimination, coalescing, loop-carried values |
| 4 | Real spills |
InlineSpiller stores and reloads |
| 5 | Forced pressure | -stress-regalloc |
| 6 | Loop containing a call | Callee-saved registers versus spilling |
| 7 | The same code at -O0
|
Fast allocator |
define i32 @f(i32 %a, i32 %b, i32 %c) {
%m = mul i32 %a, %b
%s = add i32 %m, %c
ret i32 %s
}Before Greedy (-stop-before=greedy):
%1:gpr32 = COPY $w1
%0:gpr32 = COPY $w0
%2:gpr32 = COPY $w2
%4:gpr32 = MADDWrrr %0, %1, %2
$w0 = COPY %4
RET_ReallyLR implicit $w0
After the rewriter:
renamable $w0 = MADDWrrr killed renamable $w0, killed renamable $w1, killed renamable $w2
RET_ReallyLR implicit $w0
Assembly: madd w0, w0, w1, w2 then ret.
What to look for. Each copy from or to $wN gave a copy hint. %0 and %4 don't interfere (%0 dies exactly where %4 is born, see slot indexes), so both got $w0. All four copies became identity copies and the rewriter deleted them.
declare i32 @g(i32)
define i32 @h(i32 %a, i32 %b) {
%x = call i32 @g(i32 %a)
%y = add i32 %x, %b
ret i32 %y
}Before Greedy:
%1:gpr32 = COPY $w1
%0:gpr32 = COPY $w0
ADJCALLSTACKDOWN 0, 0, implicit-def dead $sp, implicit $sp
$w0 = COPY %0
BL @g, csr_aarch64_aapcs, implicit-def dead $lr, implicit $sp, implicit $w0, implicit-def $sp, implicit-def $w0
ADJCALLSTACKUP 0, 0, implicit-def dead $sp, implicit $sp
%2:gpr32 = COPY $w0
$w0 = ADDWrr %2, %1
RET_ReallyLR implicit $w0
Assembly:
stp x30, x19, [sp, #-16]! // 16-byte Folded Spill
mov w19, w1
bl g
add w0, w0, w19
ldp x30, x19, [sp], #16 // 16-byte Folded Reload
ret
What to look for. csr_aarch64_aapcs on the BL is the register mask. It says every register outside CSR_AArch64_AAPCS is destroyed by the call. %1 (%b) is live across the call, so checkInterference returns IK_RegMask for every caller-saved register. The first free register is callee-saved w19. Prologue insertion then saves x19 (and x30, because there is a call). The stp/ldp are not allocator spills, even though the comment says "Spill".
define i32 @sum(ptr %p, i32 %n) {
entry:
br label %loop
loop:
%i = phi i32 [0, %entry], [%i.next, %loop]
%acc = phi i32 [0, %entry], [%acc.next, %loop]
%idx = zext i32 %i to i64
%addr = getelementptr i32, ptr %p, i64 %idx
%v = load i32, ptr %addr
%acc.next = add i32 %acc, %v
%i.next = add i32 %i, 1
%c = icmp ult i32 %i.next, %n
br i1 %c, label %loop, label %exit
exit:
ret i32 %acc.next
}The snapshots after two-address lowering and after the coalescer are on Preparing Code for Allocation. After the rewriter:
bb.0.entry:
liveins: $w1, $x0
renamable $x9 = COPY $xzr
renamable $w8 = COPY $wzr
bb.1.loop:
liveins: $w1, $w8, $x0, $x9
renamable $w10 = LDRWroX renamable $x0, renamable $x9, 0, 1 :: (load (s32) from %ir.scevgep)
renamable $x9 = nuw nsw ADDXri killed renamable $x9, 1, 0
dead $wzr = SUBSWrr renamable $w9, renamable $w1, implicit-def $nzcv
renamable $w8 = ADDWrr killed renamable $w8, killed renamable $w10
Bcc 3, %bb.1, implicit killed $nzcv
B %bb.2
bb.2.exit:
liveins: $w8
$w0 = COPY killed renamable $w8
RET_ReallyLR implicit $w0
Assembly:
sum:
mov x9, xzr
mov w8, wzr
.LBB0_1:
ldr w10, [x0, x9, lsl #2]
add x9, x9, #1
cmp w9, w1
add w8, w8, w10
b.lo .LBB0_1
mov w0, w8
ret
What to look for.
- Each PHI became one register (
%15→x9,%16→w8) after coalescing. The loop has no copies at all. - Registers come from the rotated allocation order:
w8,x9,w10first. -
%15.sub_32was rewritten to$w9: a sub-register of$x9. -
liveins:lists appeared;addMBBLiveInscomputed them. -
mov w0, w8stays becausew0overlapsx0, which holds the pointer through the whole loop.
24 volatile loads, all live across a call. Volatile loads cannot be rematerialised, and 24 values do not fit in the callee-saved registers.
declare void @g()
define i32 @p(ptr %p) {
%q1 = getelementptr i32, ptr %p, i64 1
%v1 = load volatile i32, ptr %q1
... ; %v2 .. %v24 the same way
call void @g()
%s1 = add i32 %v1, %v2
... ; sum all 24
ret i32 %s23
}After the rewriter (spill and reload lines only):
STRWui killed renamable $w8, %stack.11, 0 :: (store (s32) into %stack.11)
STRWui killed renamable $w8, %stack.2, 0 :: (store (s32) into %stack.2)
... 13 stores in total ...
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)
... 13 reloads in total ...
Remark (-pass-remarks-missed=regalloc):
remark: <unknown>:0:0: 13 spills 1.300000e+01 total spills cost 13
reloads 1.300000e+01 total reloads cost generated in function
Assembly excerpt:
stp x20, x19, [sp, #144] // 16-byte Folded Spill
str w8, [sp, #32] // 4-byte Spill
stp w8, w10, [sp, #48] // 8-byte Folded Spill
...
bl g
ldp w12, w9, [sp, #12] // 8-byte Folded Reload
ldr w9, [sp, #52] // 4-byte Reload
What to look for.
- 11 values went into callee-saved
w19–w29; the other 13 were spilled, each to its own slot%stack.0–%stack.12. - Each spill is
STRWuiright after its definition; each reload isLDRWuiright before its use. These come fromAArch64InstrInfo::storeRegToStackSlotandloadRegFromStackSlot(TII hooks). - In MIR the slot is still a frame index with offset 0. Prologue/epilogue insertion turned it into
[sp, #32]. - The AArch64 load/store optimiser later paired adjacent slots, hence "8-byte Folded Spill".
A spill that did not happen. The first attempt at this test computed 12 values with mul i32 %a, C instead of loading them. It produced no allocator spills: 11 values went into callee-saved registers, and the constants were rematerialised as mov w24, #11 and so on. Only values that are expensive to recompute get spilled.
The loop from example 3, with every register class cut to 2 registers (-stress-regalloc=2):
sum:
sub sp, sp, #32
mov x9, xzr
mov w8, wzr
str w1, [sp, #12] // 4-byte Spill
str x0, [sp] // 8-byte Spill
.LBB0_1:
str w8, [sp, #28] // 4-byte Spill
ldr x8, [sp] // 8-byte Reload
ldr w8, [x8, x9, lsl #2]
add x9, x9, #1
str x9, [sp, #16] // 8-byte Spill
str w8, [sp, #24] // 4-byte Spill
ldr w8, [sp, #12] // 4-byte Reload
cmp w9, w8
ldp w9, w8, [sp, #24] // 8-byte Folded Reload
add w8, w8, w9
ldr x9, [sp, #16] // 8-byte Reload
b.lo .LBB0_1
mov w0, w8
add sp, sp, #32
ret
What to look for. The loop now needs four values (p, n, i, acc) plus a temporary, but only two registers exist. n and p are spilled once before the loop, then reloaded at each use. This is the cheapest way to make a small test exercise your spill hooks while bringing up a backend (checklist).
declare void @g()
define i32 @lc(ptr %p, i32 %n, i32 %k) {
entry:
br label %loop
loop:
%i = phi i32 [0, %entry], [%i.next, %latch]
%acc = phi i32 [0, %entry], [%acc.next, %latch]
... ; load %v = p[i]; acc += v
%c0 = icmp eq i32 %v, %k
br i1 %c0, label %cold, label %latch
cold:
call void @g()
br label %latch
latch:
... ; i++; loop while i < n
exit:
ret i32 %acc.next
}Assembly:
lc:
str x30, [sp, #-64]! // 8-byte Folded Spill
stp x24, x23, [sp, #16] // 16-byte Folded Spill
stp x22, x21, [sp, #32] // 16-byte Folded Spill
stp x20, x19, [sp, #48] // 16-byte Folded Spill
mov w19, w2
mov w20, w1
mov x21, x0
mov x23, xzr
mov w22, wzr
b .LBB0_2
.LBB0_1: // %latch
add x23, x23, #1
add w22, w22, w24
cmp w23, w20
b.hs .LBB0_4
.LBB0_2: // %loop
ldr w24, [x21, x23, lsl #2]
cmp w24, w19
b.ne .LBB0_1
bl g
b .LBB0_1
.LBB0_4: // %exit
mov w0, w22
ldp x20, x19, [sp, #48] // 16-byte Folded Reload
...
ret
What to look for. Every value live in the loop (k, n, p, i, acc, v) is also live across bl g, so none of them can sit in a caller-saved register. Greedy put all six in callee-saved w19–x24. That costs four save/restore pairs, once per call of lc, and no memory traffic inside the loop. Splitting around the call would add copies or reloads on every iteration that takes the call. With callee-saved registers free, the cost model (Greedy: callee-saved registers) prefers them. A loop with more live values than callee-saved registers would force splitting or spilling.
Example 1 at -O0. Before the Fast allocator:
%0:gpr32 = COPY $w0
%1:gpr32 = COPY $w1
%2:gpr32 = COPY $w2
%3:gpr32 = MADDWrrr %0, %1, $wzr
%4:gpr32 = ADDWrr %3, %2
$w0 = COPY %4
RET_ReallyLR implicit $w0
After regallocfast:
renamable $w8 = MADDWrrr killed renamable $w0, killed renamable $w1, $wzr
renamable $w0 = ADDWrr killed renamable $w8, killed renamable $w2
RET_ReallyLR implicit killed $w0
What to look for. No combiner ran, so mul and add stay separate (the mul is a madd with wzr). Fast followed the copy chains and removed every copy. There is one block, so no slots were needed. With a branch, values crossing the block boundary would be stored and reloaded.
llc -mtriple=aarch64 -O2 file.ll -stop-before=greedy -o -
llc -mtriple=aarch64 -O2 file.ll -stop-after=virtregrewriter -o -
llc -mtriple=aarch64 -O2 file.ll -o -
llc -mtriple=aarch64 -O2 file.ll -o /dev/null -pass-remarks-missed=regalloc
llc -mtriple=aarch64 -O2 -stress-regalloc=2 file.ll -o -
llc -mtriple=aarch64 -O0 file.ll -stop-after=regallocfast -o -
See Tooling and Debugging for more.
- 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