Skip to content

SelectionDAG 09.3 control flow calls

Mugundan edited this page Nov 27, 2026 · 1 revision

Control Flow and Calls

Relevant source files

The following files were used as context for generating this wiki page:

A conditional branch is `ISD::BRCOND` (or `BR_CC` after combining). AArch64 marks both as Custom, so `LowerBR_CC` chooses between a compare-and-branch-on-zero form and a flag-setting compare plus `b.cond`. Calls and returns are lowered by `LowerCall` and `LowerReturn`.

Purpose and Scope

This page shows real results for branches, select, calls and returns on AArch64, and the lowering functions that produce them.

Branches and Select

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"]
Loading

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

Calls and Returns

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

Start here

Computer Organization & Architecture

Hexagon ISA

Operating Systems

C++ for LLVM Development

LLVM IR

SelectionDAG

Backend Assignments

Target Bring-up

Calling Conventions

Frame Lowering

MC Layer

Register Allocation

Scheduling

Clone this wiki locally