Open
Description
#![feature(core_intrinsics)]
extern crate core;
use core::intrinsics::unlikely;
#[inline(always)]
fn test(x: bool) -> bool {
unsafe {
unlikely(x)
}
}
pub fn foo(x: u32) -> u32 {
if test(x == 0) { 1 } else { x * 2 }
}
pub fn foo2(x: u32) -> u32 {
unsafe {
let c = unlikely(x == 0);
if c { 1 } else { x * 2 }
}
}
pub fn bar(x: u32) -> u32 {
if x == 0 { 1 } else { x * 2 }
}
foo2
generates:
push rbp
mov rbp, rsp
test edi, edi
je .LBB1_1
add edi, edi
.LBB1_3:
mov eax, edi
pop rbp
ret
.LBB1_1:
mov edi, 1
jmp .LBB1_3
but foo
generates the same assembly as bar
instead of the same assembly as foo2
:
push rbp
mov rbp, rsp
lea ecx, [rdi + rdi]
test edi, edi
mov eax, 1
cmovne eax, ecx
pop rbp
ret