From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 2E443385840D; Wed, 26 Apr 2023 18:17:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2E443385840D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682533041; bh=e1WQcfqL7193HHrzuDQgDfTrylIhh1xFQqamgmrQ5aM=; h=From:To:Subject:Date:From; b=ld5jp/PGdX7p/sayMyIM9hZUv0JEzXZtnZxGDjUbe77U2DelEgzv1ji4fmVuEXY6s EO34VXs/vlZKHdk+nNB9rxuco/KYEV0zGSRlxfXu8HydRPiv1pJSwfSSETPFjhmN59 HzUj6CKmpI4jxTVdsEXQNqOAmvZ7yO/woXJ1oAVs= From: "mattiase at acm dot org" To: gcc-bugs@gcc.gnu.org Subject: [Bug middle-end/109637] New: unnecessary range check in complete switch on bitfield Date: Wed, 26 Apr 2023 18:17:20 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: middle-end X-Bugzilla-Version: 13.1.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: mattiase at acm dot org X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D109637 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=