public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Optimize (X<<C)+(Y<<C) as (X+Y)<<C for signed addition.
@ 2022-09-13 18:37 Roger Sayle
  2022-09-14  8:28 ` Richard Biener
  2022-09-14 21:42 ` Marc Glisse
  0 siblings, 2 replies; 4+ messages in thread
From: Roger Sayle @ 2022-09-13 18:37 UTC (permalink / raw)
  To: 'GCC Patches'

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


This patch tweaks the match.pd transformation previously added to fold
(X<<C)+(Y<<C) as (X+Y)<<C that was previously restricted to unsigned
(wrapping) types, to also allow signed integer types provided that they
don't trap and the overflow needn't be preserved for sanitization.
i.e. this should now apply (by default) for "int x,y;", but is disabled
with -ftrapv.

This unsigned transformation has baked on mainline without problems, so it's
time to turn up the heat...  LLVM, icc and MSVC perform this optimization.


This patch has been tested on x86_64-pc-linux-gnu with make bootstrap and
make -k check, both with and without --target_board=unix{-m32}, with no
new failures.  Ok for mainline?


2022-09-13  Roger Sayle  <roger@nextmovesoftware.com>

gcc/ChangeLog
        * match.pd (op (lshift @0 @1) (lshift @2 @1)): Optimize the
        expression (X<<C) + (Y<<C) to (X+Y)<<C for signed types, that
        may have undefined overflow, provided that they don't trap.

gcc/testsuite/ChangeLog
        * gcc.dg/pr71343-3.c: New test case.


Thanks in advance,
Roger
--


[-- Attachment #2: patchfd3.txt --]
[-- Type: text/plain, Size: 1011 bytes --]

diff --git a/gcc/match.pd b/gcc/match.pd
index 17318f52..e1e6af9 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -988,7 +988,8 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
   (simplify
     (op (lshift:s @0 @1) (lshift:s @2 @1))
     (if (INTEGRAL_TYPE_P (type)
-	 && TYPE_OVERFLOW_WRAPS (type)
+	 && !TYPE_OVERFLOW_SANITIZED (type)
+	 && !TYPE_OVERFLOW_TRAPS (type)
 	 && !TYPE_SATURATING (type))
       (lshift (op @0 @2) @1))))
 
diff --git a/gcc/testsuite/gcc.dg/pr71343-3.c b/gcc/testsuite/gcc.dg/pr71343-3.c
new file mode 100644
index 0000000..d0bdbae
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr71343-3.c
@@ -0,0 +1,15 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int foo(int a, int b)
+{
+  return (a << 2) + (b << 2);
+}
+
+int bar(int a, int b)
+{
+  return (a << 2) + (b << 2) == (a + b) << 2;
+}
+
+/* { dg-final { scan-tree-dump-times " << " 1 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "return 1" 1 "optimized" } } */

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

end of thread, other threads:[~2022-09-15  6:39 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-13 18:37 [PATCH] Optimize (X<<C)+(Y<<C) as (X+Y)<<C for signed addition Roger Sayle
2022-09-14  8:28 ` Richard Biener
2022-09-14 21:42 ` Marc Glisse
2022-09-15  6:39   ` Richard Biener

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