Repository navigation
SelectionDAG 01.1 pipeline
Relevant source files
The following files were used as context for generating this wiki page:
- llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
- llvm/include/llvm/CodeGen/DAGCombine.h
- llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
- llvm/lib/CodeGen/SelectionDAG/LegalizeTypes.cpp
- llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp
- llvm/lib/CodeGen/SelectionDAG/LegalizeVectorOps.cpp
- llvm/lib/CodeGen/SelectionDAG/ScheduleDAGRRList.cpp
It documents the control flow inside one CodeGenAndEmitDAG call and summarises the traversal strategy of each phase, because the traversal direction explains many behaviours (why a pattern can absorb its operands, why combines see operands in their original shape, why legalization repeats).
Each phase has its own page; this page is the reference for how they connect. See SelectionDAG Overview for the big picture.
CodeGenAndEmitDAG sets NewNodesMustHaveLegalTypes = false for the first combine, then switches it to true after type legalization.
sequenceDiagram
participant D as SelectionDAGISel
participant C as DAGCombiner
participant T as DAGTypeLegalizer
participant V as VectorLegalizer
participant L as SelectionDAGLegalize
participant S as Scheduler and InstrEmitter
D->>C: Combine(BeforeLegalizeTypes)
D->>T: LegalizeTypes()
alt types changed
D->>C: Combine(AfterLegalizeTypes)
end
D->>V: LegalizeVectors()
alt vectors changed
D->>T: LegalizeTypes() again
D->>C: Combine(AfterLegalizeVectorOps)
end
D->>L: Legalize()
D->>C: Combine(AfterLegalizeDAG)
D->>D: ComputeLiveOutVRegInfo()
D->>D: DoInstructionSelection()
D->>S: Run(), EmitSchedule()
Notes taken from the source:
- Combine runs 2 and 3 are conditional:
Combine(AfterLegalizeTypes)runsif (Changed)afterLegalizeTypes, and the vector-legalized sequence runs only ifLegalizeVectors()returned true. -
ComputeLiveOutVRegInfo(skipped at-O0) records known-bits and sign-bit facts for integer values copied to virtual registers, which later blocks use. - Debug dump titles in order: Initial selection DAG, Optimized lowered selection DAG, Type-legalized selection DAG, Optimized type-legalized selection DAG, Vector-legalized selection DAG, Vector/type-legalized selection DAG, Optimized vector-legalized selection DAG, Legalized selection DAG, Optimized legalized selection DAG, Selected selection DAG.
Sources: llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp:947-1200, llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp:910-945
The CombineLevel enum tells each fold how much it may assume. Inside DAGCombiner::Run three flags are derived from it.
| Run | CombineLevel |
LegalTypes |
LegalOperations |
LegalDAG |
|---|---|---|---|---|
| 1 | BeforeLegalizeTypes |
false | false | false |
| 2 | AfterLegalizeTypes |
true | false | false |
| 3 | AfterLegalizeVectorOps |
true | true | false |
| 4 | AfterLegalizeDAG |
true | true | true |
Sources: llvm/include/llvm/CodeGen/DAGCombine.h:15-20, llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp:1846-1851
| Step | Visit order | In one sentence |
|---|---|---|
| Block order | Reverse post-order of the CFG | Predecessors are selected before successors where the CFG allows |
| Build DAG | IR instruction order inside the block | One visit per IR instruction, forward |
| DAGCombiner | Worklist, last-in-first-out, seeded in AllNodes order |
Starts at the root, descends into operands; every change re-queues the changed node's users |
| Type legalizer | Operands before users | A node is processed once all its operands are |
| Vector legalizer | Operands before users (topological order) | One sweep over the nodes |
| Operation legalizer | End of AllNodes toward the start (users before operands), repeated until nothing new is legalized |
Reaches a fixed point because legalization creates new nodes |
| Instruction selection | End of AllNodes toward the start (root first), single sweep |
Users first, so a pattern can absorb its operands |
| Scheduling | Bottom-up list scheduling from the exit node, then reversed | Source-order priority on AArch64 |
| Emission | The scheduled sequence, forward | One MachineInstr per machine node |
flowchart TB
subgraph UF["Users before operands (root first)"]
A1["DAGCombiner<br/>worklist LIFO"]
A2["SelectionDAG::Legalize<br/>sweeps, repeat to fixed point"]
A3["DoInstructionSelection<br/>single sweep"]
end
subgraph OF["Operands before users (leaves first)"]
B1["DAGTypeLegalizer::run<br/>ready-node worklist"]
B2["VectorLegalizer::Run<br/>topological order"]
end
Sources: llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp:1846-1963, llvm/lib/CodeGen/SelectionDAG/LegalizeTypes.cpp:200-487, llvm/lib/CodeGen/SelectionDAG/LegalizeVectorOps.cpp:207-247, llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp:6328-6375, llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp:1287-1392, llvm/lib/CodeGen/SelectionDAG/ScheduleDAGRRList.cpp:1580-1621
flowchart TD
P1["1 Build DAG<br/>SelectionDAGBuilder"] --> P2["2 DAG Combine 1"]
P2 --> P3["3 Type legalization<br/>then Combine 2 if changed"]
P3 --> P4["4 Vector legalization<br/>then types and Combine 3 if changed"]
P4 --> P5["5 Operation legalization"]
P5 --> P6["6 DAG Combine 4"]
P6 --> P7["7 Instruction selection<br/>DoInstructionSelection"]
P7 --> P8["8 Scheduling and emission<br/>InstrEmitter"]
P8 --> OUT["MachineInstrs in the MachineBasicBlock"]
- 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