Repository navigation
SelectionDAG 06.1 pattern language
Relevant source files
The following files were used as context for generating this wiki page:
- llvm/include/llvm/Target/TargetSelectionDAG.td
- llvm/lib/Target/AArch64/AArch64InstrInfo.td
- llvm/lib/Target/AArch64/AArch64InstrFormats.td
- llvm/utils/TableGen/DAGISelEmitter.cpp
- llvm/utils/TableGen/DAGISelMatcherGen.cpp
- llvm/utils/TableGen/DAGISelMatcherOpt.cpp
- llvm/utils/TableGen/DAGISelMatcherEmitter.cpp
- llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp
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.
| 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
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"]
-
CodeGenDAGPatternsparses 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) plusAddedComplexity. -
DAGISelEmitterputs all patterns in a list and runsstable_sortwithPatternSortingPredicate: larger and more specific patterns first, ties keep source order. That order is the match priority. -
DAGISelMatcherGenconverts each pattern into a linear matcher: checks and captures followed by emit steps. -
OptimizeMatcherrunsContractNodes(fuse common step pairs, such as moving to a child and recording it) andFactorNodes, which merges patterns that start the same way into shared prefixes withSwitchOpcodeandSwitchTypebranches. The result is a decision tree rather than a list tried one by one. -
DAGISelMatcherEmitterwrites auint8_ttable inAArch64GenDAGISel.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. -
SelectCode()passes the table toSelectCodeCommon.
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
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
- 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