Skip to content

SelectionDAG 09.1 arith data

Mugundan edited this page Nov 27, 2026 · 1 revision

Arithmetic and Data Movement

Relevant source files

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

Arithmetic work happens mostly in the combiner and legalizer (what to compute); selection is mostly a lookup (which instruction does it). Constants are selected as rematerialisable pseudos and expanded after register allocation.

Purpose and Scope

This page lists real AArch64 results for arithmetic, floating point, vectors, constants, copies, extensions and global addresses, and points to the page that explains each step.

Arithmetic Instructions

IR What the DAG phases did MIR after instruction selection Final assembly
add i8 %a, %b Type legalization promoted i8 to i32 (Type Legalization) ADDWrr %0, %1 add w0, w0, w1
%m = mul i32 %a, %b then add i32 %m, %c Selected as plain multiply (a MADD with $wzr) plus add; fused later by the MachineCombiner (Instruction Selection) %3 = MADDWrrr %0, %1, $wzr and %4 = ADDWrr killed %3, %2 madd w0, w0, w1, w2
shl i32 %b, 3 then add i32 %a, %s arith_shifted_reg32 swallowed the shl (Matcher Interpreter and Folding) ADDWrs %0, %1, 3 add w0, w0, w1, lsl #3
mul i32 %x, 9 Combine: x*9 becomes add (shl x,3), x (DAG Combiner), then the same ADDWrs pattern ADDWrs %0, %0, 3 add w0, w0, w0, lsl #3
udiv i32 %x, 10 Combine: magic-number multiply; constant materialised with MOVi32imm MOVi32imm -858993459, UMADDLrrr %0, killed %1, $xzr, UBFMXri killed %2, 35, 63 mov w8, #52429 movk w8, #52428, lsl #16 umull x8, w0, w8 lsr x0, x8, #35
add i128 %a, %b Integer expansion into low and high i64 halves with a carry ADDSXrr %0, %2, implicit-def $nzcv and ADCXr %1, %3, implicit $nzcv adds x0, x0, x2 adc x1, x1, x3
lshr i64 %x, 8 then and ..., 255 tryBitfieldExtractOp (AArch64 Instruction Selection) UBFMXri %0, 8, 15 ubfx x0, x0, #8, #8
sext i32 %a to i64 then add i64 %e, %b The sign extension is folded into the add's extended-register form ADDXrx %1, %0, 48 add x0, x1, w0, sxtw
fmul then fadd on float No fast-math flags, so no fusion into fmadd; both keep nofpexcept and implicit $fpcr nofpexcept FMULSrr %0, %1, implicit $fpcr and nofpexcept FADDSrr killed %3, %2, implicit $fpcr fmul s0, s0, s1 fadd s0, s0, s2
add <4 x i32> Legal type and operation ADDv4i32 %0, %1 add v0.4s, v0.4s, v1.4s
add <8 x i32> Type legalization split it into two <4 x i32> operations ADDv4i32 %0, %2 and ADDv4i32 %1, %3 add v1.4s, v1.4s, v3.4s add v0.4s, v0.4s, v2.4s

Sources: llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:21202-21215, llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:21266-21306, llvm/lib/Target/AArch64/AArch64InstrInfo.td:2911-2921, llvm/lib/Target/AArch64/AArch64ISelDAGToDAG.cpp:3017-3039

Data Movement: Constants, Copies, Extensions, Addresses

IR Mechanism MIR Final assembly
ret i32 4096 ISD::Constant matched by the imm pattern on pseudo MOVi32imm MOVi32imm 4096 mov w0, #4096
ret i32 -65536 same MOVi32imm -65536 mov w0, #-65536 (0xffff0000)
ret i32 1431655765 same; 0x55555555 is a valid logical immediate MOVi32imm 1431655765 mov w0, #1431655765
ret i64 1311768467463790320 same, 64-bit pseudo MOVi64imm 1311768467463790320 mov x0, #57072 movk x0, #39612, lsl #16 movk x0, #22136, lsl #32 movk x0, #4660, lsl #48
function argument i32 %a LowerFormalArguments makes a live-in vreg and CopyFromReg liveins: $w0 and %0:gpr32 = COPY $w0 no instruction
ret i32 %x CopyToReg $w0, then AArch64ISD::RET_GLUE $w0 = COPY %4 and RET_ReallyLR implicit $w0 ret
load i8 then zext to i64 Combine folds into a zero-extending load; 32-bit load widened with SUBREG_TO_REG %1:gpr32 = LDRBBui %0, 0 and %2:gpr64all = SUBREG_TO_REG killed %1, %subreg.sub_32 ldrb w0, [x0]
load i32, ptr @g (non-preemptible, small code model) GlobalAddress lowered to ADRP plus ADDlow, low part folded into the load ADRP target-flags(aarch64-page) @g and LDRWui killed %0, target-flags(aarch64-pageoff, aarch64-nc) @g adrp x8, g ldr w0, [x8, :lo12:g]
ret ptr @g (same) MOVaddr pseudo for the pair MOVaddr target-flags(aarch64-page) @g, target-flags(aarch64-pageoff, aarch64-nc) @g adrp x0, g add x0, x0, :lo12:g
load i32, ptr @g (preemptible, GOT) LOADgot pseudo LOADgot target-flags(aarch64-got) @g and LDRWui killed %0, 0 adrp x8, :got:g ldr x8, [x8, :got_lo12:g] ldr w0, [x8]

Constants never become mov instructions inside SelectionDAG: it selects a rematerialisable pseudo, and AArch64ExpandPseudoInsts picks the cheapest real sequence after register allocation. That is why the MIR column is identical for four very different assembly sequences.

Sources: llvm/lib/Target/AArch64/AArch64InstrInfo.td:2667-2675, llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:9059-9520, llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:11187-11218, llvm/lib/Target/AArch64/AArch64InstrInfo.td:3703

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