Repository navigation
SelectionDAG 09.1 arith data
Relevant source files
The following files were used as context for generating this wiki page:
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.
| 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
| 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
- 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