public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "jakub at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/98334] Failure to optimally optimize add loop to mul
Date: Thu, 17 Dec 2020 10:04:17 +0000	[thread overview]
Message-ID: <bug-98334-4-uxJybVmZrC@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-98334-4@http.gcc.gnu.org/bugzilla/>

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
This boils down to:
int
bar (int x, int y)
{
  return (int) (y - 1U) * x + x;
}

int
baz (int x, int y)
{
  return (y - 1) * x + x;
}

We do optimize the latter, but don't optimize the former into y * x.
For non-wrapping operation in bar that would be an invalid optimization,
as (int) (y - 1U) * x + x is well defined for y == INT_MIN and x == -1:
it is INT_MAX * -1 + -1, i.e. INT_MIN without overflow, but INT_MIN * -1
does overflow.
But we perhaps could and should turn that into just (int) ((y - 1U) * x),
i.e. unsigned multiplication.
We shouldn't do this until very late though, because turning signed arithmetics
into unsigned may disable other optimizations as it has no UB.

Or we could optimize this on RTL, combiner attempts:
Trying 7, 8 -> 9:
    7: {r90:SI=r93:SI-0x1;clobber flags:CC;}
      REG_DEAD r93:SI
      REG_UNUSED flags:CC
    8: {r91:SI=r90:SI*r92:SI;clobber flags:CC;}
      REG_UNUSED flags:CC
      REG_DEAD r90:SI
    9: r89:SI=r91:SI+r92:SI
      REG_DEAD r92:SI
      REG_DEAD r91:SI
Failed to match this instruction:
(set (reg:SI 89)
    (plus:SI (mult:SI (plus:SI (reg:SI 93)
                (const_int -1 [0xffffffffffffffff]))
            (reg:SI 92))
        (reg:SI 92)))
so if we hack up simplify-rtx.c to optimize that to (mult:SI (reg:SI 93)
(reg:SI 92)), it should work.

  reply	other threads:[~2020-12-17 10:04 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-16 18:48 [Bug tree-optimization/98334] New: " gabravier at gmail dot com
2020-12-17 10:04 ` jakub at gcc dot gnu.org [this message]
2020-12-17 10:59 ` [Bug rtl-optimization/98334] " jakub at gcc dot gnu.org
2021-01-04 15:36 ` rguenth at gcc dot gnu.org
2021-01-05  9:59 ` cvs-commit at gcc dot gnu.org
2021-01-05 10:01 ` jakub at gcc dot gnu.org
2022-05-27 20:34 ` roger at nextmovesoftware dot com
2023-02-17 18:35 ` 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-98334-4-uxJybVmZrC@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).