public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* optimization/8815: C switch statement produces unnecessary code in some cases
@ 2002-12-04 17:36 squelart
0 siblings, 0 replies; 2+ messages in thread
From: squelart @ 2002-12-04 17:36 UTC (permalink / raw)
To: gcc-gnats
>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:
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: optimization/8815: C switch statement produces unnecessary code in some cases
@ 2002-12-10 14:36 bangerth
0 siblings, 0 replies; 2+ messages in thread
From: bangerth @ 2002-12-10 14:36 UTC (permalink / raw)
To: gcc-bugs, gcc-prs, nobody, squelart
Synopsis: C switch statement produces unnecessary code in some cases
State-Changed-From-To: open->closed
State-Changed-By: bangerth
State-Changed-When: Tue Dec 10 14:36:05 2002
State-Changed-Why:
This is the "switch handles complete range, yet jump to
end of switch is generated" problem. There are more
instances of this problem in the database, so I close this
one.
http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=8815
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2002-12-10 22:36 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-12-04 17:36 optimization/8815: C switch statement produces unnecessary code in some cases squelart
2002-12-10 14:36 bangerth
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).