From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 20371 invoked by alias); 5 Dec 2002 01:36:02 -0000 Mailing-List: contact gcc-prs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-prs-owner@gcc.gnu.org Received: (qmail 20338 invoked by uid 71); 5 Dec 2002 01:36:00 -0000 Resent-Date: 5 Dec 2002 01:36:00 -0000 Resent-Message-ID: <20021205013600.20336.qmail@sources.redhat.com> Resent-From: gcc-gnats@gcc.gnu.org (GNATS Filer) Resent-Cc: gcc-prs@gcc.gnu.org, gcc-bugs@gcc.gnu.org Resent-Reply-To: gcc-gnats@gcc.gnu.org, squelart@hotmail.com Received: (qmail 18800 invoked by uid 61); 5 Dec 2002 01:32:32 -0000 Message-Id: <20021205013232.18799.qmail@sources.redhat.com> Date: Wed, 04 Dec 2002 17:36:00 -0000 From: squelart@hotmail.com Reply-To: squelart@hotmail.com To: gcc-gnats@gcc.gnu.org X-Send-Pr-Version: gnatsweb-2.9.3 (1.1.1.1.2.31) Subject: optimization/8815: C switch statement produces unnecessary code in some cases X-SW-Source: 2002-12/txt/msg00260.txt.bz2 List-Id: >Number: 8815 >Category: optimization >Synopsis: C switch statement produces unnecessary code in some cases >Confidential: no >Severity: non-critical >Priority: low >Responsible: unassigned >State: open >Class: change-request >Submitter-Id: net >Arrival-Date: Wed Dec 04 17:36:00 PST 2002 >Closed-Date: >Last-Modified: >Originator: squelart@hotmail.com >Release: gcc-3.2.1 >Organization: >Environment: i686-cygwin, arm-linux >Description: I've got some switch statements like this one: switch(value & 0xFF) { /* all 256 cases... */ }. The assembly code generated on both intel and arm (with -O2 or -O3) is something like: and r,$255 cmp r,$255 if> jump after_switch else jump (table+value*4) Since the value has been ANDed by 0xFF, comparing it to 0xFF is unnecessary and could be optimized out. >How-To-Repeat: The simplest code seems to be: int main(int argc, char** argv) { switch(argc&7) { case 0: return 0; case 1: return 1; case 2: return 2; case 3: return 3; case 4: return 4; case 5: return 5; case 6: return 6; case 7: return 7; } } gcc -S -O2 test.c >Fix: Remove the comparison and jump, when the switch value has been ANDed by a constant, and all cases&constant are handled. Also, with -Wall, the code above produces a "warning: control reaches end of non-void function", which is not true since all cases return (but maybe that's another problem, though it seems related). [Newbie disclaimer: I know _nothink_ about gcc sources, I'm just trying to help by pointing out a possible optimization, I hope it's not too difficult to implement, but my life doesn't depend on it, so please don't answer "DIY"!] >Release-Note: >Audit-Trail: >Unformatted: