https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122226
Bug ID: 122226
Summary: LICM missing opportunity
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: matt at godbolt dot org
Target Milestone: ---
Consider the following (contrived code):
```
#include <utility>
#include <span>
enum class RangeType {
Capitals, Numerics
};
[[gnu::noinline]] std::pair<int, int> get_range(RangeType rt) {
if (rt == RangeType::Capitals) return {'A', 'Z'};
return {'0', '9'};
}
std::size_t count_in_range(
RangeType type,
std::span<int> string) {
std::size_t num = 0;
for (const auto c : string) {
if (c >= get_range(type).first
&& c <= get_range(type).second) {
++num;
}
}
return num;
}
```
(originally used strings/chars etc but to avoid any aliasing issues, moved to
int)
Compiler Explorer link: https://godbolt.org/z/T4GocTjGP
Clang will LICM-move out the `get_range` call (and deduplicate it) at -O1 and
above. I can't find a way to get GCC to move the get_range() calls out of the
loop (and deduplicate them).
Am I missing something, or is this a missed opportunity? Thanks in advance!