Repository navigation
SelectionDAG 02 data structures
Relevant source files
The following files were used as context for generating this wiki page:
This page defines the vocabulary used by every later page: SelectionDAG, SDNode, SDValue, SDUse, value types, opcode ranges, chains, glue, uniquing and node numbering. It ends with a first DAG, (a * b) + c, printed in the format of -debug-only=isel.
classDiagram
class SelectionDAG {
AllNodes
EntryNode
Root
getNode()
AssignTopologicalOrder()
ReplaceAllUsesWith()
}
class SDNode {
NodeId
NodeType
ValueList
OperandList
}
class SDValue {
Node
ResNo
}
class SDUse {
Val
User
}
SelectionDAG "1" o-- "*" SDNode : owns
SDNode "1" *-- "*" SDUse : operands
SDUse --> SDValue : holds
SDValue --> SDNode : points to
| Type | Meaning | Key facts |
|---|---|---|
SelectionDAG |
The graph for one block | Owns every node in an intrusive list (AllNodes), keeps a FoldingSet for uniquing, has an EntryNode and a Root (the last chain) |
SDNode |
One operation | An opcode (NodeType), result types (ValueList), operands (OperandList, an array of SDUse), flags (nsw, nuw, fast-math), a debug location and a NodeId
|
SDValue |
One result of a node | A pair (SDNode*, ResNo); printed t5 for result 0 and t5:1 for result 1 |
SDUse |
One operand slot | Records the user and the used value and links into the definition's use list, so ReplaceAllUsesWith is cheap |
EVT / MVT
|
Value type |
MVT is a simple type (i32, v4i32, MVT::Other, MVT::Glue); EVT also covers extended types such as i7, i128, v3i16
|
Sources: llvm/include/llvm/CodeGen/SelectionDAG.h:231-273, llvm/include/llvm/CodeGen/SelectionDAGNodes.h:147, llvm/include/llvm/CodeGen/SelectionDAGNodes.h:279, llvm/include/llvm/CodeGen/SelectionDAGNodes.h:510, llvm/include/llvm/CodeGen/SelectionDAGNodes.h:656-663
The value of a node's opcode tells you how far the node has come in the pipeline.
| Range | Name | Example | Created by |
|---|---|---|---|
0 .. ISD::BUILTIN_OP_END-1 |
Target-independent ISD::
|
ISD::ADD, ISD::LOAD, ISD::SETCC
|
SelectionDAGBuilder, combiner, legalizer |
>= ISD::BUILTIN_OP_END |
Target-specific DAG nodes |
AArch64ISD::CSEL, AArch64ISD::ADRP, AArch64ISD::CALL
|
Target LowerOperation and PerformDAGCombine
|
negative (~MachineOpc) |
Machine nodes (already selected) | AArch64::ADDWrr |
AArch64DAGToDAGISel and the generated matcher |
SDNode::isTargetOpcode() tests NodeType >= ISD::BUILTIN_OP_END; isMachineOpcode() tests NodeType < 0 and getMachineOpcode() returns the instruction number. After selection the DAG holds only machine nodes plus a few structural kinds (CopyToReg, CopyFromReg, TokenFactor, EntryToken).
Sources: llvm/include/llvm/CodeGen/SelectionDAGNodes.h:709, llvm/include/llvm/CodeGen/SelectionDAGNodes.h:758-765, llvm/include/llvm/CodeGen/ISDOpcodes.h:1666-1668
-
Data edges carry values such as
i32orv4f32. Independent data-only nodes have no order between them. -
Chain edges have type
MVT::Other, printedch. They serialise anything with a side effect (loads, stores, calls,CopyToReg, barriers). A node takes a chain in and produces a chain out; independent chains are merged withISD::TokenFactor. The chain starts atISD::EntryTokenand ends at the DAG'sRoot. -
Glue edges have type
MVT::Glue, printedglue. Glue says the producer and consumer must be scheduled back to back. It is used where hardware has an implicit register dependency, such as theCopyToRegof outgoing arguments right before a call.
AArch64 models condition flags as an explicit i32 value instead of glue: AArch64ISD::SUBS returns (i32, i32) where result 1 is NZCV.
A load shows all three ideas: t9: i32,ch = load<(load (s32) from %ir.p)> t0, t4, undef:i64 has results (value, chain) and operands (chain, base pointer, offset); the trailing <...> is its MachineMemOperand.
Sources: llvm/include/llvm/CodeGen/ISDOpcodes.h:48-53, llvm/include/llvm/CodeGen/ISDOpcodes.h:224-230
SelectionDAG::getNode builds a FoldingSetNodeID from opcode, result types, operands and flags and looks it up. If an identical node exists, it is returned instead of creating a new one, so the same add t2, t4 built twice is one node with two users. Nodes whose last result is MVT::Glue are not uniqued, and loads and stores are only equal if their chain operand is equal.
Sources: llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp:1018-1043, llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp:7458
| Dump text | Opcode | Meaning |
|---|---|---|
t0: ch,glue = EntryToken |
ISD::EntryToken |
Start of the chain |
t2: i32,ch = CopyFromReg t0, Register:i32 %0 |
ISD::CopyFromReg |
Read a virtual or physical register: how arguments and cross-block values enter a block's DAG |
t9: ch,glue = CopyToReg t0, Register:i32 $w0, t7 |
ISD::CopyToReg |
Write a register: return values, outgoing arguments and live-out values leave this way |
Constant:i32<3> |
ISD::Constant |
A constant that combine and legalization may change |
TargetConstant:i32<3> |
ISD::TargetConstant |
A final immediate; selection places it in the instruction as is |
GlobalAddress:i64<ptr @g> 0 |
ISD::GlobalAddress |
Address of a symbol; the target lowers it (ADRP + ADD :lo12:) |
FrameIndex:i64<0> |
ISD::FrameIndex |
Address of a stack slot |
t6: ch = TokenFactor t3:1, t5:1 |
ISD::TokenFactor |
Joins independent chains |
Every node has an integer NodeId. SelectionDAG::AssignTopologicalOrder() runs a Kahn-style sort and renumbers AllNodes so every node appears after all its operands (EntryToken first, Root last). Instruction selection uses this order backwards, and the node ids double as a cycle-safety check during folding. See Pipeline, Phases and Traversal Orders.
Sources: llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp:13469-13551
For i32 madd(i32 a, i32 b, i32 c) the initial DAG is shown below. The text dump follows the -debug-only=isel format; node numbers are illustrative.
t0: ch,glue = EntryToken
t2: i32,ch = CopyFromReg t0, Register:i32 %0 ; a (live-in $w0 copied to vreg %0)
t4: i32,ch = CopyFromReg t0, Register:i32 %1 ; b
t6: i32,ch = CopyFromReg t0, Register:i32 %2 ; c
t7: i32 = mul t2, t4
t8: i32 = add t7, t6
t10: ch,glue = CopyToReg t0, Register:i32 $w0, t8 ; return value into $w0
t11: ch = AArch64ISD::RET_GLUE t10, Register:i32 $w0, t10:1
flowchart LR
n0["t0 EntryToken"]
n2["t2 CopyFromReg %0"]
n4["t4 CopyFromReg %1"]
n6["t6 CopyFromReg %2"]
n7["t7 mul"]
n8["t8 add"]
n10["t10 CopyToReg $w0"]
n11["t11 RET_GLUE"]
n0 -.-> n2
n0 -.-> n4
n0 -.-> n6
n2 --> n7
n4 --> n7
n7 --> n8
n6 --> n8
n8 --> n10
n0 -.-> n10
n10 -.-> n11
Solid arrows are data edges and dashed arrows are chain edges. The chain t0 -> t10 -> t11 is the only ordering; the glue operand t10:1 makes the return sit directly after the copy into $w0.
- 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