Skip to content

Suboptimal codegen for tail call followed by unreachable #53118

@hyeongyukim

Description

@hyeongyukim

Suboptimal code for tail call followed by unreachable

declare void @callee(i64)

define void @endswith_unreachable(i64 %0) {    
    tail call void @callee(i64 %0)
    unreachable
}

define void @endswith_return(i64 %0) {    
    tail call void @callee(i64 %0)
    ret void
}

This IR is compiled as below.
https://godbolt.org/z/G9jrEzYoW

endswith_unreachable:                   # @endswith_unreachable
        push    rax
        call    callee@PLT
endswith_return:                        # @endswith_return
        jmp     callee@PLT                      # TAILCALL

The function ends with tail call followed by ret void is lowered into jmp.
But, when the function ends with unreachable, a call is created rather than a tail call.
I suggest changing from call to tail call even if the function ends with unreachable.

I found this issue while resolving the objtool's (linux kernel's tool) warning for D105169.
If you want to check the original problem, you can find the reduced version of the kernel code at https://godbolt.org/z/sYxTsnh5K and generated IR code containing unreachable at https://alive2.llvm.org/ce/z/szhgAB.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions