Bug 14796 - [tree-ssa] combine two shifts into one
Summary: [tree-ssa] combine two shifts into one
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: tree-ssa
: P2 enhancement
Target Milestone: 4.1.0
Assignee: James A. Morrison
URL:
Keywords: missed-optimization, TREE
: 20544 (view as bug list)
Depends on: 15459
Blocks: 19986
  Show dependency treegraph
 
Reported: 2004-03-31 09:51 UTC by Kazu Hirata
Modified: 2005-06-13 00:39 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2005-02-14 02:15:51


Attachments
Tranform (a OP c1) OP c2 into a OP (c1 + c2) and (a >> c) << c into a & -(1 << c) (795 bytes, patch)
2005-06-09 04:00 UTC, James A. Morrison
Details | Diff
testcase (159 bytes, text/plain)
2005-06-09 04:01 UTC, James A. Morrison
Details
attempt 2 (1.00 KB, patch)
2005-06-09 05:17 UTC, James A. Morrison
Details | Diff
testcase 1 (197 bytes, text/plain)
2005-06-09 05:17 UTC, James A. Morrison
Details
testcase 2 (189 bytes, text/plain)
2005-06-09 05:18 UTC, James A. Morrison
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Kazu Hirata 2004-03-31 09:51:40 UTC
void bar (void);

unsigned int
foo (unsigned int a)
{
  a <<= 16;
  a >>= 16;
  return a;
}

unsigned int
baz (unsigned int a)
{
  a >>= 1;
  a >>= 1;
  return a;
}

I get:

;; Function foo (foo)

foo (a)
{
<bb 0>:
  a_2 = a_1 << 16;
  a_3 = a_2 >> 16;
  return a_3;

}



;; Function baz (baz)

baz (a)
{
  unsigned int pretmp.0;

<bb 0>:
  a_2 = a_1 >> 1;
  a_3 = a_2 >> 1;
  return a_3;

}

foo() could use an AND.  baz() could simply say "a_3 = a_1 >> 2;".
Comment 1 Andrew Pinski 2004-03-31 12:38:46 UTC
Confirmed.
Comment 2 Andrew Pinski 2004-05-17 01:51:08 UTC
Fold does optimize (a >> 1)>>1 once that is optimized the patch which fixes PR 15459 
will fix this one also.
Comment 3 Andrew Pinski 2005-03-18 21:44:29 UTC
*** Bug 20544 has been marked as a duplicate of this bug. ***
Comment 4 James A. Morrison 2005-06-09 04:00:18 UTC
Created attachment 9048 [details]
Tranform (a OP c1) OP c2 into a OP (c1 + c2) and (a >> c) << c into a & -(1 << c)
Comment 5 James A. Morrison 2005-06-09 04:01:28 UTC
Created attachment 9049 [details]
testcase
Comment 6 James A. Morrison 2005-06-09 05:17:10 UTC
Created attachment 9050 [details]
attempt 2

Thanks to Roger for an informal review of the previous patch.
Comment 7 James A. Morrison 2005-06-09 05:17:40 UTC
Created attachment 9051 [details]
testcase 1
Comment 8 James A. Morrison 2005-06-09 05:18:01 UTC
Created attachment 9052 [details]
testcase 2
Comment 9 CVS Commits 2005-06-12 08:03:31 UTC
Subject: Bug 14796

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	phython@gcc.gnu.org	2005-06-12 08:03:23

Modified files:
	gcc            : ChangeLog fold-const.c 
	gcc/testsuite  : ChangeLog 
Added files:
	gcc/testsuite/gcc.dg: pr14796-1.c pr14796-2.c 

Log message:
	2005-06-12  James A. Morrison  <phython@gcc.gnu.org>
	
	PR tree-optimization/14796
	* fold-const.c (fold_binary): Transform (A >> C) << C into
	one BIT_AND_EXPR.
	<shift>: Transform (A OP c1) OP c2 into A OP (c1 + c2).

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=2.9129&r2=2.9130
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fold-const.c.diff?cvsroot=gcc&r1=1.592&r2=1.593
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.5623&r2=1.5624
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/pr14796-1.c.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/pr14796-2.c.diff?cvsroot=gcc&r1=NONE&r2=1.1

Comment 10 Andrew Pinski 2005-06-12 13:55:17 UTC
Just a status, we are now waiting on the tree combiner to fix the orginal problem.
Comment 11 CVS Commits 2005-06-13 00:18:51 UTC
Subject: Bug 14796

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	phython@gcc.gnu.org	2005-06-13 00:18:41

Modified files:
	gcc            : ChangeLog fold-const.c 
	gcc/testsuite  : ChangeLog 
	gcc/testsuite/gcc.dg: pr14796-1.c 

Log message:
	2005-06-12  James A. Morrison  <phython@gcc.gnu.org>
	
	PR tree-optimization/14796
	* fold-const (fold_binary): Transform (X << C) >> C into X & (-1>>C)
	for unsigned types.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=2.9132&r2=2.9133
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fold-const.c.diff?cvsroot=gcc&r1=1.593&r2=1.594
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.5628&r2=1.5629
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/pr14796-1.c.diff?cvsroot=gcc&r1=1.1&r2=1.2

Comment 12 James A. Morrison 2005-06-13 00:27:11 UTC
 Fixed in previous commits.