public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Richard Guenther" <richard.guenther@gmail.com>
To: gcc-patches <gcc-patches@gcc.gnu.org>
Subject: [PATCH] Fix PR34027, SCEV const-prop pessimizes -Os
Date: Sun, 11 Nov 2007 22:34:00 -0000	[thread overview]
Message-ID: <84fc9c000711110433x4c5fb5ebnfbb78cfac73f987a@mail.gmail.com> (raw)

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

With the now famous enough testcase

unsigned long long foobar(unsigned long long ns)
{
  while(ns >= 1000000000L)
    ns -= 1000000000L;
  return ns;
}

SCEV const-prop creates the expression

  ns + (ns /[fl] 1000000000L) / -1000000000L

for the final value of ns and we are not able to fold this
to the more efficient modulus expression,

The following patch does extend fold to handle this
case, but as it was very late when I wrote this patch I'd
appreciate another eye on possible corner-cases that
we could get wrong.

Thanks,
Richard.

2007-11-11  Richard Guenther  <rguenther@suse.de>

        PR middle-end/34027
        * fold-const.c (fold_binary): Fold n - (n / m) * m to n % m.

        * gcc.dg/pr34027-1.c: New testcase.
        * gcc.dg/pr34027-2.c: Likewise.

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: fix-pr34027.diff --]
[-- Type: text/x-patch; name=fix-pr34027.diff, Size: 3531 bytes --]

2007-11-11  Richard Guenther  <rguenther@suse.de>

	PR middle-end/34027
	* fold-const.c (fold_binary): Fold n - (n / m) * m to n % m.

	* gcc.dg/pr34027-1.c: New testcase.
	* gcc.dg/pr34027-2.c: Likewise.

Index: fold-const.c
===================================================================
*** fold-const.c	(revision 130076)
--- fold-const.c	(working copy)
*************** fold_binary (enum tree_code code, tree t
*** 9653,9658 ****
--- 9653,9677 ----
  		  return omit_one_operand (type, t1, arg0);
  		}
  	    }
+ 
+ 	  /* X + (X / CST) * -CST is X % CST.  */
+ 	  if (TREE_CODE (arg1) == MULT_EXPR
+ 	      && (TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
+ 		  || TREE_CODE (TREE_OPERAND (arg1, 0)) == FLOOR_DIV_EXPR)
+ 	      && operand_equal_p (arg0,
+ 				  TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
+ 	    {
+ 	      tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
+ 	      tree cst1 = TREE_OPERAND (arg1, 1);
+ 	      tree diff = fold_binary (PLUS_EXPR, TREE_TYPE (cst1), cst1,
+ 				       fold_convert (TREE_TYPE (cst1), cst0));
+ 	      enum tree_code code;
+ 	      code = TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
+ 		     ? TRUNC_MOD_EXPR : FLOOR_MOD_EXPR;
+ 	      if (diff && integer_zerop (diff))
+ 		return fold_build2 (code, type, op0,
+ 				    fold_convert (type, cst0));
+ 	    }
  	}
  
        /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
*************** fold_binary (enum tree_code code, tree t
*** 10061,10066 ****
--- 10080,10106 ----
  	  && integer_all_onesp (arg0))
  	return fold_build1 (BIT_NOT_EXPR, type, op1);
  
+ 
+       /* X - (X / CST) * CST is X % CST.  */
+       if (INTEGRAL_TYPE_P (type)
+ 	  && TREE_CODE (arg1) == MULT_EXPR
+ 	  && (TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
+ 	      || TREE_CODE (TREE_OPERAND (arg1, 0)) == FLOOR_DIV_EXPR)
+ 	  && operand_equal_p (arg0,
+ 			      TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
+ 	{
+ 	  tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
+ 	  tree cst1 = TREE_OPERAND (arg1, 1);
+ 	  tree diff = fold_binary (MINUS_EXPR, TREE_TYPE (cst1), cst1,
+ 				   fold_convert (TREE_TYPE (cst1), cst0));
+ 	  enum tree_code code;
+ 	  code = TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
+ 		 ? TRUNC_MOD_EXPR : FLOOR_MOD_EXPR;
+ 	  if (diff && integer_zerop (diff))
+ 	    return fold_build2 (code, type, op0,
+ 				fold_convert (type, cst0));
+ 	}
+ 
        if (! FLOAT_TYPE_P (type))
  	{
  	  if (integer_zerop (arg0))
Index: testsuite/gcc.dg/pr34027-1.c
===================================================================
*** testsuite/gcc.dg/pr34027-1.c	(revision 0)
--- testsuite/gcc.dg/pr34027-1.c	(revision 0)
***************
*** 0 ****
--- 1,12 ----
+ /* { dg-do compile } */
+ /* { dg-options "-Os -fdump-tree-optimized" } */
+ 
+ unsigned long foobar(unsigned long ns)
+ {
+   while(ns >= 10000L)
+     ns -= 10000L;
+   return ns;
+ }
+ 
+ /* { dg-final { scan-tree-dump "ns %\\\[fl\\\] 10000" "optimized" } } */
+ /* { dg-final { cleanup-tree-dump "optimized" } } */
Index: testsuite/gcc.dg/pr34027-2.c
===================================================================
*** testsuite/gcc.dg/pr34027-2.c	(revision 0)
--- testsuite/gcc.dg/pr34027-2.c	(revision 0)
***************
*** 0 ****
--- 1,10 ----
+ /* { dg-do compile } */
+ /* { dg-options "-fdump-tree-gimple" } */
+ 
+ long foo(long n, long m)
+ {
+   return n - (n / m) * m;
+ }
+ 
+ /* { dg-final { scan-tree-dump "n % m" "gimple" } } */
+ /* { dg-final { cleanup-tree-dump "gimple" } } */

             reply	other threads:[~2007-11-11 12:33 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-11-11 22:34 Richard Guenther [this message]
2007-11-12 15:47 ` Manuel López-Ibáñez
2007-11-12 15:54   ` Richard Guenther
2007-11-12 20:39     ` Andrew Pinski
2007-11-13  6:15       ` Manuel López-Ibáñez
2007-11-13  6:19 ` Bernd Schmidt
2007-11-13 11:18   ` Richard Guenther

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=84fc9c000711110433x4c5fb5ebnfbb78cfac73f987a@mail.gmail.com \
    --to=richard.guenther@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    /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).