public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Zhongyunde <zhongyunde@huawei.com>
To: Andrew Pinski <pinskia@gcc.gnu.org>
Cc: "hongtao.liu@intel.com" <hongtao.liu@intel.com>,
	"gcc-patches@gcc.gnu.org" <gcc-patches@gcc.gnu.org>,
	"Zhangwen(Esan)" <zwzhangwen.zhang@huawei.com>,
	"Weiwei (weiwei, Compiler)" <weiwei64@huawei.com>,
	"zhong_1985624@163.com" <zhong_1985624@163.com>
Subject: RE: [PATCH] [PHIOPT] Add A ? B + CST : B match and simplify optimizations
Date: Sat, 5 Nov 2022 09:03:10 +0000	[thread overview]
Message-ID: <7b6d19fcef784a019c89e77c9917c5a8@huawei.com> (raw)
In-Reply-To: <CA+=Sn1kwT-KE5Q53xk8ZEGLnaVk_vwD7iaCtAEZSac63Z=-SOA@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1343 bytes --]


> -----Original Message-----
> From: Andrew Pinski [mailto:pinskia@gcc.gnu.org]
> Sent: Saturday, November 5, 2022 2:34 PM
> To: Zhongyunde <zhongyunde@huawei.com>
> Cc: hongtao.liu@intel.com; gcc-patches@gcc.gnu.org; Zhangwen(Esan)
> <zwzhangwen.zhang@huawei.com>; Weiwei (weiwei, Compiler)
> <weiwei64@huawei.com>; zhong_1985624@163.com
> Subject: Re: [PATCH] [PHIOPT] Add A ? B + CST : B match and simplify
> optimizations
> 
> On Fri, Nov 4, 2022 at 11:17 PM Zhongyunde <zhongyunde@huawei.com>
> wrote:
> >
> > hi,
> >   This patch is try to fix the issue
> > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107190,
> > would you like to give me some suggestion, thanks.
> 
> This seems like a "simplified" version of
> https://gcc.gnu.org/pipermail/gcc-patches/2021-November/584411.html
> which just handles power of 2 constants where we know the cond will be
> removed.
> We could do even more "simplified" of 1 if needed really.
> What is the IR before PHI-OPT? Is it just + 1?

Thanks for your attention. It is + 4294967296 before PHI-OPT  (See detail https://gcc.godbolt.org/z/6zEc6ja1z)
So we should keep matching the power of 2 constants ?

> Also your pattern can be simplified to use integer_pow2p in the match part
> instead of INTEGER_CST.
> 
Apply your comment, thanks

> Thanks,
> Andrew



[-- Attachment #2: 0001-PHIOPT-Add-A-B-op-CST-B-match-and-simplify-optimizat.patch --]
[-- Type: application/octet-stream, Size: 3072 bytes --]

From 1dfbda734390d8398a12a455028149b058076a51 Mon Sep 17 00:00:00 2001
From: zhongyunde <zhongyunde@huawei.com>
Date: Sat, 5 Nov 2022 13:22:33 +0800
Subject: [PATCH] [PHIOPT] Add A ? B op CST : B match and simplify
 optimizations

    Refer to commit b6bdd7a4, use pattern match to simple
    A ? B op CST : B (where CST is power of 2) simplifications.
    Fixes https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107190

    gcc/
            * match.pd (A ? B op CST : B): Add simplifcations for A ? B op POW2 : B

    gcc/testsuite/
            * gcc.dg/pr107190.c: New test.
---
 gcc/match.pd                    | 21 +++++++++++++++++++++
 gcc/testsuite/gcc.dg/pr107190.c | 27 +++++++++++++++++++++++++++
 2 files changed, 48 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/pr107190.c

diff --git a/gcc/match.pd b/gcc/match.pd
index 194ba8f5188..7ff393f371f 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -4503,6 +4503,27 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
       && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
   (cond @1 (convert @2) (convert @3))))
 
+#if GIMPLE
+(if (canonicalize_math_p ())
+/* These patterns are mostly used by PHIOPT to move some operations outside of
+   the if statements. They should be done late because it gives jump threading
+   and few other passes to reduce what is going on.  */
+/* a ? x op C : x -> x op << log2(C) when C is power of 2. */
+ (for op (plus minus bit_ior bit_xor lshift rshift lrotate rrotate)
+  (simplify
+   (cond @0 (op:s @1 integer_pow2p@2) @1)
+    /* powerof2cst */
+   (if (INTEGRAL_TYPE_P (type))
+    (with {
+      tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
+     }
+     (op @1 (lshift (convert (convert:boolean_type_node @0)) { shift; })))
+   )
+  )
+ )
+)
+#endif
+
 /* Simplification moved from fold_cond_expr_with_comparison.  It may also
    be extended.  */
 /* This pattern implements two kinds simplification:
diff --git a/gcc/testsuite/gcc.dg/pr107190.c b/gcc/testsuite/gcc.dg/pr107190.c
new file mode 100644
index 00000000000..235b2761a02
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr107190.c
@@ -0,0 +1,27 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fexpensive-optimizations -fdump-tree-phiopt2-details" } */
+
+#  define BN_BITS4        32
+#  define BN_MASK2        (0xffffffffffffffffL)
+#  define BN_MASK2l       (0xffffffffL)
+#  define BN_MASK2h       (0xffffffff00000000L)
+#  define BN_MASK2h1      (0xffffffff80000000L)
+#  define LBITS(a)        ((a)&BN_MASK2l)
+#  define HBITS(a)        (((a)>>BN_BITS4)&BN_MASK2l)
+#  define L2HBITS(a)      (((a)<<BN_BITS4)&BN_MASK2)
+
+unsigned int test_m(unsigned long in0, unsigned long in1) {
+    unsigned long m, m1, lt, ht, bl, bh;
+    lt = LBITS(in0);
+    ht = HBITS(in0);
+    bl = LBITS(in1);
+    bh = HBITS(in1);
+    m  = bh * lt;
+    m1 = bl * ht;
+    ht = bh * ht;
+    m  = (m + m1) & BN_MASK2;
+    if (m < m1) ht += L2HBITS((unsigned long)1);
+    return ht + m;
+}
+
+/* { dg-final { scan-tree-dump "COND_EXPR in block 2 and PHI in block 4 converted to straightline code" "phiopt2" } } */
-- 
2.19.1


  reply	other threads:[~2022-11-05  9:03 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-05 21:44 [PATCH] riscv: implement TARGET_MODE_REP_EXTENDED Philipp Tomsich
2022-09-06 11:39 ` Alexander Monakov
2022-09-16 23:48   ` Jeff Law
2022-09-17  7:59     ` Palmer Dabbelt
2022-11-04 23:00   ` Philipp Tomsich
2022-11-05  6:16     ` [PATCH] [PHIOPT] Add A ? B + CST : B match and simplify optimizations Zhongyunde
2022-11-05  6:34       ` Andrew Pinski
2022-11-05  9:03         ` Zhongyunde [this message]
2022-11-08 14:58           ` Richard Biener
2022-11-08 15:51             ` 钟云德
2022-11-09  8:00               ` Richard Biener
2022-11-07 13:55     ` [PATCH] riscv: implement TARGET_MODE_REP_EXTENDED Alexander Monakov
2022-11-08 23:45       ` Philipp Tomsich
2022-11-09 17:21         ` Alexander Monakov
2022-11-20 16:09     ` Jeff Law
2022-11-21 13:49       ` Alexander Monakov
2022-11-21 14:56         ` Jeff Law
2022-11-21 15:33           ` Alexander Monakov

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=7b6d19fcef784a019c89e77c9917c5a8@huawei.com \
    --to=zhongyunde@huawei.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=hongtao.liu@intel.com \
    --cc=pinskia@gcc.gnu.org \
    --cc=weiwei64@huawei.com \
    --cc=zhong_1985624@163.com \
    --cc=zwzhangwen.zhang@huawei.com \
    /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).