Open
Description
The following Rust code produces five assembly instructions when compiled with optimizations:
pub fn add(left: usize, right: usize) -> usize {
let result = left + right;
if result < 10 {
return 3;
}
return result
}
add x9, x1, x0
mov w8, #3
cmp x9, #10
csel x0, x8, x9, lo
ret
However, line-level DWARF information is lost for the if condition, despite it fairly obviously mapping to the “cmp” and “csel” instructions.
compiler explorer links:
It seems that in the “AArch64 Instruction Selection” pass, the conditional is lowered to:
3:gpr64 = SUBSXri %2:gpr64common, 10, 0, implicit-def $nzcv, debug-location !21; example.rs:0
%4:gpr32 = MOVi32imm 3
%5:gpr64 = SUBREG_TO_REG 0, killed %4:gpr32, %subreg.sub_32
%6:gpr64 = CSELXr killed %5:gpr64, %2:gpr64common, 3, implicit $nzcv, debug-location !21; example.rs:0
which as I understand it, is “subtract 10 from the value, and select based on a flag indicating if the result is 0”.
At some point later, this is optimized to a simple comparison, but the line-level information is lost.