Skip to content

SelectionDAG 09.2 memory

Mugundan edited this page Nov 27, 2026 · 1 revision

Memory Instructions

Relevant source files

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

Loads and stores are `ISD::LOAD` and `ISD::STORE` nodes with a chain. The initial DAG builds the address as plain `ADD`, `MUL` and `SHL` nodes, the combiner shapes it, and then `ComplexPattern`s choose an addressing mode.

Purpose and Scope

This page lists the AArch64 memory instructions that selection produces and the ordering guarantees the chain provides. The addressing-mode matchers are explained on AArch64 Instruction Selection; chain construction on DAG Construction.

Load and Store Forms

IR MIR Assembly
load i32, ptr (gep i32, p, i64 %i) LDRWroX %0, %1, 0, 1 ldr w0, [x0, x1, lsl #2]
store i32 %v, ptr (gep i32, p, i64 %i) STRWroX %2, %0, %1, 0, 1 str w2, [x0, x1, lsl #2]
load i32, ptr (gep i32, p, i64 3) LDRWui %0, 3 ldr w0, [x0, #12]
load i32, ptr (gep i32, p, i64 -1) LDURWi %0, -4 ldur w0, [x0, #-4]
load i32, ptr (gep i32, p, sext i32 %i) LDRWroW %0, %1, 1, 1 ldr w0, [x0, w1, sxtw #2]
loop load plus pointer bump by 4 LDRWpost %0, 4 (via tryIndexedLoad) ldr w9, [x0], #4
two loads at p and p+8 LDRXui %0, 0 and LDRXui %0, 1 ldp x8, x9, [x0]
two stores at p and p+8 STRXui %1, %0, 0 and STRXui %2, %0, 1 stp x1, x2, [x0]

Two points worth noting. Scaled (ui) forms need an offset that is a non-negative multiple of the access size; anything else (the -4 above) falls to the unscaled LDUR family, which is a different ComplexPattern. And ldp and stp do not come from SelectionDAG: the last two rows show two independent instructions after selection, and AArch64LoadStoreOptimizer pairs them later, after register allocation.

Sources: llvm/lib/Target/AArch64/AArch64ISelDAGToDAG.cpp:1490-1563, llvm/lib/Target/AArch64/AArch64ISelDAGToDAG.cpp:1801-1939, llvm/lib/Target/AArch64/AArch64InstrFormats.td:4210-4233, llvm/lib/Target/AArch64/AArch64InstrInfo.td:4610

How the Chain Keeps Memory Ordered

Chain edges make memory ordering safe. For store p, 1; x = load p; store p, 2 the DAG gives the load a chain input from the first store and the second store a chain input that depends on the load, so no later phase can reorder them, whatever it does with the data edges.

flowchart TD
  E["EntryToken"] --> S1["store p, 1"]
  S1 -- chain --> L["load p"]
  L -- chain --> S2["store p, 2"]
  S2 -- chain --> R["Root"]
  L -- value --> U["users of x"]
Loading

Independent loads, by contrast, are collected in PendingLoads while building and are not ordered against each other (see DAG Construction), so the DAG does not constrain their relative order; pairing into ldp happens later, after selection.

Sources: llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp:4920-4993, llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp:1206-1229

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