Bug 19055

Summary: Minor bit optimization with or and xor
Product: gcc Reporter: Andrew Pinski <pinskia>
Component: tree-optimizationAssignee: Andrew Pinski <pinskia>
Status: RESOLVED FIXED    
Severity: enhancement CC: gcc-bugs, segher
Priority: P2 Keywords: missed-optimization, patch
Version: 4.0.0   
Target Milestone: 4.1.0   
URL: http://gcc.gnu.org/ml/gcc-patches/2005-07/msg00258.html
Host: Target: powerpc-darwin
Build: Known to work:
Known to fail: Last reconfirmed: 2005-07-05 00:02:17
Bug Depends on:    
Bug Blocks: 19987    
Attachments: Patch which I am testing

Description Andrew Pinski 2004-12-17 14:16:06 UTC
The following two functions should produce the same assembly
int f(int a)
{
  a|=1;
  a^=1;
  return a;
}

int f1(int a)
{
  return a&~1;
}
Comment 1 Andrew Pinski 2004-12-17 14:25:44 UTC
Another example where we don't get the optimization and f1 is still one instruction (on 32bit PPC):
int f(int a,int b)
{
  a|=b;
  a^=b;
  return a;
}

int f1(int a,int b)
{
  return a&=~b;
}
Comment 2 Andrew Pinski 2004-12-22 07:42:13 UTC
I had forgot to mention, I found this when Nathan was asking about a~=b (aka a&=~b) and Segher 
sugessted a^=b;a|=b; and then we (Segher and I) both noticed that gcc was not doing the optimization.
Comment 3 Andrew Pinski 2005-02-22 14:44:40 UTC
If we changed the function in comment #0 like so:
int f(int a)
{
  a=(a|1)^1;
  return a;
}

Fold should do this simplification.

And likewise for comment #1 (which is just general case):
int f(int a,int b)
{
  a=(a|b) ^ b;
  return a;
}
Comment 4 Andrew Pinski 2005-07-05 00:02:16 UTC
I have a fix which I will be testing over night.
Comment 5 Andrew Pinski 2005-07-05 01:27:51 UTC
Created attachment 9203 [details]
Patch which I am testing

ChangeLog:
* fold-const.c (fold_binary): Transform "(X | Y) ^ X" to "Y & ~ X".
Comment 6 Andrew Pinski 2005-07-05 13:53:01 UTC
Patch posted here: <http://gcc.gnu.org/ml/gcc-patches/2005-07/msg00258.html>.
Comment 7 GCC Commits 2005-07-21 19:34:02 UTC
Subject: Bug 19055

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	pinskia@gcc.gnu.org	2005-07-21 19:33:50

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

Log message:
	2005-07-21  Andrew Pinski  <pinskia@physics.uc.edu>
	
	PR middle-end/19055
	* gcc.dg/tree-ssa/pr19055.c: New test.
	* gcc.dg/tree-ssa/pr19055-2.c: New test.
	
	2005-07-21  Andrew Pinski  <pinskia@physics.uc.edu>
	
	PR middle-end/19055
	* fold-const.c (fold_binary): Transform "(X | Y) ^ X" to "Y & ~ X".

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=2.9501&r2=2.9502
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fold-const.c.diff?cvsroot=gcc&r1=1.606&r2=1.607
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.5798&r2=1.5799

Comment 8 Andrew Pinski 2005-07-21 19:34:03 UTC
Fixed.