public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [RFA][PATCH][PR rtl-optimization/47477] Type narrowing in match.pd
@ 2015-02-10 20:55 Jeff Law
  2015-02-11 10:56 ` Richard Biener
  2015-02-12  7:02 ` Bin.Cheng
  0 siblings, 2 replies; 10+ messages in thread
From: Jeff Law @ 2015-02-10 20:55 UTC (permalink / raw)
  To: gcc-patches

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


This PR was originally minor issue where we regressed on this kind of 
sequence:

typedef struct toto_s *toto_t;
toto_t add (toto_t a, toto_t b) {
   int64_t tmp = (int64_t)(intptr_t)a + ((int64_t)(intptr_t)b&~1L);
   return (toto_t)(intptr_t) tmp;
}


There was talk of trying to peephole this in the x86 backend.  But later 
Jakub speculated that if we had good type narrowing this could be done 
in the tree optimizers...

Soooo, here we go.  I didn't do anything with logicals are those are 
already handled elsewhere in match.pd.  I didn't try to handle MULT as 
in the early experiments I did, it was a lose because of the existing 
mechanisms for widening multiplications.

Interestingly enough, this patch seems to help out libjava more than 
anything else in a GCC build and it really only helps a few routines. 
There weren't any routines I could see where the code regressed after 
this patch.  This is probably an indicator that these things aren't 
*that* common, or the existing shortening code better than we thought, 
or some important shortening case is missing.


I think we should pull the other tests from 47477 which are not 
regressions out into their own bug for future work.  Or alternately, 
when this fix is checked in remove the regression marker in 47477.


Bootstrapped and regression tested on x86_64-unknown-linux-gnu.  OK for 
the trunk?








[-- Attachment #2: P --]
[-- Type: text/plain, Size: 2959 bytes --]

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 7f3816c..7a95029 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,8 @@
+2015-02-10  Jeff Law  <law@redhat.com>
+
+	* match.pd (convert (plus/minus (convert @0) (convert @1): New
+	simplifier to narrow arithmetic.
+
 2015-02-10  Richard Biener  <rguenther@suse.de>
 
 	PR tree-optimization/64909
diff --git a/gcc/match.pd b/gcc/match.pd
index 81c4ee6..abc703e 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1018,3 +1018,21 @@ along with GCC; see the file COPYING3.  If not see
    (logs (pows @0 @1))
    (mult @1 (logs @0)))))
 
+/* If we have a narrowing conversion of an arithmetic operation where
+   both operands are widening conversions from the same type as the outer
+   narrowing conversion.  Then convert the innermost operands to a suitable
+   unsigned type (to avoid introducing undefined behaviour), perform the
+   operation and convert the result to the desired type. 
+
+   This narrows the arithmetic operation.  */
+(for op (plus minus)
+  (simplify
+    (convert (op (convert@2 @0) (convert @1)))
+    (if (TREE_TYPE (@0) == TREE_TYPE (@1)
+         && TREE_TYPE (@0) == type
+         && INTEGRAL_TYPE_P (type)
+         && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
+	 /* This prevents infinite recursion.  */
+	 && unsigned_type_for (TREE_TYPE (@0)) != TREE_TYPE (@2))
+      (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
+        (convert (op (convert:utype @0) (convert:utype @1)))))))
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 15d5e2d..76e5254 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2015-02-10  Jeff Law  <law@redhat.com>
+
+	PR rtl-optimization/47477
+	* gcc.dg/tree-ssa/narrow-arith-1.c: New test.
+
 2015-02-10  Richard Biener  <rguenther@suse.de>
 
 	PR tree-optimization/64909
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/narrow-arith-1.c b/gcc/testsuite/gcc.dg/tree-ssa/narrow-arith-1.c
new file mode 100644
index 0000000..104cb6f5
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/narrow-arith-1.c
@@ -0,0 +1,22 @@
+/* PR tree-optimization/47477 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized -w" } */
+/* { dg-require-effective-target ilp32 } */
+
+typedef int int64_t __attribute__ ((__mode__ (__DI__)));
+typedef int * intptr_t;
+
+typedef struct toto_s *toto_t;
+toto_t add (toto_t a, toto_t b) {
+  int64_t tmp = (int64_t)(intptr_t)a + ((int64_t)(intptr_t)b&~1L);
+  return (toto_t)(intptr_t) tmp;
+}
+
+/* For an ILP32 target there'll be 6 casts when we start, but just 4
+   if the match.pd pattern is successfully matched.  */
+/* { dg-final { scan-tree-dump-times "= \\(int\\)" 1 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "= \\(unsigned int\\)" 2 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "= \\(struct toto_s \\*\\)" 1 "optimized" } } */
+/* { dg-final { cleanup-tree-dump "optimized" } } */
+
+

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2015-02-13 21:02 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-02-10 20:55 [RFA][PATCH][PR rtl-optimization/47477] Type narrowing in match.pd Jeff Law
2015-02-11 10:56 ` Richard Biener
2015-02-11 17:05   ` Jeff Law
2015-02-12 13:02     ` Richard Biener
2015-02-12 16:55       ` Jeff Law
2015-02-12 23:23       ` Jeff Law
2015-02-13  9:09         ` Richard Biener
2015-02-13 21:02           ` Jeff Law
2015-02-12  7:02 ` Bin.Cheng
2015-02-12 16:43   ` Jeff Law

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).