public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Robin Dapp <rdapp@linux.vnet.ibm.com>
To: Richard Biener <richard.guenther@gmail.com>
Cc: "Bin.Cheng" <amker.cheng@gmail.com>,
	GCC Patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH 2/3] Simplify wrapped binops
Date: Wed, 05 Jul 2017 08:51:00 -0000	[thread overview]
Message-ID: <3924b68e-bd53-ef94-f1d4-ccab2ba564f2@linux.vnet.ibm.com> (raw)
In-Reply-To: <CAFiYyc1QNLC2N-_Yo5-73u2akOAshoPPtxYLCczsMNJ=TYqBhA@mail.gmail.com>

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

> While the initialization value doesn't matter (wi::add will overwrite it)
> better initialize both to false ;)  Ah, you mean because we want to
> transform only if get_range_info returned VR_RANGE.  Indeed somewhat
> unintuitive (but still the best variant for now).

> so I'm still missing a comment on why min_ovf && max_ovf is ok.
> The simple-minded would have written [...]

I suppose it's more a matter of considering too many things at the same
time for me...  I was still thinking of including more cases than
necessary for the regression.  Guess the attached version will do as
well and should not contain any more surprises.  If needed, I'll add
additional cases some time.

Tests in a followup message.

Regards
 Robin

[-- Attachment #2: gcc-wrapped-binop-p2.diff --]
[-- Type: text/x-patch, Size: 2507 bytes --]

diff --git a/gcc/match.pd b/gcc/match.pd
index 80a17ba..3acf8be 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1290,6 +1290,70 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
     (if (cst && !TREE_OVERFLOW (cst))
      (plus { cst; } @0))))
 
+/* ((T)(A + CST1)) + CST2 -> (T)(A) + CST  */
+#if GIMPLE
+  (simplify
+    (plus (convert (plus@3 @0 INTEGER_CST@1)) INTEGER_CST@2)
+      (if (INTEGRAL_TYPE_P (type)
+           && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@3)))
+       /* Combine CST1 and CST2 to CST and convert to outer type if
+          (A + CST1)'s range does not overflow.  */
+       (with
+       {
+         tree inner_type = TREE_TYPE (@3);
+         wide_int wmin0, wmax0;
+         wide_int w1 = @1;
+
+         bool ovf_undef = TYPE_OVERFLOW_UNDEFINED (inner_type);
+         bool min_ovf = true, max_ovf = true;
+
+         enum value_range_type vr0 = get_range_info (@0, &wmin0, &wmax0);
+
+         if (ovf_undef || vr0 == VR_RANGE)
+           {
+             if (!ovf_undef && vr0 == VR_RANGE)
+	       {
+		 wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
+		 wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
+	       }
+	     w1 = w1.from (@1, TYPE_PRECISION (type), TYPE_SIGN (inner_type));
+           }
+       }
+   (if (ovf_undef || !(min_ovf || max_ovf))
+    (plus (convert @0) { wide_int_to_tree (type, wi::add (w1, @2)); }
+     )))))
+#endif
+
+/* ((T)(A)) + CST -> (T)(A + CST)  */
+#if GIMPLE
+  (simplify
+   (plus (convert SSA_NAME@0) INTEGER_CST@1)
+    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
+         && INTEGRAL_TYPE_P (type)
+         && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
+         && int_fits_type_p (@1, TREE_TYPE (@0)))
+     /* Perform binary operation inside the cast if the constant fits
+        and (A + CST)'s range does not overflow.  */
+     (with
+      {
+        bool min_ovf = true, max_ovf = true;
+        tree inner_type = TREE_TYPE (@0);
+
+        wide_int w1 = w1.from (@1, TYPE_PRECISION (inner_type), TYPE_SIGN
+      		(inner_type));
+
+        wide_int wmin0, wmax0;
+        if (get_range_info (@0, &wmin0, &wmax0) == VR_RANGE)
+          {
+            wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
+            wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
+          }
+      }
+     (if (!min_ovf && !max_ovf)
+      (convert (plus @0 { {wide_int_to_tree (TREE_TYPE (@0), w1)}; })))
+     )))
+#endif
+
   /* ~A + A -> -1 */
   (simplify
    (plus:c (bit_not @0) @0)

  reply	other threads:[~2017-07-05  8:51 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-07-21 10:42 [PATCH] Tree-level fix for PR 69526 Robin Dapp
2016-07-21 11:28 ` Richard Biener
2016-08-22 14:58   ` Robin Dapp
2016-09-05  7:50     ` Robin Dapp
2016-09-14 13:08     ` Richard Biener
2016-09-14 17:04       ` Jeff Law
2016-10-14  8:33         ` Robin Dapp
2016-09-20 12:39       ` Robin Dapp
2016-09-20 15:31         ` Jeff Law
2016-10-05 10:40         ` Robin Dapp
2016-10-14 11:49         ` Richard Biener
2016-11-16 15:54           ` Robin Dapp
2016-11-25  6:49             ` Robin Dapp
2016-11-25 13:47             ` Richard Biener
2016-11-28 11:13               ` Richard Biener
2016-11-28 13:26                 ` Robin Dapp
2016-12-05  7:57                   ` Robin Dapp
2016-12-06 13:03                   ` Richard Biener
2016-12-07 16:15                     ` Robin Dapp
2016-12-13 14:13                       ` Richard Biener
2017-01-10 13:33                         ` Robin Dapp
2017-01-17  7:34                           ` Robin Dapp
2017-01-17  9:48                           ` Richard Biener
2017-02-02  9:27                             ` Robin Dapp
2017-05-09  7:31                               ` Robin Dapp
2017-05-11 15:08                             ` Bin.Cheng
2017-05-18 14:47                               ` Robin Dapp
2017-05-18 14:48                               ` [PATCH 1/3] Simplify wrapped binops Robin Dapp
2017-05-18 14:49                               ` [PATCH 2/3] " Robin Dapp
2017-05-18 15:46                                 ` Bin.Cheng
2017-05-18 16:09                                   ` Robin Dapp
2017-05-18 17:15                                     ` Bin.Cheng
2017-05-19 10:13                                       ` Robin Dapp
2017-05-19 10:22                                         ` Bin.Cheng
2017-05-19 10:32                                           ` Richard Biener
2017-06-20 13:08                                           ` Robin Dapp
2017-06-20 13:49                                             ` Richard Biener
2017-06-21 11:44                                               ` Robin Dapp
2017-06-27  7:17                                                 ` Robin Dapp
2017-06-27 12:14                                                 ` Richard Biener
2017-06-28 14:35                                                   ` Robin Dapp
2017-07-03 13:10                                                     ` Richard Biener
2017-07-05  8:51                                                       ` Robin Dapp [this message]
2017-07-05  8:54                                                         ` Robin Dapp
2017-07-15  9:58                                                         ` Marc Glisse
2017-05-18 15:08                               ` [PATCH 3/3] " Robin Dapp
2016-08-23  7:11   ` [PATCH] Tree-level fix for PR 69526 Robin Dapp

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=3924b68e-bd53-ef94-f1d4-ccab2ba564f2@linux.vnet.ibm.com \
    --to=rdapp@linux.vnet.ibm.com \
    --cc=amker.cheng@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=richard.guenther@gmail.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).