Repository navigation
Calling Conventions 06 lower return
Relevant source files
The following files were used as context for generating this wiki page:
Both hooks as implemented by AArch64, and the minimal version for a new target. Line numbers for AArch64ISelLowering.cpp are from the committed file at a2171756.
bool AArch64TargetLowering::CanLowerReturn(
CallingConv::ID CallConv, MachineFunction &MF, bool isVarArg,
const SmallVectorImpl<ISD::OutputArg> &Outs, LLVMContext &Context,
const Type *RetTy) const {
CCAssignFn *RetCC = CCAssignFnForReturn(CallConv);
SmallVector<CCValAssign, 16> RVLocs;
CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
return CCInfo.CheckReturn(Outs, RetCC);
}CheckReturn runs the return rules and returns false as soon as one value cannot be placed. RetCC_AArch64_AAPCS has no stack rules, so anything beyond eight integer or eight FP/vector registers fails, and the function is demoted. The default TargetLowering::CanLowerReturn returns true, which is only safe if the return rules can place every value the target can return:
virtual bool CanLowerReturn(CallingConv::ID /*CallConv*/,
MachineFunction &/*MF*/, bool /*isVarArg*/,
const SmallVectorImpl<ISD::OutputArg> &/*Outs*/,
LLVMContext &/*Context*/, const Type *RetTy) const
{
// Return true by default to get preexisting behavior.
return true;
}Sources: llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:11172-11180, llvm/lib/CodeGen/CallingConvLower.cpp:99-109, llvm/lib/Target/AArch64/AArch64CallingConvention.td:128-162, llvm/include/llvm/CodeGen/TargetLowering.h:5199-5206
flowchart LR
A["AnalyzeReturn(Outs, RetCC)"] --> B["for each RVLoc: extend/bitcast, CopyToReg (glued)"]
B --> C["RetOps: chain, one register operand per return reg"]
C --> D["sret pointer in X0 (Win64), CSRs via copy"]
D --> E["AArch64ISD::RET_GLUE"]
AArch64TargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
bool isVarArg,
const SmallVectorImpl<ISD::OutputArg> &Outs,
const SmallVectorImpl<SDValue> &OutVals,
const SDLoc &DL, SelectionDAG &DAG) const {
...
CCAssignFn *RetCC = CCAssignFnForReturn(CallConv);
SmallVector<CCValAssign, 16> RVLocs;
CCState CCInfo(CallConv, isVarArg, MF, RVLocs, *DAG.getContext());
CCInfo.AnalyzeReturn(Outs, RetCC);
// Copy the result values into the output registers.
SDValue Glue;
...Each copy is glued to the previous one so the copies and the return stay together. Every return register is also added to RetOps as a register operand: that makes the register live-out (used by the return instruction), so the copies are not deleted as dead. The function ends with:
RetOps[0] = Chain; // Update chain.
// Add the glue if we have it.
if (Glue.getNode())
RetOps.push_back(Glue);
...
return DAG.getNode(AArch64ISD::RET_GLUE, DL, MVT::Other, RetOps);RET_GLUE is matched by the pattern on the RET_ReallyLR pseudo, def RET_ReallyLR : Pseudo<(outs), (ins), [(AArch64retglue)]>, seen in MIR as RET_ReallyLR implicit $w0; it becomes ret before emission.
Sources: llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:11182-11200, llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:11292-11326, llvm/lib/Target/AArch64/AArch64InstrInfo.td:895, llvm/lib/Target/AArch64/AArch64InstrInfo.td:3758
llc -O2 -mtriple=aarch64-linux-gnu -stop-after=finalize-isel on i32 @add(i32 %a, i32 %b):
%1:gpr32 = COPY $w1
%0:gpr32 = COPY $w0
%2:gpr32 = ADDWrr %0, %1
$w0 = COPY %2
RET_ReallyLR implicit $w0
$w0 = COPY %2 is the CopyToReg from LowerReturn; implicit $w0 on the return is the register operand added to RetOps.
Illustrative, not LLVM source.
SDValue ToyTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CC,
bool IsVarArg, const SmallVectorImpl<ISD::OutputArg> &Outs,
const SmallVectorImpl<SDValue> &OutVals, const SDLoc &DL,
SelectionDAG &DAG) const {
SmallVector<CCValAssign, 4> RVLocs;
CCState CCInfo(CC, IsVarArg, DAG.getMachineFunction(), RVLocs,
*DAG.getContext());
CCInfo.AnalyzeReturn(Outs, RetCC_Toy);
SDValue Glue;
SmallVector<SDValue, 4> RetOps(1, Chain);
for (unsigned i = 0; i != RVLocs.size(); ++i) {
CCValAssign &VA = RVLocs[i];
SDValue V = OutVals[i];
if (VA.getLocInfo() == CCValAssign::SExt)
V = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), V);
else if (VA.getLocInfo() == CCValAssign::ZExt)
V = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), V);
else if (VA.getLocInfo() == CCValAssign::AExt)
V = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), V);
Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), V, Glue);
Glue = Chain.getValue(1);
RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
}
RetOps[0] = Chain;
if (Glue.getNode())
RetOps.push_back(Glue);
return DAG.getNode(ToyISD::RET, DL, MVT::Other, RetOps);
}
bool ToyTargetLowering::CanLowerReturn(CallingConv::ID CC, MachineFunction &MF,
bool IsVarArg, const SmallVectorImpl<ISD::OutputArg> &Outs,
LLVMContext &Ctx, const Type *RetTy) const {
SmallVector<CCValAssign, 4> RVLocs;
CCState CCInfo(CC, IsVarArg, MF, RVLocs, Ctx);
return CCInfo.CheckReturn(Outs, RetCC_Toy);
}With CanLowerReturn in place, an i128 return on a 32-bit Toy (four i32 parts, only r0/r1 available) is demoted to a hidden sret pointer automatically.
- 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