From: Richard Biener <richard.guenther@gmail.com>
To: Arjun Shankar <arjun@redhat.com>
Cc: GCC Patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH v2] tree-optimization/94899: Remove "+ 0x80000000" in int comparisons
Date: Fri, 4 Feb 2022 12:14:33 +0100 [thread overview]
Message-ID: <CAFiYyc0GQBPTO9h0-c0Lfs7NuFwvq3mmYbO3O7kD85qSOyfiLg@mail.gmail.com> (raw)
In-Reply-To: <CAG_osaYoeMU_8Ntx2t9Hr1NMrAEWwkmRqfZ8t9RdXY=zOW_spA@mail.gmail.com>
On Thu, Feb 3, 2022 at 4:50 AM Arjun Shankar <arjun@redhat.com> wrote:
>
> Expressions of the form "X + CST < Y + CST" where:
>
> * CST is an unsigned integer constant with only the MSB set, and
> * X and Y's types have integer conversion ranks <= CST's
>
> can be simplified to "(signed) X < (signed) Y".
>
> This is because, assuming a 32-bit signed numbers,
> (unsigned) INT_MIN + 0x80000000 is 0, and
> (unsigned) INT_MAX + 0x80000000 is UINT_MAX.
>
> i.e. the result increases monotonically with signed input.
>
> This means:
> ((signed) X < (signed) Y) iff (X + 0x80000000 < Y + 0x80000000)
+ (op (plus:c INTEGER_CST@0 @1) (plus:c INTEGER_CST@0 @2))
INTEGER_CST are put last by canonicalization so you should write
(op (plus @1 INTEGER_CST@0) (plus @2 INTEGER_CST@0))
and thus omit the :c.
+#define MAGIC 0x80000000
I _think_ writing the constant this way requires
/* { dg-require-effective-target int32plus } */
as on a target where int is 16 bits it possibly yields a diagnostic?
I've been using explicit int32_t types w/o such effective target
in the past but I'm not sure how to write an explicit precision
constant to avoid diagnostics.
Note I didn't actually check we get a diagnostic ...
Slapping on the dg-requires-effective-target should be safe though.
Otherwise looks good now - note this has to wait for stage1 of GCC 13,
so make sure to ping then or commit then in case you have git access.
Thanks,
Richard.
> gcc/
> * match.pd (X + C < Y + C -> (signed) X < (signed) Y, if C is
> 0x80000000): New simplification.
> gcc/testsuite/
> * gcc.dg/pr94899.c: New test.
> ---
> gcc/match.pd | 13 +++++++++
> gcc/testsuite/gcc.dg/pr94899.c | 48 ++++++++++++++++++++++++++++++++++
> 2 files changed, 61 insertions(+)
> create mode 100644 gcc/testsuite/gcc.dg/pr94899.c
> ---
> v1: https://gcc.gnu.org/pipermail/gcc-patches/2022-February/589557.html
>
> Notes on v2, based on Richard's review comments:
>
> 1. I removed matching on "convert", and therefore also replaced the
> removal of convert upon simplification with an explicit cast to
> signed. I originally thought this simplification only applies to
> signed operands that have been cast to unsigned, but thinking about
> it, it became clear that they do not necessarily have to be signed
> originally. The simplification is now a bit more general.
>
> 2. Removed checks for operands' types as it seems to be unnecessary. I
> hope this is correct.
>
> 3. Added unsigned types and mismatched sizes of operands to the test.
> These are now simplified.
next prev parent reply other threads:[~2022-02-04 11:14 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-02-01 4:53 [PATCH] " Arjun Shankar
2022-02-01 7:18 ` Richard Biener
2022-02-01 15:21 ` Arjun Shankar
2022-02-02 9:20 ` Richard Biener
2022-02-02 15:55 ` Arjun Shankar
2022-02-03 3:50 ` [PATCH v2] " Arjun Shankar
2022-02-04 11:14 ` Richard Biener [this message]
2022-02-04 11:50 ` Jakub Jelinek
2022-02-04 13:21 ` Richard Biener
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=CAFiYyc0GQBPTO9h0-c0Lfs7NuFwvq3mmYbO3O7kD85qSOyfiLg@mail.gmail.com \
--to=richard.guenther@gmail.com \
--cc=arjun@redhat.com \
--cc=gcc-patches@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).