public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "drepper.fsp+rhbz at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/103857] New: implement ternary without jump (and comparison)
Date: Wed, 29 Dec 2021 12:16:02 +0000	[thread overview]
Message-ID: <bug-103857-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103857

            Bug ID: 103857
           Summary: implement ternary without jump (and comparison)
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: drepper.fsp+rhbz at gmail dot com
  Target Milestone: ---

This test case is derived from actual code and I expect it to be not uncommon
in general.  Maybe not exactly in this form but perhaps the matcher can catch a
few more cases.

Take this code:

extern void g(int);
void f1(int* a, int b, int c)
{
  for (unsigned i = 0; i < 100; ++i)
    g(a[i] == b ? c : b);
}
void f2(int* a)
{
  for (unsigned i = 0; i < 100; ++i)
    g(a[i] == 42 ? 10 : 42);
}


The function 'g' is called in each loop with one of two values which is the
opposite from the one that is match in the condition of the ternary operation. 
This allows the respective loops to be rewritten as

  for (unsigned i = 0; i < 100; ++i)
    g(a[i] ^ c ^ b);

and

  for (unsigned i = 0; i < 100; ++i)
    g(a[i] ^ 10 ^ 42);

In the former case the c ^ b operation can be hoisted.

This should be faster and smaller in pretty much all situations and on all
platforms.

             reply	other threads:[~2021-12-29 12:16 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-29 12:16 drepper.fsp+rhbz at gmail dot com [this message]
2021-12-29 13:03 ` [Bug tree-optimization/103857] " jakub at gcc dot gnu.org
2021-12-29 13:24 ` drepper.fsp+rhbz at gmail dot com
2021-12-29 20:23 ` pinskia at gcc dot gnu.org
2021-12-29 20:26 ` pinskia at gcc dot gnu.org

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=bug-103857-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@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).