This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.
Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
---|---|---|
Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |
Other format: | [Raw text] |
>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:
Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
---|---|---|
Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |