Repository navigation
Calling Conventions 02 cc td
Relevant source files
The following files were used as context for generating this wiki page:
The action classes in TargetCallingConv.td, how AArch64 combines them, the Entry flag, CCDelegateTo, CCCustom, and CalleeSavedRegs. How the generated function is driven is on CCState and CCValAssign.
flowchart TD
V["value: ValVT, LocVT, ArgFlags"] --> A1{"action 1 applies?"}
A1 -->|"no"| A2{"action 2 applies?"}
A1 -->|"yes: changes type (Promote/BitConvert)"| A2
A1 -->|"yes: assigns location"| DONE["CCValAssign added, return false"]
A2 --> AN["... action N"]
AN -->|"nothing assigned"| FAIL["return true (unhandled)"]
Type-changing actions (CCPromoteToType, CCBitConvertToType) do not end the search: they change LocVT and LocInfo, and evaluation continues with the next action using the new type. That is why AArch64 can write CCIfType<[i1, i8, i16], CCPromoteToType<i32>> once and then handle all of them with the i32 rules.
| Class | Line | Effect |
|---|---|---|
CCCustom<"fn"> |
18 | Call a hand-written C++ function |
CCIfType<[types], A> |
30 | Apply A if LocVT is in the list |
CCIf<"cond", A> |
35 | Apply A if the C++ expression is true |
CCIfByVal<A> |
41 | Argument has the byval attribute |
CCIfSwiftSelf, CCIfSwiftAsync, CCIfSwiftError
|
51-61 | Swift ABI attributes |
CCIfConsecutiveRegs<A> |
71 | Part of an aggregate split into consecutive registers (HFAs) |
CCIfCC<"CC", A> |
75 | Current calling convention is CC
|
CCIfInReg, CCIfNest, CCIfSplit, CCIfSRet
|
80-92 | Argument flags |
CCIfVarArg, CCIfNotVarArg
|
95-98 | Function or call is variadic |
CCIfArgFixed, CCIfArgVarArg
|
101-104 | This particular argument is a fixed or variadic one |
CCAssignToReg<[regs]> |
118 | First free register in the list |
CCAssignToRegWithShadow<[regs], [shadows]> |
131 | Same, but also marks the matching shadow register used |
CCAssignToStack<size, align> |
141 | Next stack slot (size 0 means "size of the type") |
CCAssignToStackWithShadow<size, align, [regs]> |
149 | Stack slot, and mark registers used |
CCPassByVal<size, align> |
169 | Copy a byval aggregate to the stack |
CCPromoteToType<type> |
176 | Extend to a wider type (sign/zero/any from the flags) |
CCBitConvertToType<type> |
188 | Reinterpret as another type of the same size |
CCPassIndirect<type> |
200 | Pass a pointer to a copy |
CCDelegateTo<CC> |
206 | Run another CallingConv's rules |
CallingConv<[actions]> |
212 | The record itself |
CalleeSavedRegs<(add ...)> |
235 | Registers a callee must preserve |
Sources: llvm/include/llvm/Target/TargetCallingConv.td:14-235
AArch64 keeps the common rules in a defvar list and builds several conventions from it. The order matters:
- Special-purpose arguments first:
nestinX15,sretinX8,byvalto the stack, Swift registers, HFA blocks viaCCCustom. - Type normalization: pointers as
i64, some vectors bit-converted,i1/i8/i16promoted toi32. - Registers:
W0–W7/X0–X7for integers,H/S/D/Q0–7 for floating point and vectors,Z0–Z7andP0–P3for SVE. - Stack, when the registers run out.
// In AAPCS, an SRet is passed in X8, not X0 like a normal pointer parameter.
...
CCIfSRet<CCIfType<[i64], CCAssignToReg<[X8]>>>,
// Put ByVal arguments directly on the stack. Minimum size and alignment of a
// slot is 64-bit.
CCIfByVal<CCPassByVal<8, 8>>,
...
CCIfConsecutiveRegs<CCCustom<"CC_AArch64_Custom_Block">>,
...
CCIfType<[i1, i8, i16], CCPromoteToType<i32>>,
CCIfType<[i32], CCAssignToReg<[W0, W1, W2, W3, W4, W5, W6, W7]>>,
// i128 is split to two i64s, we can't fit half to register X7.
CCIfType<[i64], CCIfSplit<CCAssignToRegWithShadow<[X0, X2, X4, X6],
[X0, X1, X3, X5]>>>,
...
CCIfType<[i64], CCAssignToReg<[X0, X1, X2, X3, X4, X5, X6, X7]>>,
...
CCIfType<[f64], CCAssignToReg<[D0, D1, D2, D3, D4, D5, D6, D7]>>,
...
// If more than will fit in registers, pass them on the stack instead.
CCIfType<[i1, i8, i16, f16, bf16], CCAssignToStack<8, 8>>,
CCIfType<[i32, f32], CCAssignToStack<8, 8>>,
The i128 rule shows CCAssignToRegWithShadow: the first half of an i128 must go in an even register (X0, X2, ...), and assigning X2 also uses up X1, so the pair never starts at an odd register.
Integer and floating-point registers are counted separately, because CCAssignToReg only looks at the listed registers. A function (i32, double, i8, float) gets w0, d0, w1, s1; see the walkthroughs.
Sources: llvm/lib/Target/AArch64/AArch64CallingConvention.td:30-123
let Entry = 1 in
def CC_AArch64_AAPCS : CallingConv<AArch64_Common>;
let Entry = 1 in
def RetCC_AArch64_AAPCS : CallingConv<[
...
Entry = 1 makes -gen-callingconv emit a non-static function that C++ can name. Conventions without it are only usable through CCDelegateTo from other records. The generated functions have the CCAssignFn signature and are included into AArch64ISelLowering.cpp (and the GlobalISel call lowering) from AArch64GenCallingConv.inc.
Sources: llvm/lib/Target/AArch64/AArch64CallingConvention.td:125-162, llvm/lib/Target/AArch64/CMakeLists.txt:8
When a rule cannot be written in TableGen, CCCustom<"Name"> calls a C++ function with the CCCustomFn-like signature (ValNo, ValVT, LocVT, LocInfo, ArgFlags, State) returning true if it handled the value. AArch64 uses CC_AArch64_Custom_Block for homogeneous floating-point aggregates, which must go in consecutive registers or entirely on the stack; it collects the pieces in State.getPendingLocs() until the last one arrives, then allocates them together.
| Function | Line |
|---|---|
finishStackBlock |
43 |
CC_AArch64_Custom_Stack_Block |
116 |
CC_AArch64_Custom_Block |
135 |
Sources: llvm/lib/Target/AArch64/AArch64CallingConvention.cpp:43-135
def CSR_AArch64_AAPCS : CalleeSavedRegs<(add X19, X20, X21, X22, X23, X24,
...
def CSR_AArch64_AAPCS_ThisReturn : CalleeSavedRegs<(add CSR_AArch64_AAPCS, X0)>;
...
def CSR_AArch64_NoRegs : CalleeSavedRegs<(add)>;
-gen-register-info produces <Name>_SaveList (a null-terminated list, for getCalleeSavedRegs) and <Name>_RegMask (a bit mask, for getCallPreservedMask). Variants are built with add: ThisReturn adds X0 for calls whose result is known to equal the first argument. How register masks are used by the allocator is on the regalloc registers page.
Sources: llvm/lib/Target/AArch64/AArch64CallingConvention.td:594, llvm/lib/Target/AArch64/AArch64CallingConvention.td:702, llvm/lib/Target/AArch64/AArch64CallingConvention.td:720
A target can define its own predicate classes on top of CCIf. AArch64 defines three at the top of its file:
/// CCIfBigEndian - Match only if we're in big endian mode.
class CCIfBigEndian<CCAction A> :
CCIf<"State.getMachineFunction().getDataLayout().isBigEndian()", A>;
class CCIfILP32<CCAction A> :
CCIf<"State.getMachineFunction().getDataLayout().getPointerSize() == 4", A>;
/// CCIfSubtarget - Match if the current subtarget has a feature F.
class CCIfSubtarget<string F, CCAction A>
The string is pasted into the generated C++, where State is the CCState& argument.
Sources: llvm/lib/Target/AArch64/AArch64CallingConvention.td:13-24
- 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