Repository navigation
SelectionDAG 06.3 aarch64 selection
Relevant source files
The following files were used as context for generating this wiki page:
This page documents the AArch64-specific selection code in AArch64ISelDAGToDAG.cpp with real MIR and assembly: addressing modes, indexed loads, constants, bitfields, and compare-and-select. The generic machinery is on Instruction Selection.
One IR load i32 becomes one of several instructions depending on the address expression. Each form is a different ComplexPattern.
| Address in IR | Matched by | Real MIR | Assembly |
|---|---|---|---|
p + i*4, i is i64
|
ro_Xindexed32 (SelectAddrModeXRO) |
LDRWroX %0, %1, 0, 1 |
ldr w0, [x0, x1, lsl #2] |
p + sext(i32 i)*4 |
ro_Windexed32 (SelectAddrModeWRO) |
LDRWroW %0, %1, 1, 1 |
ldr w0, [x0, w1, sxtw #2] |
p + 12 |
am_indexed32 (SelectAddrModeIndexed), immediate scaled by 4 |
LDRWui %0, 3 |
ldr w0, [x0, #12] |
p - 4 |
am_unscaled32 (SelectAddrModeUnscaled), signed 9-bit byte offset |
LDURWi %0, -4 |
ldur w0, [x0, #-4] |
zext of an i8 load |
zextloadi8 pattern |
LDRBBui %0, 0 then SUBREG_TO_REG
|
ldrb w0, [x0] |
The two trailing operands of LDRWroX (0, 1) are the sign-extend flag (false: the index is a 64-bit register) and the shift flag (true: shift by the access size). SelectAddrModeXRO produces them: it requires the address to be an ISD::ADD, refuses if the add has non-memory users, looks for a shl on either operand through SelectExtendedSHL, and otherwise returns base and index with no shift. It also handles a wide constant offset by materialising it with MOVi64imm so the register-offset form saves an add.
Stores work the same way (STRWroX, STRWui, STURWi), using the Store32RO multiclass. Scaled (ui) forms need an offset that is a non-negative multiple of the access size; anything else falls to the unscaled LDUR family.
Sources: llvm/lib/Target/AArch64/AArch64ISelDAGToDAG.cpp:1490-1563, llvm/lib/Target/AArch64/AArch64ISelDAGToDAG.cpp:1277-1330, llvm/lib/Target/AArch64/AArch64InstrFormats.td:3744, llvm/lib/Target/AArch64/AArch64InstrFormats.td:3927-3949, llvm/lib/Target/AArch64/AArch64InstrFormats.td:4210-4233, llvm/lib/Target/AArch64/AArch64InstrFormats.td:4455
An indexed load is a single DAG node (LOAD with POST_INC addressing mode) with three results: value, updated base pointer, chain. The pattern language is awkward for multi-result addressing-mode choices, so AArch64DAGToDAGISel::tryIndexedLoad handles it. It reads LD->getAddressingMode() (PRE_INC or POST_INC), the memory type and extension kind, picks an opcode (LDRWpost, LDRXpre, LDRSWpost, LDRBBpost, ...), and builds the machine node, adding a SUBREG_TO_REG when a zero-extended 32-bit result must become 64 bits. Real MIR from the loop on PHI Node Resolution:
early-clobber %14:gpr64sp, %15:gpr32 = LDRWpost %0:gpr64sp(tied-def 0), 4 :: (load (s32) from %ir.lsr.iv1)
early-clobber and tied-def 0 encode that the written-back pointer must share a register with the incoming base. The DAG node itself was formed earlier by CombineToPostIndexedLoadStore, using getPostIndexedAddressParts; see DAG Combiner.
Sources: llvm/lib/Target/AArch64/AArch64ISelDAGToDAG.cpp:1801-1939, llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:31289-31350, llvm/lib/Target/AArch64/AArch64InstrInfo.td:4610
-
Constants.
ISD::Constantis matched by[(set GPR32:$dst, imm:$src)]on theMOVi32immpseudo (andMOVi64imm). The pseudo exists so the constant can be rematerialised cheaply; after register allocationAArch64ExpandPseudoInststurns it into the realmovz/movk/orrsequence. Real output forx / 10:MOVi32imm -858993459in MIR andmov w8, #52429plusmovk w8, #52428, lsl #16in the assembly. Small constants that fit an instruction's immediate field never get this far; patterns likeADDWriabsorb them directly. -
Bitfield extract.
and (srl x, 8), 255is claimed bytryBitfieldExtractOpand becomesUBFMXri %0, 8, 15, printed asubfx x0, x0, #8, #8. -
Compare and select. After legalization the DAG holds
AArch64ISD::SUBSandAArch64ISD::CSEL. Patterns match them toSUBSWrrandCSELWr. The flags value flows between them as ani32in the DAG and as the physical registerNZCVin MIR (implicit-def $nzcvon the compare,implicit $nzcvon the select). When one arm of the select is the constant1, a more specificPatproducesCSINCWr. When both arms are constants that differ by one,LowerSELECT_CCbuildsAArch64ISD::CSINCitself, before any pattern runs: the real MIR forselect i1 %c, i32 5, i32 6isCSINCWr %2, %2, 12, i.e.cinc w0, w8, le. -
Return.
RET_ReallyLRis defined asPseudo<(outs), (ins), [(AArch64retglue)]>, soAArch64ISD::RET_GLUEselects to it and it expands toretat the end.
Sources: llvm/lib/Target/AArch64/AArch64ISelDAGToDAG.cpp:3082-3108, llvm/lib/Target/AArch64/AArch64InstrInfo.td:2667-2675, llvm/lib/Target/AArch64/AArch64InstrInfo.td:3555-3562, llvm/lib/Target/AArch64/AArch64InstrInfo.td:3703, llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:12868-12912
- 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