Skip to content

SelectionDAG 06.1 pattern language

Mugundan edited this page Nov 27, 2026 · 1 revision

TableGen Pattern Language

Relevant source files

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

A pattern pairs a source tree over DAG nodes with a result tree over machine instructions. TableGen turns thousands of such patterns into one generated decision procedure that `SelectCode` runs for every node.

Purpose and Scope

This page documents the pattern vocabulary defined in TargetSelectionDAG.td and used by AArch64InstrInfo.td, and the build-time pipeline (llvm-tblgen -gen-dag-isel) that produces AArch64GenDAGISel.inc. The run-time side is on Matcher Interpreter and Folding.

Pattern Vocabulary

Construct What it is AArch64 example
SDNode Names a DAG opcode and its operand/result type profile def AArch64csel : SDNode<"AArch64ISD::CSEL", SDT_AArch64CSel>; and def add : SDNode<"ISD::ADD", SDTIntBinOp, ...>;
Operand leaf with class A value of a register class; binds a name GPR32:$Rn
Immediate leaf A constant, optionally constrained by a predicate imm, imm0_63, simm9
Pat<src, dst> A standalone pattern def : Pat<(i32 (mul GPR32:$Rn, GPR32:$Rm)), (MADDWrrr GPR32:$Rn, GPR32:$Rm, WZR)>;
Pattern inside an instruction The instruction lists what it computes and TableGen derives the pattern def MOVi32imm : Pseudo<(outs GPR32:$dst), (ins i32imm:$src), [(set GPR32:$dst, imm:$src)]>;
PatFrag / PatFrags A named reusable sub-pattern with a C++ predicate load, sextloadi8, zextloadi8
ComplexPattern Delegates matching of one operand to a C++ function that may produce several output operands ro_Xindexed32 calls SelectAddrModeXRO<32>; arith_shifted_reg32 calls SelectArithShiftedRegister
SDNodeXForm A C++ snippet that transforms a matched constant before use inv_cond_XFORM inverts a condition code
Predicates = [...] Subtarget feature test checked at match time [HasCSSC], [HasNEON]
AddedComplexity Priority bonus let AddedComplexity = 5 in { ... MADD patterns }; register-offset loads use 10

Two real definitions, shortened. First, register-offset loads from AArch64InstrFormats.td; second, a conditional-select pattern from AArch64InstrInfo.td:

let AddedComplexity = 10 in
def roX : LoadStore32RO<sz, V, opc, asm, (outs regtype:$Rt),
               (ins GPR64sp:$Rn, GPR64:$Rm, ro_Xextend32:$extend),
               [(set (Ty regtype:$Rt),
                     (loadop (ro_Xindexed32 GPR64sp:$Rn, GPR64:$Rm,
                                            ro_Xextend32:$extend)))]>;

def : Pat<(AArch64csel GPR32:$tval, (i32 1), (i32 imm:$cc), NZCV),
          (CSINCWr GPR32:$tval, WZR, (i32 imm:$cc))>;

The first says a load whose address SelectAddrModeXRO accepts becomes the LDR ... [Xn, Xm, extend] form, with $Rn, $Rm and the extend/shift flags as operands. The second says a csel whose false value is the constant 1 is better done as csinc with the zero register.

Sources: llvm/include/llvm/Target/TargetSelectionDAG.td:368, llvm/include/llvm/Target/TargetSelectionDAG.td:1000, llvm/include/llvm/Target/TargetSelectionDAG.td:1042, llvm/include/llvm/Target/TargetSelectionDAG.td:1195, llvm/include/llvm/Target/TargetSelectionDAG.td:2460-2468, llvm/lib/Target/AArch64/AArch64InstrInfo.td:851, llvm/lib/Target/AArch64/AArch64InstrInfo.td:2667-2675, llvm/lib/Target/AArch64/AArch64InstrInfo.td:2911-2921, llvm/lib/Target/AArch64/AArch64InstrInfo.td:3555-3562, llvm/lib/Target/AArch64/AArch64InstrFormats.td:3927-3949, llvm/lib/Target/AArch64/AArch64InstrFormats.td:4210-4233

From Patterns to a Matcher Table

flowchart LR
  PAT["Pat and instruction<br/>definitions in .td files"] --> CGP["CodeGenDAGPatterns<br/>parse, infer types, complexity"]
  CGP --> SORT["DAGISelEmitter<br/>stable_sort by PatternSortingPredicate"]
  SORT --> GEN["DAGISelMatcherGen<br/>one linear matcher per pattern"]
  GEN --> OPT["OptimizeMatcher<br/>ContractNodes, FactorNodes"]
  OPT --> EMIT["DAGISelMatcherEmitter<br/>uint8_t MatcherTable"]
  EMIT --> INC["AArch64GenDAGISel.inc"]
  INC --> SC["SelectCode calls SelectCodeCommon"]
Loading
  1. CodeGenDAGPatterns parses every pattern, infers types, and gives each pattern a complexity: pattern size (roughly the number of nodes it covers, with bonuses for constants and predicates) plus AddedComplexity.
  2. DAGISelEmitter puts all patterns in a list and runs stable_sort with PatternSortingPredicate: larger and more specific patterns first, ties keep source order. That order is the match priority.
  3. DAGISelMatcherGen converts each pattern into a linear matcher: checks and captures followed by emit steps.
  4. OptimizeMatcher runs ContractNodes (fuse common step pairs, such as moving to a child and recording it) and FactorNodes, which merges patterns that start the same way into shared prefixes with SwitchOpcode and SwitchType branches. The result is a decision tree rather than a list tried one by one.
  5. DAGISelMatcherEmitter writes a uint8_t table in AArch64GenDAGISel.inc; its comments show each pattern as // Src: and // Dst: lines next to the matcher bytes, which is the easiest way to see where a pattern landed.
  6. SelectCode() passes the table to SelectCodeCommon.

Sources: llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp:1463-1511, llvm/utils/TableGen/DAGISelEmitter.cpp:84-149, llvm/utils/TableGen/DAGISelEmitter.cpp:184-192, llvm/utils/TableGen/DAGISelMatcherOpt.cpp:25-304, llvm/utils/TableGen/DAGISelMatcherOpt.cpp:353-662, llvm/utils/TableGen/DAGISelMatcherOpt.cpp:664-667

Build-Time Notes for AArch64

In the current tree the AArch64ISD:: node enum is generated into AArch64GenSDNodeInfo.inc from the SDNode<"AArch64ISD::..."> records, and AArch64SelectionDAGInfo derives from the generated SelectionDAGGenTargetInfo. Older releases declare the enum by hand in AArch64ISelLowering.h. Adding a new AArch64ISD:: node therefore starts with a record in the .td files.

Sources: llvm/lib/Target/AArch64/AArch64SelectionDAGInfo.h:10-30

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