https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103429

            Bug ID: 103429
           Summary: Optimization of Auto-generated condition chain is not
                    giving good lookup tables.
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ed at edwardrosten dot com
  Target Milestone: ---

I've got come generated condition chains (using recursive templates) and am
getting some odd/suboptimal optimization results. Code is provided below and
with a godbolt link.

In the first case (without a force inline), the compiler inlines the functions
but does not perform condition chain optimization. In the second case
(identical code but with force inline), it will optimize condition chains but
only with exactly 5 elements. Otherwise it will end up with an if-else
structure indexing optimized 5 element condition chains, and an if-else chain
for anything spare.

It only attempts the optimization from gcc 11 onwards, I checked on trunk too.


Example:
https://godbolt.org/z/c9xbPqq7r

Here's the code:
template<int I> void f();

constexpr int N=5;

template<int I=0> 
static inline void f_dispatch(int i){
    if constexpr (I == N)
        return;
    else if(i == I)
        f<I>();
    else
        f_dispatch<I+1>(i);
}

template<int I=0> __attribute__((always_inline)) 
static inline void f_dispatch_always_inline(int i){
    if constexpr (I == N)
        return;
    else if(i == I)
        f<I>();
    else
        f_dispatch_always_inline<I+1>(i);
}

void run(int i){
    f_dispatch<>(i);
}

void run_inline(int i){
    f_dispatch_always_inline<>(i);
}

Reply via email to