public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Richard Biener <richard.guenther@gmail.com>
To: "Hurugalawadi, Naveen" <Naveen.Hurugalawadi@caviumnetworks.com>
Cc: "gcc-patches@gcc.gnu.org" <gcc-patches@gcc.gnu.org>
Subject: Re: Move some bit and binary optimizations in simplify and match
Date: Mon, 12 Oct 2015 13:11:00 -0000	[thread overview]
Message-ID: <CAFiYyc12AuTFeiB5WkQEdzPf_3vSaQyV6UdWsXNSYP8H+91p_g@mail.gmail.com> (raw)
In-Reply-To: <SN2PR0701MB10245F3B91BE5291AD40C1848E310@SN2PR0701MB1024.namprd07.prod.outlook.com>

On Mon, Oct 12, 2015 at 12:22 PM, Hurugalawadi, Naveen
<Naveen.Hurugalawadi@caviumnetworks.com> wrote:
> Hi Richard,
>
> Thanks for your review and useful comments.
> I will  move the future optimization patterns with all the conditions
> present in fold-const or builtins file as per your suggestions.
>
> Please find attached the patch as per your comments.
> Please review the patch and let me know if any further modifications
> are required.

+/* Fold X + (X / CST) * -CST to X % CST.  */
+(simplify
+ (plus (convert1? @0) (convert2? (mult (trunc_div @0 INTEGER_CST@1)
INTEGER_CST@2)))
+  (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
+       && wi::add (@1, @2) == 0)

when you use convert? to mimic fold-const.c behavior you have to add

        && tree_nop_conversion_p (type, TREE_TYPE (@0))

note that in this case only both or no conversion can occur so please
use convert? in both places.

+   (trunc_mod (convert @0) (convert @1))))

This applies to other uses of convert[12]?, too.

As said for the above pattern fold-const.c also handled X + (X / A) * (-A) with
A not being a constant.  Unfortunately predicate syntax can't capture
both -A and -CST but one can use

(match (xdivamulminusa @X @A)
 (mult (trunc_div @X @A) (negate @X)))
(match (xdivamulminusa @X @A)
 (mult (trunc_div @X INTEGER_CST@A) INTEGER_CST@0)
 (if (wi::add (@A, @0) == 0)))

and then

(simplify
  (plus (convert? @0) (convert? (xdivamulminusa @0 @1)))
  (if (...)
   (trunc_mod (convert @0) (convert @1))))

to avoid duplicating the pattern (though that might be more readable
in this case...)

+/* Fold (A & ~B) - (A & B) into (A ^ B) - B.  */
+(simplify
+ (minus (bit_and:s @0 (bit_not @1)) (bit_and:s @0 @2))
+  (if (! FLOAT_TYPE_P (type)
+       && wi::eq_p (@1, @2))
+   (minus (bit_xor @0 @1) @1)))

you can't simply use wi::eq_p on random trees.  Same solution like above
can be used (or pattern duplication).

+/* Fold (a * (1 << b)) into (a << b)  */
+(simplify
+ (mult:c (convert1? @0) (convert2? (lshift integer_onep@1 @2)))
+  (if (! FLOAT_TYPE_P (type))
+   (lshift (convert @0) (convert @2))))

the conversion on @0 isn't interesting to capture, only that on
the lshift is.

+/* Fold (C1/X)*C2 into (C1*C2)/X.  */
+(simplify
+ (mult (rdiv REAL_CST@0 @1) REAL_CST@2)
+  (with
+   { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
+  (if (tem && FLOAT_TYPE_P (type)
+       && flag_associative_math)
+   (rdiv (mult @0 @2) @1))))

you comuted 'tem', so use it:

    (rdiv { tem; } @1))))

The FLOAT_TYPE_P check is redundant I think and the flag_associative_math
check should be done before computing tem.

+/* Simplify (X & ~Y) | (~X & Y) is X ^ Y.  */
+(simplify
+ (bit_ior (bit_and:s @0 (bit_not @1)) (bit_and:s (bit_not @2) @3))
+  (if (wi::eq_p (@0, @2)
+       && wi::eq_p (@1, @3))
+   (bit_xor @0 @3)))

See above for handling constants and using wi::eq_p.  Looks like I
really need to make 'match' handle these kind of things.

+/* Simplify ~X & X as zero.  */
+(simplify
+ (bit_and:c (convert? @0) (convert? (bit_not @0)))
+  (convert { build_zero_cst (TREE_TYPE (@0)); }))

please simplify to

   { build_zero_cst (type); }

directly.

+/* (-A) * (-B) -> A * B  */
+(simplify
+ (mult:c (convert? (negate @0)) (convert? (negate @1)))
+  (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
+       || (GENERIC && type == TREE_TYPE (@0)))
+   (mult (convert @0) (convert @1))))

note that fold-const.c handled multiple ways of negation thus please
use the existing negate_expr_p 'match' like

(simplify
  (mult:c (convert? (negate @0)) (convert? negate_expr_p@1))
  (if ...
    (mult (convert @0) (convert (negate @1)))

also use tree_nop_conversion_p, not the GIMPLE/GENERIC variants.

Thanks,
Richard.


> The last pattern has been removed due to the discussions over it
> and a regression it caused.
> ================================================
> +/* Fold X & (X ^ Y) as X & ~Y.  */
> +(simplify
> + (bit_and:c (convert? @0) (convert? (bit_xor:c @0 @1)))
> +  (bit_and (convert @0) (convert (bit_not @1))))
> ================================================
>
> FAIL: gcc.dg/tree-ssa/vrp47.c scan-tree-dump-times vrp2 " & 1;" 0
> FAIL: gcc.dg/tree-ssa/vrp59.c scan-tree-dump-not vrp1 " & 3;"
>
> Thanks,
> Naveen

  parent reply	other threads:[~2015-10-12 13:11 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-07  9:54 Hurugalawadi, Naveen
2015-10-08 13:39 ` Richard Biener
2015-10-12 10:22   ` Hurugalawadi, Naveen
2015-10-12 12:49     ` Marc Glisse
2015-10-12 13:11     ` Richard Biener [this message]
2015-10-13 10:52       ` Hurugalawadi, Naveen
2015-10-13 11:38         ` Marc Glisse
2015-10-13 11:57         ` Richard Biener
2015-10-13 12:18           ` Marc Glisse
2015-10-13 12:50             ` Richard Biener
2015-10-14  5:13               ` Hurugalawadi, Naveen
2015-10-14  5:40                 ` Marc Glisse
2015-10-14 10:09                   ` Richard Biener
2015-10-14 10:45                     ` Marc Glisse
2015-10-14 10:53                       ` Richard Biener
2015-10-14 11:38                         ` Marc Glisse
2015-10-15  6:11                           ` Hurugalawadi, Naveen
2015-10-15 12:38                             ` Richard Biener
2015-10-16 10:30                               ` Hurugalawadi, Naveen
2015-10-16 11:05                                 ` Marc Glisse
2015-10-19 11:14                                   ` Hurugalawadi, Naveen
2015-10-19 13:04                                     ` Richard Biener
2015-10-19 11:22                                   ` Hurugalawadi, Naveen
2015-10-19 11:42                                     ` Marc Glisse
2015-10-20  6:48                                       ` Hurugalawadi, Naveen
2015-10-20 12:13                                         ` Richard Biener
2015-10-21  4:05                                           ` Hurugalawadi, Naveen
2015-10-21  7:26                                           ` Marc Glisse
2015-10-21  9:49                                             ` Richard Biener
2015-10-23  5:11                                               ` Hurugalawadi, Naveen
2015-10-23  9:07                                                 ` Richard Biener
2015-10-24 21:37                                                 ` Marc Glisse
2015-10-26  9:29                                                   ` Richard Biener
2015-10-26  9:33                                                     ` Richard Biener
2015-10-08 16:58 ` Bernd Schmidt
2015-10-08 18:03   ` Joseph Myers
2015-10-08 18:15     ` Bernd Schmidt
2015-10-09  9:32       ` 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=CAFiYyc12AuTFeiB5WkQEdzPf_3vSaQyV6UdWsXNSYP8H+91p_g@mail.gmail.com \
    --to=richard.guenther@gmail.com \
    --cc=Naveen.Hurugalawadi@caviumnetworks.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).