Repository navigation
Calling Conventions 03 ccstate
Relevant source files
The following files were used as context for generating this wiki page:
The CCValAssign record, the CCAssignFn signature, the CCState allocation functions and the Analyze* drivers. How the three lowering hooks consume the result is on LowerFormalArguments, LowerCall and LowerReturn.
flowchart LR
INS["Ins / Outs<br/>(ISD::InputArg / OutputArg)"] --> AN["CCState::Analyze*"]
AN -->|"for each value"| FN["CCAssignFn<br/>(generated CC_Toy)"]
FN -->|"AllocateReg / AllocateStack"| ST["CCState: UsedRegs, StackSize"]
FN -->|"addLoc"| LOCS["SmallVector<CCValAssign>"]
LOCS --> HOOK["target hook walks the list"]
Each entry holds the value number, its original type (ValVT), the type in the location (LocVT), either a register or a stack offset, and a LocInfo describing the conversion:
class CCValAssign {
public:
enum LocInfo {
Full, // The value fills the full location.
SExt, // The value is sign extended in the location.
ZExt, // The value is zero extended in the location.
AExt, // The value is extended with undefined upper bits.
...
BCvt, // The value is bit-converted in the location.
Trunc, // The value is truncated in the location.
...
FPExt, // The floating-point value is fp-extended in the location.
Indirect // The location contains pointer to the value.| Accessor | Meaning |
|---|---|
isRegLoc() / getLocReg()
|
Value is in a physical register |
isMemLoc() / getLocMemOffset()
|
Value is on the stack at this offset from the start of the argument area |
getValVT() / getLocVT()
|
Type before and after conversion |
getLocInfo() |
How to convert (SExt → emit AssertSext on entry, SIGN_EXTEND at a call, ...) |
needsCustom() |
Set by a CCCustom handler; the hook must handle it specially |
Sources: llvm/include/llvm/CodeGen/CallingConvLower.h:34-55
Every Entry = 1 record becomes a function of this type:
typedef bool CCAssignFn(unsigned ValNo, MVT ValVT, MVT LocVT,
CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
Type *OrigTy, CCState &State);It returns false when it assigned a location and true when no rule matched. Targets can also write these by hand and mix them with generated ones.
Sources: llvm/include/llvm/CodeGen/CallingConvLower.h:157-166
CCAssignToReg<[...]> turns into a call to AllocateReg, which picks the first unallocated register in the list and marks it (and its aliases) used:
MCRegister AllocateReg(ArrayRef<MCPhysReg> Regs) {
unsigned FirstUnalloc = getFirstUnallocated(Regs);
if (FirstUnalloc == Regs.size())
return MCRegister(); // Didn't find the reg.
// Mark the register and any aliases as allocated.
MCPhysReg Reg = Regs[FirstUnalloc];
MarkAllocated(Reg);
return Reg;Because aliases are marked, assigning W0 also makes X0 unavailable. CCAssignToStack<Size, Align> becomes AllocateStack:
int64_t AllocateStack(unsigned Size, Align Alignment) {
int64_t Offset;
if (NegativeOffsets) {
StackSize = alignTo(StackSize + Size, Alignment);
Offset = -StackSize;
} else {
Offset = alignTo(StackSize, Alignment);
StackSize = Offset + Size;
}
MaxStackArgAlign = std::max(Alignment, MaxStackArgAlign);
ensureMaxAlignment(Alignment);
return Offset;
}After the analysis, getStackSize() is the total size of the outgoing (or incoming) stack argument area. LowerCall passes it to CALLSEQ_START.
Sources: llvm/include/llvm/CodeGen/CallingConvLower.h:246, llvm/include/llvm/CodeGen/CallingConvLower.h:333-360, llvm/include/llvm/CodeGen/CallingConvLower.h:408-431
| Function | Used by | Input | On failure |
|---|---|---|---|
AnalyzeFormalArguments(Ins, Fn) |
LowerFormalArguments |
ISD::InputArg |
report_fatal_error("unable to allocate function argument #N") |
AnalyzeCallOperands(Outs, Fn) |
LowerCall |
ISD::OutputArg |
llvm_unreachable (prints the type in debug builds) |
AnalyzeCallResult(Ins, Fn) |
LowerCall |
ISD::InputArg |
fatal error |
AnalyzeReturn(Outs, Fn) |
LowerReturn |
ISD::OutputArg |
report_fatal_error("unable to allocate function return #N") |
CheckReturn(Outs, Fn) |
CanLowerReturn |
ISD::OutputArg |
returns false
|
All of them are the same loop: call Fn for each value with ValVT == LocVT and LocInfo == Full:
CCState::AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
CCAssignFn Fn) {
unsigned NumArgs = Ins.size();
for (unsigned i = 0; i != NumArgs; ++i) {
MVT ArgVT = Ins[i].VT;
ISD::ArgFlagsTy ArgFlags = Ins[i].Flags;
if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, Ins[i].OrigTy, *this))
report_fatal_error("unable to allocate function argument #" + Twine(i));
}
}CheckReturn is the only one that does not fail: it is used to ask whether the return fits, and if not, LLVM rewrites the function to return through a hidden sret pointer.
AArch64 does not use AnalyzeFormalArguments directly: it writes the loop itself so it can pass the original i8/i16 type as ValVT for stack arguments (see LowerFormalArguments).
Sources: llvm/lib/CodeGen/CallingConvLower.cpp:85-141, llvm/lib/CodeGen/CallingConvLower.cpp:164-180
CCState::CCState(CallingConv::ID CC, bool IsVarArg, MachineFunction &MF,
SmallVectorImpl<CCValAssign> &Locs, LLVMContext &Context,
bool NegativeOffsets)IsVarArg is what CCIfVarArg tests. NegativeOffsets is for targets whose argument area grows the other way.
HandleByVal (reached from CCPassByVal) takes the size and alignment from the byval attribute, lets the target claim registers for part of the aggregate through TargetLowering::HandleByVal, and allocates the rest on the stack:
Align Alignment = ArgFlags.getNonZeroByValAlign();
unsigned Size = ArgFlags.getByValSize();
...
MF.getSubtarget().getTargetLowering()->HandleByVal(this, Size, Alignment);
Size = unsigned(alignTo(Size, MinAlign));
uint64_t Offset = AllocateStack(Size, Alignment);
addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));Sources: llvm/lib/CodeGen/CallingConvLower.cpp:28-59, llvm/include/llvm/CodeGen/CallingConvLower.h:433-442
| Function | Line | Use |
|---|---|---|
getRemainingRegParmsForType |
203 | Registers still free for a type (musttail forwarding) |
analyzeMustTailForwardedRegisters |
243 | Keep unused argument registers live through a musttail vararg thunk |
resultsCompatible |
264 | Do two conventions return values in the same places? (tail-call check) |
- 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