Repository navigation
SelectionDAG 09.3 control flow calls
Relevant source files
The following files were used as context for generating this wiki page:
This page shows real results for branches, select, calls and returns on AArch64, and the lowering functions that produce them.
| IR | MIR | Assembly |
|---|---|---|
br (icmp eq i32 %x, 0) |
CBZW %0, %bb.2 then B %bb.3
|
cbz w0, .LBB0_2 |
br (icmp slt i32 %x, %y) |
%2 = SUBSWrr %0, %1, implicit-def $nzcv, Bcc 11, %bb.2, implicit $nzcv, B %bb.3
|
cmp w0, w1 b.ge .LBB2_2 (the layout pass inverted the condition) |
select (icmp slt %a, %b), %a, %b |
SUBSWrr %0, %1 and CSELWr %0, %1, 11, implicit $nzcv
|
cmp w0, w1 csel w0, w0, w1, lt
|
select (icmp sgt %x, 10), 5, 6 |
SUBSWri %0, 10, MOVi32imm 5, CSINCWr %2, %2, 12
|
mov w8, #5 cmp w0, #10 cinc w0, w8, le
|
br (icmp eq %x, 0) with trivial arms |
SimplifyCFG turned it into a select before ISel, so no branch: SUBSWri %0, 0, CSINCWr %1, %1, 0
|
cmp w0, #0 cinc w0, w1, ne
|
The 11, 12 and 0 in Bcc and CSEL are AArch64 condition codes (0 = EQ, 1 = NE, 10 = GE, 11 = LT, 12 = GT, 13 = LE). The unconditional B %bb.3 that follows a Bcc is the DAG's fall-through ISD::BR; branch folding and block placement remove it when the target is the next block.
LowerBR_CC emits AArch64ISD::CBZ, CBNZ, TBZ or TBNZ for comparisons with zero and single-bit tests, and AArch64ISD::BRCOND with a condition code and flags from SUBS otherwise.
flowchart TD
B["ISD::BRCOND or BR_CC<br/>Custom on AArch64"] --> LB["LowerBR_CC"]
LB --> Z{"Compare with zero<br/>or single-bit test?"}
Z -- yes --> CB["AArch64ISD::CBZ, CBNZ,<br/>TBZ or TBNZ"]
Z -- no --> SU["AArch64ISD::SUBS<br/>sets NZCV"]
SU --> BC["AArch64ISD::BRCOND<br/>condition code and flags"]
CB --> S1["Selected: CBZW, CBNZW, TBZ"]
BC --> S2["Selected: Bcc"]
Sources: llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:11803-11990, llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:12744-13022, llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:13078-13156, llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp:2840-2926
declare i32 @g(i32)
define i32 @call(i32 %x) {
%r = call i32 @g(i32 %x)
%s = add i32 %r, 1
ret i32 %s
}visitCall calls LowerCallTo, which calls AArch64TargetLowering::LowerCall. That function assigns locations with the AAPCS rules (AArch64CallingConvention.td), emits CALLSEQ_START, one CopyToReg per register argument (glued to the call), stores for stack arguments, the AArch64ISD::CALL node with the callee and the register mask csr_aarch64_aapcs, CALLSEQ_END, and a CopyFromReg for each result. After selection and emission:
%0:gpr32 = COPY $w0
ADJCALLSTACKDOWN 0, 0, implicit-def dead $sp, implicit $sp
$w0 = COPY %0
BL @g, csr_aarch64_aapcs, implicit-def dead $lr, implicit $sp, implicit $w0, implicit-def $sp, implicit-def $w0
ADJCALLSTACKUP 0, 0, implicit-def dead $sp, implicit $sp
%1:gpr32sp = COPY $w0
%2:gpr32sp = ADDWri %1, 1, 0
$w0 = COPY %2
RET_ReallyLR implicit $w0
ADJCALLSTACKDOWN and UP are CALLSEQ_START and END: they bracket the argument setup and are rewritten by prologue/epilogue insertion once the frame size is known. BL implicitly defines $lr, which is why the function must save x30; that is why the assembly begins with str x30, [sp, #-16]! and ends with ldr x30, [sp], #16, even though nothing in the IR mentions it. The csr_aarch64_aapcs mask tells later passes which registers survive the call.
The return is CopyToReg $w0 followed by AArch64ISD::RET_GLUE, glued so the copy cannot be separated from the return. RET_ReallyLR is defined as Pseudo<(outs), (ins), [(AArch64retglue)]>, so selection turns RET_GLUE into it, and it is expanded to ret at the end.
Sources: llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:10176-10934, llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:10947-11090, llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp:9316-9437, llvm/lib/Target/AArch64/AArch64InstrInfo.td:3703
- 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