Repository navigation
Calling Conventions 05 lower call
Relevant source files
The following files were used as context for generating this wiki page:
The structure of AArch64's LowerCall and what each part produces. Tail calls are on Tail Calls and Sibling Calls; varargs and byval on Varargs, byval, sret. Line numbers for AArch64ISelLowering.cpp are from the committed file at a2171756.
Generic code fills a CallLoweringInfo (callee, arguments as Outs/OutVals, expected results as Ins, calling convention, IsTailCall, IsVarArg) in SelectionDAGBuilder::LowerCallTo, and TargetLowering::LowerCallTo calls the target hook:
virtual SDValue
LowerCall(CallLoweringInfo &/*CLI*/,
SmallVectorImpl<SDValue> &/*InVals*/) const {
llvm_unreachable("Not Implemented");
}Sources: llvm/include/llvm/CodeGen/TargetLowering.h:5187-5191, llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp:9202, llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp:11333-11334
flowchart TD
A["analyze operands (CC_*) and results (RetCC_*)"] --> B{"tail call possible?"}
B --> C["CALLSEQ_START NumBytes"]
C --> D["argument values: extend / bitcast per LocInfo"]
D --> E["registers: RegsToPass list"]
D --> F["stack: store to SP + offset (MemOpChains)"]
F --> G["TokenFactor of stores"]
E --> H["CopyToReg chain, glued"]
G --> H
H --> I["AArch64ISD::CALL (callee, arg regs, regmask, glue)"]
I --> J["CALLSEQ_END NumBytes"]
J --> K["LowerCallResult: CopyFromReg per RetCC location"]
| Step | Lines | Notes |
|---|---|---|
| Analyze operands and results | 10471-10492 |
analyzeCallOperands (a static helper at 9977) and AnalyzeCallResult
|
| Tail-call decision | 10516-10532 | See Tail Calls |
| Stack size | 10535-10541 |
CCInfo.getStackSize(), 0 for sibling calls |
CALLSEQ_START |
10648-10649 | Not emitted for sibling calls |
| Argument registers and stores | 10666-10941 |
RegsToPass, MemOpChains
|
| Call node and mask | 10999-11083 |
CALL or TC_RETURN
|
CALLSEQ_END |
11119-11122 | Records how many bytes the callee pops |
| Results | 11127-11129 |
LowerCallResult (9875) |
Sources: llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:10442-10541, llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:9977, llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:9875
// Analyze operands of the call, assigning locations to each operand.
SmallVector<CCValAssign, 16> ArgLocs;
CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
...
analyzeCallOperands(*this, Subtarget, CLI, CCInfo);
CCAssignFn *RetCC = CCAssignFnForReturn(CallConv);
// Assign locations to each value returned by this call.
SmallVector<CCValAssign, 16> RVLocs;
CCState RetCCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
*DAG.getContext());
RetCCInfo.AnalyzeCallResult(Ins, RetCC);Two separate CCState objects are used: one for arguments, one for results, because the same registers (x0...) can appear in both.
Sources: llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:10471-10492
CALLSEQ_START and CALLSEQ_END bracket the call. They become the ADJCALLSTACKDOWN / ADJCALLSTACKUP pseudo instructions, which frame lowering later removes or turns into SP adjustments (eliminateCallFramePseudoInstr, see the frame wiki). The byte count is the outgoing stack argument area:
// Get a count of how many bytes are to be pushed on the stack.
unsigned NumBytes = CCInfo.getStackSize();
...
Chain = DAG.getCALLSEQ_START(Chain, IsTailCall ? 0 : NumBytes, 0, DL);
...
uint64_t CalleePopBytes =
DoesCalleeRestoreStack(CallConv, TailCallOpt) ? alignTo(NumBytes, 16) : 0;
Chain = DAG.getCALLSEQ_END(Chain, NumBytes, CalleePopBytes, InGlue, DL);Sources: llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:10535, llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:10649, llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:11119-11122
For each CCValAssign, the argument value is first converted according to LocInfo (SExt → SIGN_EXTEND, BCvt → BITCAST, ...). Then:
-
Register location: the pair (register, value) is added to
RegsToPass(line 10821). After all stores, the pairs become a chain ofCopyToRegnodes glued together, so nothing can be scheduled between the copies and the call. -
Stack location: the value is stored at
SP + LocMemOffset; the store chains are collected inMemOpChainsand merged with oneTokenFactor(lines 10940-10941). For a tail call the store goes to a fixed object in the caller's own incoming area instead (line 10880).
Sources: llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:10666, llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:10821, llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:10880, llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:10940-10941
The operands of AArch64ISD::CALL are: chain, callee, one register operand per argument register (so the registers are live into the call), the register mask, and the glue.
unsigned Opc = IsTailCall ? AArch64ISD::TC_RETURN : AArch64ISD::CALL;
std::vector<SDValue> Ops;
Ops.push_back(Chain);
Ops.push_back(Callee);
...
// Add a register mask operand representing the call-preserved registers.
const uint32_t *Mask;
const AArch64RegisterInfo *TRI = Subtarget->getRegisterInfo();
if (IsThisReturn) {
...
} else
Mask = TRI->getCallPreservedMask(MF, CallConv);
...
assert(Mask && "Missing call preserved mask for calling convention");
Ops.push_back(DAG.getRegisterMask(Mask));The mask is what tells the register allocator which registers the call clobbers: every register not in the mask is dead after the call. It comes from the CalleeSavedRegs record through getCallPreservedMask (AArch64RegisterInfo.cpp, line 270-271). The regalloc wiki's registers page explains how the allocator uses it.
Sources: llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:10999-11003, llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:11063-11083, llvm/lib/Target/AArch64/AArch64RegisterInfo.cpp:270-271
After CALLSEQ_END, LowerCallResult walks RVLocs and emits a CopyFromReg for each return register, glued to the call so the copies stay right after it, then undoes any extension:
// Handle result values, copying them out of physregs into vregs that we
// return.
SDValue Result = LowerCallResult(
Chain, InGlue, CallConv, IsVarArg, RVLocs, DL, DAG, InVals, IsThisReturn,
IsThisReturn ? OutVals[0] : SDValue(), RequiresSMChange);Sources: llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:11125-11129
llc -O2 -mtriple=aarch64-linux-gnu -stop-after=finalize-isel for a call with ten i64 arguments (callee(x, 1, 2, ..., 9)), first lines of the body:
%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)
%6:gpr32 = MOVi32imm 1
...
$x0 = COPY %0
$x1 = COPY %7
$x2 = COPY %9
...
ADJCALLSTACKDOWN 16 is the CALLSEQ_START with 16 bytes for the two stack arguments; the two STRXui stores are the MemOpChains; the $xN = COPY lines are RegsToPass. The full function, including the BL with its register mask, is on the walkthroughs page.
- 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