public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
From: Kevin Ryde <user42@zip.com.au>
To: gcc-gnats@gcc.gnu.org
Subject: optimization/3756: gcc wishlist: arithmetic right shift for ternary operator
Date: Fri, 20 Jul 2001 18:26:00 -0000	[thread overview]
Message-ID: <87hew7krlk.fsf@zip.com.au> (raw)

>Number:         3756
>Category:       optimization
>Synopsis:       gcc wishlist: arithmetic right shift for ternary operator
>Confidential:   yes
>Severity:       non-critical
>Priority:       low
>Responsible:    unassigned
>State:          open
>Class:          pessimizes-code
>Submitter-Id:   net
>Arrival-Date:   Fri Jul 20 18:26:01 PDT 2001
>Closed-Date:
>Last-Modified:
>Originator:     
>Release:        3.0 (Debian) (Debian testing/unstable)
>Organization:
>Environment:
System: Linux blah 2.2.15 #1 Tue Apr 25 17:13:48 EST 2000 i586 unknown
Architecture: i586
	<machine, os, target, libraries (multiple lines)>
host: i386-pc-linux-gnu
build: i386-pc-linux-gnu
target: i386-pc-linux-gnu
configured with: ../src/configure -v --enable-languages=c,c++,java,f77,proto,objc --prefix=/usr --infodir=/share/info --mandir=/share/man --enable-shared --with-gnu-as --with-gnu-ld --with-system-zlib --enable-long-long --enable-nls --without-included-gettext --disable-checking --enable-threads=posix --enable-java-gc=boehm --with-cpp-install-dir=bin --enable-objc-gc i386-linux

>Description:
It seems that, on i386 at least, gcc misses the opportunity to use an
arithmetic right shift when compiling a ternary expression like "n >=
0 ? a : b", for constant a and b.

>How-To-Repeat:
An example I first struck was

        int
        pn (int n)
        {
          return (n >= 0 ? 1 : -1);
        }

compiled with

        gcc-3.0 -O9 -fomit-frame-pointer -S pn.c

giving

	.file	"pn.c"
	.text
	.align 4
.globl pn
	.type	pn,@function
pn:
	movl	4(%esp), %eax
	notl	%eax
	shrl	$31, %eax
	leal	-1(%eax,%eax), %eax
	ret
.Lfe1:
	.size	pn,.Lfe1-pn
	.ident	"GCC: (GNU) 3.0 (Debian)"

I think instead of notl/shrl/leal it could do

        sarl    $31, %eax
        orl     $1, %eax

If I'm not mistaken "or" and "and" get used for that sort of thing
already, it's just the sarl that's missed.

A more general example

        int
        sel (int n)
        {
          return (n >= 0 ? 60 : 100);
        }

gives a slightly different form

        shrl    $31, %eax
        decl    %eax
        andl    $-40, %eax
        addl    $100, %eax

Again I think again the shrl+decl could be a sarl (and the constants
adjusted accordingly).
>Fix:
>Release-Note:
>Audit-Trail:
>Unformatted:


             reply	other threads:[~2001-07-20 18:26 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-07-20 18:26 Kevin Ryde [this message]
2002-04-22 21:02 billingd

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=87hew7krlk.fsf@zip.com.au \
    --to=user42@zip.com.au \
    --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).