public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/109637] New: unnecessary range check in complete switch on bitfield
@ 2023-04-26 18:17 mattiase at acm dot org
  2023-04-26 18:19 ` [Bug middle-end/109637] " pinskia at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: mattiase at acm dot org @ 2023-04-26 18:17 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 109637
           Summary: unnecessary range check in complete switch on bitfield
           Product: gcc
           Version: 13.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mattiase at acm dot org
  Target Milestone: ---

This fully populated switch still produces some kind of useless range check:

struct S { unsigned x : 2; };
int f (struct S *s) {
    switch(s->x) {
        case 0: return 0;
        case 1: return 1;
        case 2: return 2;
        case 3: return 3;
    }
}

->
        movzbl  (%rdi), %eax
        andl    $3, %eax
        leal    -1(%rax), %edx
        movzbl  %al, %eax
        cmpb    $3, %dl
        movl    $0, %edx
        cmovnb  %edx, %eax
        ret

GCC apparently understands that the switch is complete at some level since
anything after the switch is recognised as dead code, so the range check is a
bit puzzling.

The code is fine if we explicitly mask the switch value as in `switch(s->x &
3)`:

        movzbl  (%rdi), %eax
        andl    $3, %eax
        ret

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2023-08-11  0:39 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-26 18:17 [Bug middle-end/109637] New: unnecessary range check in complete switch on bitfield mattiase at acm dot org
2023-04-26 18:19 ` [Bug middle-end/109637] " pinskia at gcc dot gnu.org
2023-04-26 18:28 ` [Bug tree-optimization/109637] " pinskia at gcc dot gnu.org
2023-04-26 18:52 ` amacleod at redhat dot com
2023-04-26 19:02 ` amacleod at redhat dot com
2023-04-26 19:05 ` pinskia at gcc dot gnu.org
2023-04-26 19:16 ` amacleod at redhat dot com
2023-08-11  0:17 ` pinskia at gcc dot gnu.org
2023-08-11  0:39 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).