public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
From: squelart@hotmail.com
To: gcc-gnats@gcc.gnu.org
Subject: optimization/8815: C switch statement produces unnecessary code in some cases
Date: Wed, 04 Dec 2002 17:36:00 -0000	[thread overview]
Message-ID: <20021205013232.18799.qmail@sources.redhat.com> (raw)


>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:


             reply	other threads:[~2002-12-05  1:36 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-12-04 17:36 squelart [this message]
2002-12-10 14:36 bangerth

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20021205013232.18799.qmail@sources.redhat.com \
    --to=squelart@hotmail.com \
    --cc=gcc-gnats@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).