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

            Bug ID: 120278
           Summary: [9/10/11/12/13/14/15 Regression] Switch expansion
                    generates extra compares with -fno-jump-tables
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: selectstriker2 at protonmail dot com
  Target Milestone: ---

Created attachment 61423
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=61423&action=edit
Simple source file

Starting with GCC9, switch expansion to decision tree changed and has extra
comparison instructions that will only ever evaluate to false because the true
case has already been eliminated above.

This was noted when switching to the arm-none-eabi C compiler based on GCC9,
and exists in the x86_64 C compiler as well.

Godbolt example : https://godbolt.org/z/f1fzMhbEx

gcc main.c -fno-jump-tables

Correct in GCC8

.L10:
  cmpl $2, -4(%rbp)
  je .L2
  cmpl $2, -4(%rbp)
  jg .L3
  cmpl $0, -4(%rbp)
  je .L4
  cmpl $1, -4(%rbp)
  je .L5
  jmp .L6
.L3:
  cmpl $3, -4(%rbp)
  je .L7
  cmpl $4, -4(%rbp)
  je .L8
  jmp .L6


Extra/incorrect comparisons started in GCC9

.L9:
  cmpl $4, -4(%rbp)
  je .L2
  cmpl $4, -4(%rbp)
  jg .L3
  cmpl $3, -4(%rbp)
  je .L4
  cmpl $3, -4(%rbp)
  jg .L3            - already checked for ==4, >4, ==3 so >3 will always be
false
  cmpl $2, -4(%rbp)
  je .L5
  cmpl $2, -4(%rbp)
  jg .L3            - already checked for ==4, >4, ==3, ==2 so >2 will always
be false
  cmpl $0, -4(%rbp)
  je .L6
  cmpl $1, -4(%rbp)
  je .L7
  jmp .L3

Reply via email to