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

            Bug ID: 86707
           Summary: Missed optimization: optimizing set of if statements
           Product: gcc
           Version: tree-ssa
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: zamazan4ik at tut dot by
  Target Milestone: ---

gcc(trunk) with -O3 -std=c++17 for this code:

unsigned int foo(unsigned int x)
{
    if(x % 2 == 0)
    {
        return x * 2;
    }
    if(x % 4 == 0)
    {
        return x * 4;
    }
    if(x % 8 == 0)
    {
        return x * 8;
    }
    if(x % 16 == 0)
    {
        return x * 16;
    }
    if(x % 32 == 0)
    {
        return x * 32;
    }
    return 100;
}

generates this:

foo(unsigned int):
  test dil, 1
  je .L9
  test dil, 3
  je .L10
  test dil, 7
  je .L11
  mov eax, edi
  test dil, 15
  je .L12
  sal eax, 5
  and edi, 31
  mov edi, 100
  cmovne eax, edi
  ret
.L10:
  lea eax, [0+rdi*4]
  ret
.L9:
  lea eax, [rdi+rdi]
  ret
.L12:
  sal eax, 4
  ret
.L11:
  lea eax, [0+rdi*8]
  ret


As you see, generated code is suboptimal: here we can leave only first 'if'
statement and otherwise return 100.

Reply via email to