Function with multi return path?

Hi folks,

I’m new to LLVM and non-expert in hardware architecture, so my question might be dumb.

I’m looking for the possibility to define/call a function with multi return path, it might look like this:

; Define a function with 1 alternate return path
define i32 @f(i1 %i) fork 1 {
br i1 %i, label %noraml, label %alternate
setpath ret i32 42 ; take normal return path
br %cleanup
setpath fork 0 ; take the alternate return path
br %cleanup
cleanup: ; preds = %noraml, %alternate
unwind ; return to the caller

And at the call side:

%ret = call @f(i1 %i) fork [%otherwise]

Ideally, the callee sets the return address so it returns directly to the desired location in the caller w/o the caller taking extra switch.

The idea is to implement some non-local control flow w/o extra overhead at each call side.

I know LLVM doesn’t have something like this currently, but is it possible to implement?

The closest thing LLVM has to this is the invoke instruction used for exception handling. Using it requires reusing or building a lot of EH runtime support, though. invoke only supports a single alternative return destination (the landingpad), but you can use the selector value to switch over multiple language level alternative return destinations.