Skip to content

likely/unlikely intrinsics fail to propagate through inline functions #45440

Open
@gnzlbg

Description

@gnzlbg

See it live:

#![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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions