Repository navigation
Frame Lowering 10 aarch64 walkthroughs
Relevant source files
The following files were used as context for generating this wiki page:
Concrete evidence for each frame shape: leaf, call, large frame, dynamic alloca, and shrink wrapping. Each case links back to the page that explains it.
define i64 @leaf(i64 %a, i64 %b) {
%s = mul i64 %a, %b
ret i64 %s
}leaf: // @leaf
.cfi_startproc
mul x0, x0, x1
retNo callee-saved registers used, no stack objects, stackSize 0: emitPrologue emits nothing. With -frame-pointer=all the same function gets a frame record (stp x29, x30, [sp, #-16]!, mov x29, sp), because hasFPImpl returns true when frame-pointer elimination is disabled.
declare i64 @h(i64)
define i64 @withcall(i64 %a) {
%r = call i64 @h(i64 %a)
%s = add i64 %r, %a
ret i64 %s
}Before PEI:
renamable $x19 = COPY $x0
ADJCALLSTACKDOWN 0, 0, implicit-def dead $sp, implicit $sp
BL @h, csr_aarch64_aapcs, implicit-def dead $lr, implicit $sp, implicit $x0, implicit-def $sp, implicit-def $x0
ADJCALLSTACKUP 0, 0, implicit-def dead $sp, implicit $sp
$x0 = ADDXrr killed renamable $x0, killed renamable $x19
RET_ReallyLR implicit $x0
After PEI:
early-clobber $sp = frame-setup STPXpre killed $lr, killed $x19, $sp, -2 :: (store (s64) into %stack.1), (store (s64) into %stack.0)
frame-setup CFI_INSTRUCTION def_cfa_offset 16
frame-setup CFI_INSTRUCTION offset $w19, -8
frame-setup CFI_INSTRUCTION offset $w30, -16
renamable $x19 = COPY $x0
BL @h, csr_aarch64_aapcs, implicit-def dead $lr, implicit $sp, implicit $x0, implicit-def $sp, implicit-def $x0
$x0 = ADDXrr killed renamable $x0, killed renamable $x19
early-clobber $sp, $lr, $x19 = frame-destroy LDPXpost $sp, 2 :: (load (s64) from %stack.1), (load (s64) from %stack.0)
RET_ReallyLR implicit $x0
| Change | Step |
|---|---|
ADJCALLSTACKDOWN/UP gone |
calculateCallFrameInfo (reserved call frame) |
x19 and lr saved |
determineCalleeSaves + pairing in spillCalleeSavedRegisters
|
Pre-indexed STPXpre also allocates the frame |
Prologue combined the CSR store with the SP decrement |
CFI_INSTRUCTIONs |
Prologue CFI (CFI) |
stackSize: 16, slots at −8 and −16 |
calculateFrameObjectOffsets |
Sources: llvm/lib/CodeGen/PrologEpilogInserter.cpp:211-283
declare void @ext(ptr)
define void @bigframe() {
%buf = alloca [70000 x i8], align 16
call void @ext(ptr %buf)
ret void
}Before PEI: $x0 = ADDXri %stack.0.buf, 0, 0. After PEI:
early-clobber $sp = frame-setup STPXpre killed $fp, killed $lr, $sp, -2 :: (store (s64) into %stack.2), (store (s64) into %stack.1)
$sp = frame-setup SUBXri $sp, 17, 12
$sp = frame-setup SUBXri $sp, 368, 0
frame-setup CFI_INSTRUCTION def_cfa_offset 70016
frame-setup CFI_INSTRUCTION offset $w30, -8
frame-setup CFI_INSTRUCTION offset $w29, -16
$x0 = ADDXri $sp, 0, 0
BL @ext, csr_aarch64_aapcs, implicit-def dead $lr, implicit $sp, implicit killed $x0, implicit-def $sp
$sp = frame-destroy ADDXri $sp, 17, 12
$sp = frame-destroy ADDXri $sp, 368, 0
early-clobber $sp, $fp, $lr = frame-destroy LDPXpost $sp, 2 :: (load (s64) from %stack.2), (load (s64) from %stack.1)
RET_ReallyLR
- The 70000-byte local (rounded to 70000 + 16 for the frame record = 70016) needs two
subinstructions: 17 × 4096 = 69632 plus 368. -
x29is saved and restored even thoughmov x29, spnever appears:determineCalleeSavesadded it to the saved set for this large frame (it is not used as a frame pointer here). The exact rule is inAArch64FrameLowering::determineCalleeSaves(line 2521 onwards); this page does not trace which condition triggered it. -
%stack.0.bufbecame$sp + 0ineliminateFrameIndex(Frame Index Elimination).
define void @dynalloca(i64 %n) {
%p = alloca i8, i64 %n, align 16
call void @ext(ptr %p)
ret void
}dynalloca: // @dynalloca
.cfi_startproc
stp x29, x30, [sp, #-16]! // 16-byte Folded Spill
mov x29, sp
.cfi_def_cfa w29, 16
.cfi_offset w30, -8
.cfi_offset w29, -16
add x9, x0, #15
mov x8, sp
and x9, x9, #0xfffffffffffffff0
sub x0, x8, x9
mov sp, x0
bl ext
mov sp, x29
ldp x29, x30, [sp], #16 // 16-byte Folded Reload
rethasVarSizedObjects() makes hasFPImpl true, so x29 is set up and the CFA is based on it (.cfi_def_cfa w29, 16). The allocation rounds %n up to 16 and moves SP; the epilogue restores SP from x29 (mov sp, x29) instead of adding a known size. See TargetFrameLowering Hooks.
Sources: llvm/lib/Target/AArch64/AArch64FrameLowering.cpp:588-594, llvm/lib/Target/AArch64/AArch64FrameLowering.cpp:659-667
The shrink function's prologue is placed after the tbz so the fast path has no frame; with -enable-shrink-wrap=false it is back in the entry block. Both outputs are on Shrink Wrapping.
llc -O2 -mtriple=aarch64-linux-gnu frame.ll -o -
llc -O2 -mtriple=aarch64-linux-gnu -stop-before=prolog-epilog frame.ll -o before.mir
llc -O2 -mtriple=aarch64-linux-gnu -stop-after=prolog-epilog frame.ll -o after.mir
llc -O2 -mtriple=aarch64-linux-gnu -frame-pointer=all frame.ll -o -
llc -O2 -mtriple=aarch64-linux-gnu -enable-shrink-wrap=false frame.ll -o -
The register allocation that chose x19 for the value kept across the call is traced in the regalloc wiki's AArch64 walkthroughs; the stack-argument side of calls is on the calling conventions walkthroughs.
- 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