This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] Move X - (X / Y) * Y folding to match.pd


On Fri, 26 Jun 2015, Marek Polacek wrote:

This is an attempt to move one pattern from fold-const.c to match.pd.
It ought to be 1:1, but is not, e.g. with this patch we won't fold e.g.

int
f (int a, int b)
{
 return a - (unsigned) ((a / b) * b)
}

anymore, but we're able to fold

int
ff (int a, unsigned int b)
{
 return a - ((a / b) * b);
}

and fold-const.c is not.  I played around with converts, but didn't find
anything that would work well.  Any suggestions how to make this pattern
better?

Anything wrong with this?

+/* X - (X / Y) * Y is the same as X % Y.  */
+(simplify
+ (minus (convert? @0) (convert? (mult (trunc_div @0 @1) @1)))
+ (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
+  (convert (trunc_mod @0 @1))))

(the other div/mod pairs could benefit from the same transformation as long as there are no conversions, but the conversion seems easier to handle with trunc_)

diff --git gcc/match.pd gcc/match.pd
index b2f8429..2bc158b 100644
--- gcc/match.pd
+++ gcc/match.pd
@@ -238,6 +238,12 @@ along with GCC; see the file COPYING3.  If not see
      && tree_nop_conversion_p (type, TREE_TYPE (@1)))
  (trunc_mod @0 (convert @1))))

+/* X - (X / Y) * Y is the same as X % Y.  */
+(simplify
+ (minus @0 (mult (trunc_div @0 @1) @1))
+ (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
+  (trunc_mod @0 @1)))
+
/* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
   i.e. "X % C" into "X & (C - 1)", if X and C are positive.
   Also optimize A % (C << N)  where C is a power of 2,

	Marek


--
Marc Glisse


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]