Repository navigation
Calling Conventions 04 formal arguments
Relevant source files
The following files were used as context for generating this wiki page:
The steps of AArch64's LowerFormalArguments, and the pattern a new target should copy. Line numbers for AArch64ISelLowering.cpp are from the committed file at a2171756.
virtual SDValue LowerFormalArguments(
SDValue /*Chain*/, CallingConv::ID /*CallConv*/, bool /*isVarArg*/,
const SmallVectorImpl<ISD::InputArg> & /*Ins*/, const SDLoc & /*dl*/,
SelectionDAG & /*DAG*/, SmallVectorImpl<SDValue> & /*InVals*/) const {
llvm_unreachable("Not Implemented");
}It is called from SelectionDAGISel::LowerArguments, which builds Ins from the IR arguments (splitting i128 into two i64s, setting sext/zext/byval flags) and maps the returned values back to the IR arguments.
Sources: llvm/include/llvm/CodeGen/TargetLowering.h:4893-4898, llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp:11970
flowchart TD
A["1. CCState + assign a location to every Ins[i]"] --> B{"for each CCValAssign"}
B -->|byval| BV["fixed object at LocMemOffset, pass its frame index"]
B -->|register| R["pick reg class, MF.addLiveIn, CopyFromReg, undo extension"]
B -->|stack| S["CreateFixedObject, extending load"]
R --> I["InVals.push_back"]
S --> I
BV --> I
I --> V["2. varargs: save remaining registers, record stack offset"]
V --> F["3. record stack argument size for tail calls"]
// Assign locations to all of the incoming arguments.
SmallVector<CCValAssign, 16> ArgLocs;
CCState CCInfo(CallConv, isVarArg, MF, ArgLocs, *DAG.getContext());
...
CCAssignFn *AssignFn = CCAssignFnForCall(CallConv, UseVarArgCC);
for (unsigned i = 0; i != NumArgs; ++i) {
MVT ValVT = Ins[i].VT;
if (Ins[i].isOrigArg()) {
...
// If ActualMVT is i1/i8/i16, we should set LocVT to i8/i8/i16.
if (ActualMVT == MVT::i1 || ActualMVT == MVT::i8)
ValVT = MVT::i8;
else if (ActualMVT == MVT::i16)
ValVT = MVT::i16;
}
bool Res = AssignFn(i, ValVT, ValVT, CCValAssign::Full, Ins[i].Flags,
Ins[i].OrigTy, CCInfo);
assert(!Res && "Call operand has unhandled type");AArch64 runs the loop itself instead of calling AnalyzeFormalArguments so that an i8 passed on the stack is treated as one byte (the stack rule CCAssignToStack<8, 8> keeps the slot 8 bytes, but the load is a byte load).
Sources: llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:9314-9372
For each register location, choose a register class from the location type, make the physical register a live-in of the function (which creates a virtual register), and copy from it:
if (VA.isRegLoc()) {
// Arguments stored in registers.
EVT RegVT = VA.getLocVT();
const TargetRegisterClass *RC;
if (RegVT == MVT::i32)
RC = &AArch64::GPR32RegClass;
else if (RegVT == MVT::i64)
RC = &AArch64::GPR64RegClass;
...
// Transform the arguments in physical registers into virtual ones.
Register Reg = MF.addLiveIn(VA.getLocReg(), RC);
...
ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);Then LocInfo decides the conversion back to the value type. BCvt adds a BITCAST; SExt/ZExt/AExt need nothing here on AArch64 (the truncation to the IR type is inserted by generic code).
Sources: llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:9405-9486
A stack location becomes a fixed frame object: its offset is relative to the stack pointer at the call, which frame lowering later turns into an offset from SP or FP (see the frame wiki).
} else {
int FI = MFI.CreateFixedObject(ArgSize, ArgOffset + BEAlign, true);
// Create load nodes to retrieve arguments from the stack.
FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
PtrInfo = MachinePointerInfo::getFixedStack(MF, FI);
}
...
ArgValue = DAG.getExtLoad(ExtType, DL, VA.getLocVT(), Chain, FIN, PtrInfo,
MemVT);ExtType comes from LocInfo (SExt → SEXTLOAD ...). The last true marks the object immutable, which lets later passes treat the load as invariant.
Sources: llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:9487-9552
A byval argument is already in the caller's outgoing area, so the callee just creates a fixed object over it and passes its address:
if (Ins[i].Flags.isByVal()) {
...
unsigned FrameIdx =
MFI.CreateFixedObject(8 * NumRegs, VA.getLocMemOffset(), false);
SDValue FrameIdxN = DAG.getFrameIndex(FrameIdx, PtrVT);
InVals.push_back(FrameIdxN);
continue;
}Sources: llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:9384-9399
If the function calls va_start, the unused argument registers are spilled (saveVarArgRegisters) and the offset of the first variadic stack argument is recorded; see Varargs, byval, sret. Finally the size of the incoming stack argument area is saved, because tail calls may reuse it:
unsigned StackArgSize = CCInfo.getStackSize();
...
FuncInfo->setBytesInStackArgArea(StackArgSize);Sources: llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:9640-9660, llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:9702-9720
llc -O2 -mtriple=aarch64-linux-gnu on double @mixed(i32 %i, double %d, i8 %c, float %f) (body: sext %c, add to %i, convert, add %d and fpext %f):
mixed: // @mixed
.cfi_startproc
add w8, w0, w1, sxtb
fcvt d1, s1
scvtf d2, w8
fadd d0, d2, d0
fadd d0, d0, d1
ret%i is in w0, %d in d0, %c in w1 (the next free GPR, not w2), %f in s1. The sxtb shows that the i8 arrives with undefined upper bits and the callee extends it.
Illustrative, not LLVM source. For a target with only i32 arguments:
SDValue ToyTargetLowering::LowerFormalArguments(
SDValue Chain, CallingConv::ID CC, bool IsVarArg,
const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
MachineFunction &MF = DAG.getMachineFunction();
SmallVector<CCValAssign, 16> Locs;
CCState CCInfo(CC, IsVarArg, MF, Locs, *DAG.getContext());
CCInfo.AnalyzeFormalArguments(Ins, CC_Toy);
for (CCValAssign &VA : Locs) {
SDValue V;
if (VA.isRegLoc()) {
Register VReg = MF.addLiveIn(VA.getLocReg(), &Toy::GPRRegClass);
V = DAG.getCopyFromReg(Chain, DL, VReg, VA.getLocVT());
} else {
int FI = MF.getFrameInfo().CreateFixedObject(4, VA.getLocMemOffset(), true);
V = DAG.getLoad(VA.getLocVT(), DL, Chain, DAG.getFrameIndex(FI, MVT::i32),
MachinePointerInfo::getFixedStack(MF, FI));
}
if (VA.getLocInfo() == CCValAssign::SExt)
V = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), V,
DAG.getValueType(VA.getValVT()));
if (VA.getLocInfo() != CCValAssign::Full)
V = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), V);
InVals.push_back(V);
}
return Chain;
}- 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