public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] match.pd: reassociate multiplications with constants
@ 2017-07-13 16:37 Alexander Monakov
  2017-07-13 18:31 ` Marc Glisse
  0 siblings, 1 reply; 16+ messages in thread
From: Alexander Monakov @ 2017-07-13 16:37 UTC (permalink / raw)
  To: gcc-patches; +Cc: Marc Glisse

Hi,

This is a followup to https://gcc.gnu.org/ml/gcc-patches/2017-05/msg01545.html

Recently due to a fix for PR 80800 GCC has lost the ability to reassociate
signed multiplications chains to go from 'X * CST1 * Y * CST2'
to 'X * Y * (CST1 * CST2)'.  The fix to that PR prevents extract_muldiv from
introducing '(X * (CST1 * CST2)) * Y', which was wrong because it may cause
intermediate signed overflow (unexpected if Y == 0).

As mentioned in that thread, we can reassociate constants to outermost operands
instead: this is safe because CST1 cannot be 0 or -1, since those are handled
by other match.pd rules.

(in fact it's possible to reassociate negates too, and go from '(-X) * Y * CST'
to '(X * Y) * (-CST)' if (-CST) doesn't overflow (again, we know that CST != 1),
but I'm not sure how valuable that is in practice, so opted not to do that yet)

The following patch reinstates folding by adding a new match.pd rule that moves
constants to outermost operands, where they can be merged by fold_binary
machinery if their product doesn't overflow.  Bootstrapped and regtested on
amd64, OK for trunk?

	* match.pd ((X * CST) * Y): Reassociate to (X * Y) * CST.

diff --git a/gcc/match.pd b/gcc/match.pd
index 4c64b21..e49f879 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -2139,6 +2139,15 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  (mult @0 integer_minus_onep)
  (negate @0))

+/* Reassociate (X * CST) * Y to (X * Y) * CST.  This does not introduce
+   signed overflow: previous rules handle CST being -1 or 0, and for
+   the rest we know that if X * Y overflows, so does (X * CST) * Y.  */
+(if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_SATURATING (type))
+ (simplify
+  (mult:c (mult @0 INTEGER_CST@1) @2)
+  (if (TREE_CODE (@2) != INTEGER_CST)
+   (mult (mult @0 @2) @1))))
+
 /* True if we can easily extract the real and imaginary parts of a complex
    number.  */
 (match compositional_complex

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

end of thread, other threads:[~2017-07-20 10:51 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-07-13 16:37 [PATCH] match.pd: reassociate multiplications with constants Alexander Monakov
2017-07-13 18:31 ` Marc Glisse
2017-07-13 19:42   ` Alexander Monakov
2017-07-14  6:00     ` Marc Glisse
2017-07-18  8:23       ` Richard Biener
2017-07-15 17:01   ` Alexander Monakov
2017-07-15 17:16   ` Alexander Monakov
2017-07-17 20:00     ` Marc Glisse
2017-07-17 20:50       ` Alexander Monakov
2017-07-18 17:08         ` Alexander Monakov
2017-07-19 11:28           ` Richard Biener
2017-07-19 11:33             ` Richard Biener
2017-07-19 14:40               ` Alexander Monakov
2017-07-20  8:41                 ` Richard Biener
2017-07-20  9:40                   ` Alexander Monakov
2017-07-20 10:51                     ` 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).