`switch` lowering generates lookup tables with unnecessarily large elements
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
Instead of generating a LUT with 64-bit elements, LLVM should generate a LUT with 8-bit elements and then zero-extend. This doesn't change the number of instructions required to do the lookup, but does reduce binary size
https://godbolt.org/z/1Mhcoz6bh
```c++
#include
// clang-format off
enum Month {
January, February, March,
April, May, June,
July, August, September,
October, November, December,
};
auto src(Month month) -> std::size_t {
switch (month) {
case January: return 31;
case February: return 31;
case March: return 31;
case April: return 30;
case May: return 31;
case June: return 30;
case July: return 31;
case August: return 31;
case September: return 30;
case October: return 31;
case November: return 30;
case December: return 31;
default: __builtin_unreachable();
}
}
auto tgt(Month month) -> std::size_t {
static const std::uint8_t LUT[12] = {
31, 31, 31, 30,
31, 30, 31, 31,
30, 31, 30, 31,
};
return LUT[month];
}
```
Contributor guide
Research direction
Reproduce the C++ example from the issue using the linked Compiler Explorer example, then inspect LLVM's switch-lowering path and generated assembly. Done means the lookup table uses 8-bit elements, the result is zero-extended to the required size, and the generated binary is smaller without adding lookup instructions.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100