Jump tables in LLVM IR


Is there a way to represent jump tables in LLVM IR such that the size
or contents of the jump table is not known at compile time?

I.e. not a select statement.
Kind of like: indirectbr ptr %Addr, [ label %bb1, label %bb2, label %bb3 ]
but without the need for any of the labels ?

Kind Regards


The labels are necessary for SSA form, among other things: the lifetime of variables can only be understood if the possible branch targets are known.

Note that the list of labels of the indirectbr could be a strict superset of the labels that can actually be reached – perhaps that helps.

If you don’t care about SSA variables across the jump, then perhaps whatever you want to achieve can be achieved by splitting up your code into multiple functions and using musttail indirect calls instead of indirect branches.

So, I guess this is an example of where LLVM IR with its “PHI Nodes” ends up being much more complicated than MLIR “Block Arguments”.

In MLIR indirect_br could just contain one set of arguments, as the same set of arguments would be passed to every destination of the jump.

LLVM IR needs all the destinations, so that the PHI nodes at all the destination sites is correct.

The two are really isomorphic: you can translate between PHI nodes and BB arguments with essentially no information loss.

If you did this in MLIR without listing the possible jump targets, any subsequent liveness analysis etc. would have to make very conservative assumptions. That would be equivalent to just adding all basic blocks to the successor list of an indirectbr. So you’d really want a list of possible successors in MLIR anyway.