Skip to content

SelectionDAG 06.3 aarch64 selection

Mugundan edited this page Nov 27, 2026 · 1 revision

AArch64 Instruction Selection

Relevant source files

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

AArch64 combines three mechanisms: generated pattern matching, `ComplexPattern` C++ functions for addressing modes and shifted operands, and hand-written `tryXxx` selectors for cases the pattern language handles poorly.

Purpose and Scope

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.

Addressing Modes for Loads and Stores

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

Pre- and Post-Indexed Loads

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, Bitfields and Compare-and-Select

  • Constants. ISD::Constant is matched by [(set GPR32:$dst, imm:$src)] on the MOVi32imm pseudo (and MOVi64imm). The pseudo exists so the constant can be rematerialised cheaply; after register allocation AArch64ExpandPseudoInsts turns it into the real movz / movk / orr sequence. Real output for x / 10: MOVi32imm -858993459 in MIR and mov w8, #52429 plus movk w8, #52428, lsl #16 in the assembly. Small constants that fit an instruction's immediate field never get this far; patterns like ADDWri absorb them directly.
  • Bitfield extract. and (srl x, 8), 255 is claimed by tryBitfieldExtractOp and becomes UBFMXri %0, 8, 15, printed as ubfx x0, x0, #8, #8.
  • Compare and select. After legalization the DAG holds AArch64ISD::SUBS and AArch64ISD::CSEL. Patterns match them to SUBSWrr and CSELWr. The flags value flows between them as an i32 in the DAG and as the physical register NZCV in MIR (implicit-def $nzcv on the compare, implicit $nzcv on the select). When one arm of the select is the constant 1, a more specific Pat produces CSINCWr. When both arms are constants that differ by one, LowerSELECT_CC builds AArch64ISD::CSINC itself, before any pattern runs: the real MIR for select i1 %c, i32 5, i32 6 is CSINCWr %2, %2, 12, i.e. cinc w0, w8, le.
  • Return. RET_ReallyLR is defined as Pseudo<(outs), (ins), [(AArch64retglue)]>, so AArch64ISD::RET_GLUE selects to it and it expands to ret at 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

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