public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Marc Glisse <marc.glisse@inria.fr>
To: Jakub Jelinek <jakub@redhat.com>
Cc: gcc-patches@gcc.gnu.org, Jonathan Wakely <jwakely@redhat.com>,
	 Richard Biener <rguenther@suse.de>
Subject: Re: [PATCH] phiopt: Optimize (x <=> y) cmp z [PR94589]
Date: Thu, 6 May 2021 21:42:41 +0200 (CEST)	[thread overview]
Message-ID: <4139aa5f-e3c3-c1ce-7fc3-a1e41a4d4c20@hippo.saclay.inria.fr> (raw)
In-Reply-To: <20210506102230.GY1179226@tucnak>

On Thu, 6 May 2021, Jakub Jelinek via Gcc-patches wrote:

> Though, (x&1) == x is equivalent to both (x&~1)==0 and to x < 2U
> and from the latter two it isn't obvious which one is better/more canonical.
> On aarch64 I don't see differences in number of insns nor in their size:
>  10:	13001c00 	sxtb	w0, w0
>  14:	721f781f 	tst	w0, #0xfffffffe
>  18:	1a9f17e0 	cset	w0, eq  // eq = none
>  1c:	d65f03c0 	ret
> vs.
>  20:	12001c00 	and	w0, w0, #0xff
>  24:	7100041f 	cmp	w0, #0x1
>  28:	1a9f87e0 	cset	w0, ls  // ls = plast
>  2c:	d65f03c0 	ret
> On x86_64 same number of insns, but the comparison is shorter (note, the
> spaceship result is a struct with signed char based enum):
>  10:	31 c0                	xor    %eax,%eax
>  12:	81 e7 fe 00 00 00    	and    $0xfe,%edi
>  18:	0f 94 c0             	sete   %al
>  1b:	c3                   	retq
>  1c:	0f 1f 40 00          	nopl   0x0(%rax)
> vs.
>  20:	31 c0                	xor    %eax,%eax
>  22:	40 80 ff 01          	cmp    $0x1,%dil
>  26:	0f 96 c0             	setbe  %al
>  29:	c3                   	retq
> Generally, I'd think that the comparison should be better because it
> will be most common in user code that way and VRP will be able to do the
> best thing for it.

We can probably do it in 2 steps, first something like

(for cmp (eq ne)
  (simplify
   (cmp (bit_and:c @0 @1) @0)
   (cmp (@0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); })))

to get rid of the double use, and then simplify X&C==0 to X<=~C if C is a 
mask 111...000 (I thought we already had a function to detect such masks, 
or the 000...111, but I can't find them anymore).

I agree that the comparison seems preferable, although if X is signed, the 
way GIMPLE represents types will add an inconvenient cast. And I think VRP 
already manages to use the bit test to derive a range.

-- 
Marc Glisse

  reply	other threads:[~2021-05-06 19:42 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-04  7:44 Jakub Jelinek
2021-05-04  9:42 ` Jonathan Wakely
2021-05-04  9:56   ` Jakub Jelinek
2021-05-05 11:39 ` Richard Biener
2021-05-05 13:18   ` [PATCH] phiopt, v2: " Jakub Jelinek
2021-05-05 14:17     ` Richard Biener
2021-05-05 11:45 ` [PATCH] phiopt: " Marc Glisse
2021-05-05 16:52   ` Jakub Jelinek
2021-05-06 10:22     ` Jakub Jelinek
2021-05-06 19:42       ` Marc Glisse [this message]
2021-05-11  7:34         ` [PATCH] match.pd: Optimize (x & y) == x into (x & ~y) == 0 [PR94589] Jakub Jelinek
2021-05-11  8:11           ` Marc Glisse
2021-05-11  8:20           ` Richard Biener
2021-05-14 17:26         ` [PATCH] phiopt: Optimize (x <=> y) cmp z [PR94589] Jakub Jelinek
2021-05-15 10:09           ` Marc Glisse
2021-05-05 15:45 ` Martin Sebor

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=4139aa5f-e3c3-c1ce-7fc3-a1e41a4d4c20@hippo.saclay.inria.fr \
    --to=marc.glisse@inria.fr \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jakub@redhat.com \
    --cc=jwakely@redhat.com \
    --cc=rguenther@suse.de \
    /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).