Skip to content

SelectionDAG 01.1 pipeline

Mugundan edited this page Nov 27, 2026 · 1 revision

Pipeline, Phases and Traversal Orders

Relevant source files

The following files were used as context for generating this wiki page:

This page gives the exact order of the phases in `SelectionDAGISel::CodeGenAndEmitDAG` and the order in which each phase visits the nodes of the DAG.

Purpose and Scope

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.

Phase Order in CodeGenAndEmitDAG

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()
Loading

Notes taken from the source:

  • Combine runs 2 and 3 are conditional: Combine(AfterLegalizeTypes) runs if (Changed) after LegalizeTypes, and the vector-legalized sequence runs only if LegalizeVectors() 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

Combine Levels

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

Traversal Orders at a Glance

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
Loading

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

Pipeline Diagram

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"]
Loading

Start here

Computer Organization & Architecture

Hexagon ISA

Operating Systems

C++ for LLVM Development

LLVM IR

SelectionDAG

Backend Assignments

Target Bring-up

Calling Conventions

Frame Lowering

MC Layer

Register Allocation

Scheduling

Clone this wiki locally