I'm toying with some algorithm in Rust (though the language doesn't really matter for my question). Consider the code:
#[no_mangle]
pub fn test(x: f32) -> f32 {
let m = 0.;
x + m
}
fn main() {
test(2.);
}
It produces the following LLVM IR and corresponding x86_64 asm (optimizations enabled):
;; LLVM IR
define float @test(float %x) unnamed_addr #0 {
start:
%0 = fadd float %x, 0.000000e+00
ret float %0
}
;; x86_64
; test:
xorps xmm1, xmm1
addss xmm0, xmm1
ret
If I change let m = 0.;
to let m = -0.;
the floating point addition is optimized away:
;; LLVM IR
define float @test(float returned %x) unnamed_addr #0 {
start:
ret float %x
}
;; x86_64
; fn disappears entirely
fn(-0.0)
. – Gera