Repository navigation
Calling Conventions 10 aarch64 walkthroughs
Relevant source files
The following files were used as context for generating this wiki page:
Concrete evidence for each rule: incoming arguments, an outgoing call with stack arguments, mixed integer and floating-point arguments, and a tail call. Varargs, sret and byval output is on Varargs, byval, sret.
define i64 @many(i64 %a0, i64 %a1, i64 %a2, i64 %a3, i64 %a4, i64 %a5, i64 %a6, i64 %a7, i64 %a8, i64 %a9) {
%s = add i64 %a0, %a9
%t = add i64 %s, %a8
ret i64 %t
}many: // @many
.cfi_startproc
ldp x9, x8, [sp]
add x8, x0, x8
add x0, x8, x9
ret| Argument | Rule | Location |
|---|---|---|
%a0–%a7
|
CCIfType<[i64], CCAssignToReg<[X0, ..., X7]>> |
x0–x7
|
%a8 |
CCIfType<[i64, ...], CCAssignToStack<8, 8>> |
[sp] (offset 0) |
%a9 |
same | [sp, #8] |
LowerFormalArguments made two fixed frame objects at offsets 0 and 8 and loaded them; the load/store optimizer later merged the two loads into one ldp. The function has no frame of its own, so the fixed objects are addressed directly from sp at entry.
Sources: llvm/lib/Target/AArch64/AArch64CallingConvention.td:106, llvm/lib/Target/AArch64/AArch64CallingConvention.td:119-120, llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:9487-9552
declare i64 @callee(i64, i64, i64, i64, i64, i64, i64, i64, i64, i64)
define i64 @caller(i64 %x) {
%r = call i64 @callee(i64 %x, i64 1, i64 2, i64 3, i64 4, i64 5, i64 6, i64 7, i64 8, i64 9)
%s = add i64 %r, 1
ret i64 %s
}MIR after selection (abridged in the middle):
%0:gpr64 = COPY $x0
ADJCALLSTACKDOWN 16, 0, implicit-def dead $sp, implicit $sp
%1:gpr64sp = COPY $sp
%2:gpr32 = MOVi32imm 9
%3:gpr64 = SUBREG_TO_REG killed %2, %subreg.sub_32
STRXui killed %3, %1, 1 :: (store (s64) into stack + 8)
%4:gpr32 = MOVi32imm 8
%5:gpr64 = SUBREG_TO_REG killed %4, %subreg.sub_32
STRXui killed %5, %1, 0 :: (store (s64) into stack)
...
$x0 = COPY %0
$x1 = COPY %7
...
$x7 = COPY %19
BL @callee, csr_aarch64_aapcs, implicit-def dead $lr, implicit $sp, implicit $x0, implicit $x1, implicit $x2, implicit $x3, implicit $x4, implicit $x5, implicit $x6, implicit $x7, implicit-def $sp, implicit-def $x0
ADJCALLSTACKUP 16, 0, implicit-def dead $sp, implicit $sp
%20:gpr64sp = COPY $x0
%21:gpr64sp = ADDXri %20, 1, 0
$x0 = COPY %21
RET_ReallyLR implicit $x0
| MIR | Comes from |
|---|---|
ADJCALLSTACKDOWN 16 / ADJCALLSTACKUP 16
|
CALLSEQ_START / CALLSEQ_END with CCInfo.getStackSize() = 16 |
Two STRXui to stack and stack + 8
|
Stack locations for arguments 9 and 10, MemOpChains
|
$x0 = COPY ... ... $x7 = COPY ...
|
RegsToPass → glued CopyToReg
|
csr_aarch64_aapcs on BL
|
Register mask from getCallPreservedMask
|
implicit $x0 ... implicit $x7
|
Argument register operands on the call node |
implicit-def $x0 |
The result register, from RetCC_AArch64_AAPCS
|
%20 = COPY $x0 |
LowerCallResult |
Final assembly:
caller: // @caller
.cfi_startproc
sub sp, sp, #32
.cfi_def_cfa_offset 32
.cfi_offset w30, -16
mov w8, #9 // =0x9
mov w9, #8 // =0x8
mov w1, #1 // =0x1
mov w2, #2 // =0x2
mov w3, #3 // =0x3
mov w4, #4 // =0x4
mov w5, #5 // =0x5
mov w6, #6 // =0x6
mov w7, #7 // =0x7
stp x8, x30, [sp, #8] // 8-byte Folded Spill
str x9, [sp]
bl callee
ldr x30, [sp, #16] // 8-byte Reload
add x0, x0, #1
add sp, sp, #32
retThe 16-byte outgoing area sits at the bottom of the 32-byte frame (AArch64 reserves the call frame, so ADJCALLSTACKDOWN/UP became no SP change). Frame lowering put the saved x30 at [sp, #16], right after argument 10 at [sp, #8], and the store of argument 10 merged with the x30 save into one stp. x0 (%x) needed no copy because it was already in place.
Sources: llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:10535-10541, llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:11063-11129
define double @mixed(i32 %i, double %d, i8 %c, float %f) {
%e = sext i8 %c to i32
%x = add i32 %i, %e
%y = sitofp i32 %x to double
%fe = fpext float %f to double
%z = fadd double %y, %d
%w = fadd double %z, %fe
ret double %w
}mixed: // @mixed
.cfi_startproc
add w8, w0, w1, sxtb
fcvt d1, s1
scvtf d2, w8
fadd d0, d2, d0
fadd d0, d0, d1
ret| Argument | Rule | Location |
|---|---|---|
i32 %i |
CCIfType<[i32], CCAssignToReg<[W0, ...]>> |
w0 |
double %d |
CCIfType<[f64], CCAssignToReg<[D0, ...]>> |
d0 |
i8 %c |
CCPromoteToType<i32>, then the i32 rule |
w1 |
float %f |
CCIfType<[f32], CCAssignToReg<[S0, ...]>> |
s1 |
return double
|
RetCC_AArch64_AAPCS f64 rule |
d0 |
Integer and FP registers are allocated independently, so %c takes w1 even though %d came between. S1 and D0 are different registers from CCState's point of view (only aliases are marked used), so %f gets s1.
Sources: llvm/lib/Target/AArch64/AArch64CallingConvention.td:97-110, llvm/lib/Target/AArch64/AArch64CallingConvention.td:150
%0:gpr64 = COPY $x0
$x0 = COPY %0
TCRETURNdi @g, 0, csr_aarch64_aapcs, implicit $sp, implicit $x0
sib: // @sib
.cfi_startproc
b gNo ADJCALLSTACKDOWN, no result copy, no RET_ReallyLR: LowerCall returned the TC_RETURN node directly, which ends the block. See Tail Calls for the eligibility checks, and the frame wiki's walkthroughs for how the epilogue is placed before such jumps.
Sources: llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:10516-10541, llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:11091-11105
llc -O2 -mtriple=aarch64-linux-gnu args.ll -o -
llc -O2 -mtriple=aarch64-linux-gnu -stop-after=finalize-isel call.ll -o -
The SelectionDAG wiki's AArch64 walkthroughs show the same kind of trace for instruction selection, and the MC wiki's streamers page shows how the final bl and b become encoded instructions with relocations.
- 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