Repository navigation
Bringup 09 frame lowering
Relevant source files
The following files were used as context for generating this wiki page:
The minimum frame lowering a new target needs. The frame lowering wiki covers MachineFrameInfo, all the hooks, the PEI pass step by step, callee-saved spilling, frame index elimination, shrink wrapping and CFI.
flowchart TD
A["calculateCallFrameInfo<br/>(eliminateCallFramePseudoInstr)"] --> B["calculateSaveRestoreBlocks"]
B --> C["spillCalleeSavedRegs<br/>(determineCalleeSaves, spill/restoreCalleeSavedRegisters)"]
C --> D["processFunctionBeforeFrameFinalized"]
D --> E["calculateFrameObjectOffsets"]
E --> F["insertPrologEpilogCode<br/>(emitPrologue, emitEpilogue)"]
F --> G["replaceFrameIndices<br/>(eliminateFrameIndex)"]
G --> H["scavengeFrameVirtualRegs"]
This is the order in PEIImpl::run. Target hooks are in parentheses.
Sources: llvm/lib/CodeGen/PrologEpilogInserter.cpp:211-283
| Hook | Class | Declared at | Purpose |
|---|---|---|---|
Constructor (StackDirection, StackAlign, LocalAreaOffset)
|
TargetFrameLowering |
TargetFrameLowering.h:86 |
Stack grows down, alignment, offset of local area |
emitPrologue, emitEpilogue
|
TargetFrameLowering |
211, 213 | Pure virtual: build and remove the frame |
hasFPImpl |
TargetFrameLowering |
520 | Pure virtual: does this function need a frame pointer? (hasFP at 306 calls it) |
hasReservedCallFrame |
TargetFrameLowering |
315 | Whether outgoing argument space is part of the fixed frame |
eliminateCallFramePseudoInstr |
TargetFrameLowering |
431 | Removes ADJCALLSTACKDOWN/UP; the default is llvm_unreachable, so any target that uses call-frame pseudos must override it |
eliminateFrameIndex |
TargetRegisterInfo |
TargetRegisterInfo.h:1020 |
Pure virtual: rewrites frame-index operands |
AArch64's versions are short wrappers at this commit: emitPrologue and emitEpilogue construct an AArch64PrologueEmitter / AArch64EpilogueEmitter (in AArch64PrologueEpilogue.cpp) and call it:
void AArch64FrameLowering::emitPrologue(MachineFunction &MF,
MachineBasicBlock &MBB) const {
AArch64PrologueEmitter PrologueEmitter(MF, MBB, *this);
PrologueEmitter.emitPrologue();
}The default for call-frame pseudos:
eliminateCallFramePseudoInstr(MachineFunction &MF,
MachineBasicBlock &MBB,
MachineBasicBlock::iterator MI) const {
llvm_unreachable("Call Frame Pseudo Instructions do not exist on this "
"target!");
}Sources: llvm/include/llvm/CodeGen/TargetFrameLowering.h:86, llvm/include/llvm/CodeGen/TargetFrameLowering.h:211-213, llvm/include/llvm/CodeGen/TargetFrameLowering.h:306-315, llvm/include/llvm/CodeGen/TargetFrameLowering.h:431-436, llvm/include/llvm/CodeGen/TargetFrameLowering.h:520, llvm/include/llvm/CodeGen/TargetRegisterInfo.h:1020, llvm/lib/Target/AArch64/AArch64FrameLowering.cpp:1236-1246
Two AArch64 functions from llc -O2 -mtriple=aarch64-linux-gnu. A leaf function gets no frame at all:
leaf: // @leaf
.cfi_startproc
mul x0, x0, x1
retA function with a call saves the return address and the callee-saved register it uses in one paired store, and emits CFI for the unwinder:
withcall: // @withcall
.cfi_startproc
stp x30, x19, [sp, #-16]! // 16-byte Folded Spill
.cfi_def_cfa_offset 16
.cfi_offset w19, -8
.cfi_offset w30, -16
mov x19, x0
bl h
add x0, x0, x19
ldp x30, x19, [sp], #16 // 16-byte Folded Reload
retThe frame lowering walkthroughs show the MIR before and after PEI and the large-frame and alloca cases.
Illustrative, not LLVM source. Fixed-size frames only, no frame pointer, outgoing arguments in the reserved call frame:
class ToyFrameLowering : public TargetFrameLowering {
public:
ToyFrameLowering()
: TargetFrameLowering(StackGrowsDown, Align(8), /*LocalAreaOffset=*/0) {}
void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override {
uint64_t Size = MF.getFrameInfo().getStackSize();
if (!Size) return;
const auto &TII = *MF.getSubtarget<ToySubtarget>().getInstrInfo();
auto MBBI = MBB.begin();
BuildMI(MBB, MBBI, DebugLoc(), TII.get(Toy::ADDI), Toy::SP)
.addReg(Toy::SP).addImm(-(int64_t)Size)
.setMIFlag(MachineInstr::FrameSetup);
}
void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override {
uint64_t Size = MF.getFrameInfo().getStackSize();
if (!Size) return;
const auto &TII = *MF.getSubtarget<ToySubtarget>().getInstrInfo();
auto MBBI = MBB.getFirstTerminator();
BuildMI(MBB, MBBI, DebugLoc(), TII.get(Toy::ADDI), Toy::SP)
.addReg(Toy::SP).addImm(Size)
.setMIFlag(MachineInstr::FrameDestroy);
}
MachineBasicBlock::iterator
eliminateCallFramePseudoInstr(MachineFunction &, MachineBasicBlock &MBB,
MachineBasicBlock::iterator I) const override {
return MBB.erase(I); // valid because hasReservedCallFrame() is true
}
protected:
bool hasFPImpl(const MachineFunction &) const override { return false; }
};// ToyRegisterInfo.cpp (illustrative)
bool ToyRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
int SPAdj, unsigned FIOp,
RegScavenger *RS) const {
MachineInstr &MI = *II;
MachineFunction &MF = *MI.getMF();
int FI = MI.getOperand(FIOp).getIndex();
int64_t Off = MF.getFrameInfo().getObjectOffset(FI) +
MF.getFrameInfo().getStackSize() +
MI.getOperand(FIOp + 1).getImm();
assert(isInt<16>(Off) && "large frames need a scratch register");
MI.getOperand(FIOp).ChangeToRegister(Toy::SP, false);
MI.getOperand(FIOp + 1).ChangeToImmediate(Off);
return false; // MI was not erased
}This assumes every frame-index user has the operand pair (base, offset) as in the LW/SW/ADDI records on Instructions and ISel. Callee-saved registers need no extra code: the default spillCalleeSavedRegisters returns false, so PEI calls TargetFrameLowering::spillCalleeSavedRegister for each register, which uses ToyInstrInfo::storeRegToStackSlot (restores go through loadRegFromStackSlot). See the frame wiki's PEI 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