Issue 142206
Summary Relax default constraint to build lookup table for switch
Labels new issue
Assignees
Reporter LucasSte
    **Context**

In `SimplifyCFG.cpp`, there is a `SwitchToLookupTable` function which simplifies switch instructions in LLVM-IR. This is very useful for reducing the number of machine instructions in Rust match-statements.

According to the description on top of the function (see below), it only runs when simplifying the initialization of Phi nodes with constant values.

https://github.com/llvm/llvm-project/blob/f7e172da4caeab9d92f6e97501b1a2c561e616c1/llvm/lib/Transforms/Utils/SimplifyCFG.cpp#L6853-L6856

Going further down, it may modify the table index (below) with a subtraction:

https://github.com/llvm/llvm-project/blob/f7e172da4caeab9d92f6e97501b1a2c561e616c1/llvm/lib/Transforms/Utils/SimplifyCFG.cpp#L7013-L7015

And later uses the `SwitchLookupTable` class to create the table:

https://github.com/llvm/llvm-project/blob/f7e172da4caeab9d92f6e97501b1a2c561e616c1/llvm/lib/Transforms/Utils/SimplifyCFG.cpp#L6454

Alongside the `SwitchLookupTable::BuildLookup`:

https://github.com/llvm/llvm-project/blob/f7e172da4caeab9d92f6e97501b1a2c561e616c1/llvm/lib/Transforms/Utils/SimplifyCFG.cpp#L7123

Upon examination of this part of the code, it looks like the switch to lookup table transformation does not emit any indirect branch instructions or jump table instructions.

At the same time, if we track down the condition on whether to run the transformation at the top:

https://github.com/llvm/llvm-project/blob/f7e172da4caeab9d92f6e97501b1a2c561e616c1/llvm/lib/Transforms/Utils/SimplifyCFG.cpp#L6865

Which goes to:

https://github.com/llvm/llvm-project/blob/c5f3018668efea156831feb24843e34e4602a8e6/llvm/lib/Analysis/TargetTransformInfo.cpp#L595

And then to:

https://github.com/llvm/llvm-project/blob/c5f3018668efea156831feb24843e34e4602a8e6/llvm/include/llvm/CodeGen/BasicTTIImpl.h#L606-L610

LLVM requires targets to either implement indirect jumps or jump table specific instructions.

**Issue itself**

I maintain a company specific LLVM target in an LLVM fork, that is derived from the upstream BPF target. We don't support indirect branches or specific jump table instructions. I found that the `switchToLookupTable` transformation is very useful for decreasing code size, however it is gated behind the requirement that targets implement indirect branches, while not emitting any specific instruction for them.

Can we reformulate the standard condition on whether we the pass can run? I'm happy to also open a PR for this, so I'm looking for guidance on the best approach now.

I did some testing with the company specific target relaxing that constraint and found no issue. Additionally, I haven't found any failure in the LLVM test suit (`make check-all`) with the same changes.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to