Repository navigation
Register Allocation 04 preparation
Relevant source files
The following files were used as context for generating this wiki page:
This page gives the big picture and shows the same loop at each step. The details are on PHI Elimination and Two-Address Lowering and Register Coalescer.
The first two passes make the code correct for allocation by adding COPY instructions, many of them unnecessary. The coalescer then removes every copy it can prove is safe to remove. This split keeps each pass simple: PHI elimination and two-address lowering never have to be clever, because the coalescer cleans up after them.
flowchart LR
A["SSA MIR<br/>PHIs, tied operands unmet"] --> B["PHIElimination<br/>PHI becomes COPYs"]
B --> C["TwoAddressInstruction<br/>tied operands get a COPY"]
C --> D["RegisterCoalescer<br/>merge COPY source and destination"]
D --> E["Allocator input<br/>few COPYs, long live ranges"]
Source (after LoopStrengthReduce):
loop:
%lsr.iv = phi i64 [ %lsr.iv.next, %loop ], [ 0, %entry ]
%acc = phi i32 [ 0, %entry ], [ %acc.next, %loop ]
...
%acc.next = add i32 %acc, %v
%lsr.iv.next = add nuw nsw i64 %lsr.iv, 1After two-address lowering (llc -mtriple=aarch64 -O2 -stop-after=twoaddressinstruction): the two PHIs are gone and there are sixteen COPYs (eight in bb.0, seven in bb.1, one in bb.2).
bb.0.entry:
%5:gpr32 = COPY killed $w1
%4:gpr64common = COPY killed $x0
%8:gpr32all = COPY $wzr
%7:gpr32all = COPY killed %8
%9:gpr64all = COPY $xzr
%6:gpr64all = COPY killed %9
%15:gpr64common = COPY killed %6
%16:gpr32 = COPY killed %7
bb.1.loop:
%1:gpr32 = COPY killed %16
%0:gpr64common = COPY killed %15
%10:gpr32 = LDRWroX %4, %0, 0, 1 :: (load (s32) from %ir.scevgep)
%11:gpr32 = ADDWrr killed %1, killed %10
%2:gpr32all = COPY killed %11
%12:gpr64sp = nuw nsw ADDXri killed %0, 1, 0
%3:gpr64all = COPY %12
%13:gpr32 = COPY killed %12.sub_32
dead $wzr = SUBSWrr killed %13, %5, implicit-def $nzcv
%15:gpr64common = COPY killed %3
%16:gpr32 = COPY %2
Bcc 3, %bb.1, implicit killed $nzcv
B %bb.2
bb.2.exit:
$w0 = COPY killed %2
RET_ReallyLR implicit killed $w0
Note that %15 and %16 are defined twice (once per predecessor): the code is no longer in SSA form.
After the coalescer (-stop-after=register-coalescer): only the copies that connect to physical registers are left.
bb.0.entry:
%5:gpr32 = COPY $w1
%4:gpr64common = COPY $x0
%15:gpr64common = COPY $xzr
%16:gpr32 = COPY $wzr
bb.1.loop:
%10:gpr32 = LDRWroX %4, %15, 0, 1 :: (load (s32) from %ir.scevgep)
%16:gpr32 = ADDWrr %16, %10
%15:gpr64common = nuw nsw ADDXri %15, 1, 0
dead $wzr = SUBSWrr %15.sub_32, %5, implicit-def $nzcv
Bcc 3, %bb.1, implicit killed $nzcv
B %bb.2
bb.2.exit:
$w0 = COPY %16
RET_ReallyLR implicit $w0
Each PHI's whole web of copies (%0, %3, %6, %9, %12, %15, and the 32-bit view %13) collapsed into a single register, %15, whose class was narrowed to the intersection gpr64common. The accumulator web became %16. The machine scheduler then moved the ADDWrr below the compare, which is the order you see in the allocated loop on AArch64 Walkthroughs.
After allocation:
- The copies of
$xzr/$wzrbecamemov x9, xzrandmov w8, wzr. - The copies from
$w1and$x0disappeared, because%5and%4were given$w1and$x0(hints from the copies). -
$w0 = COPY %16stayed, asmov w0, w8.%16cannot be given$w0: the pointer%4lives in$x0for the whole loop, andw0is the low half ofx0, so they share a register unit and interfere.
Greedy does try to give both sides of a copy the same register (through hints), and the rewriter deletes copies that end up as $wN = COPY $wN. But hints are only preferences that can be broken under pressure, while coalescing is a guarantee made before allocation starts. The trade-off is that coalescing makes live ranges longer, which can make allocation harder; the coalescer therefore refuses some merges (see Register Coalescer), and Greedy can undo a merge later by splitting.
| Pass | What it does to prepare |
|---|---|
ProcessImplicitDefs |
Deletes IMPLICIT_DEFs and marks their uses undef, so undefined values do not occupy registers |
DetectDeadLanes |
Marks sub-register lanes that are never read, so they do not extend liveness |
RenameIndependentSubregs |
If a register's sub-register parts are never used together, gives each part its own register |
MachineScheduler |
Reorders instructions within blocks; its pressure tracking tries to keep the number of simultaneously live values below the register count |
- 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