Bug 15458

Summary: Combine ~ and ^.
Product: gcc Reporter: Kazu Hirata <kazu>
Component: tree-optimizationAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: enhancement CC: gabravier, gcc-bugs, pinskia
Priority: P2 Keywords: missed-optimization, TREE
Version: 4.0.0   
Target Milestone: 4.3.0   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2006-03-01 02:56:09
Bug Depends on: 15459    
Bug Blocks: 19986    

Description Kazu Hirata 2004-05-15 12:18:00 UTC
unsigned int
xornot (unsigned int a)
{
  unsigned int b = a ^ 0xffff;
  unsigned int c = ~b;
  return c;
}

unsigned int
notxor (unsigned int a)
{
  unsigned int b = ~a;
  unsigned int c = b ^ 0xffff;
  return c;
}

The last tree-ssa form looks like:

;; Function xornot (xornot)

xornot (a)
{
  unsigned int c;
  unsigned int b;

<bb 0>:
  b_2 = a_1 ^ 65535;
  c_3 = ~b_2;
  return c_3;

}



;; Function notxor (notxor)

notxor (a)
{
  unsigned int c;
  unsigned int b;

<bb 0>:
  b_2 = ~a_1;
  c_3 = b_2 ^ 65535;
  return c_3;

}
Comment 1 Andrew Pinski 2004-05-15 12:23:00 UTC
Confirmed.
Comment 2 Andrew Pinski 2004-05-17 02:05:32 UTC
Will be fix by PR 15459 when fold is able to optimize ~(a<D1109>_1 ^ 65535) into 
~a<D1104>_1 ^ 65535
Comment 3 Andrew Pinski 2004-06-02 21:14:30 UTC
I should note that the RTL level does this optimization.
Comment 4 Andrew Pinski 2005-11-30 17:32:35 UTC
Actually it is better for ~(a ^ CST) to come out as a ^ ~CST.

Right now we actually already implement ~(a^CST) as a ^ ~CST.  So we need just to implement (~a) ^ CST as a ^ ~CST.  In fact this is what simplify_rtx does.
Comment 5 Andrew Pinski 2005-11-30 17:41:12 UTC
~(a^CST) is done in fold_unary with the comment of:
/* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */


Only (~a^~b) is simplified.  That can be expanded to:
(~a^b) if ~b simplifies, simplify the expression.
likewise for (a^~b) (if ~a simplifies, simplify the expression).

I am going to implement this.
Comment 6 Andrew Pinski 2005-12-04 04:27:51 UTC
I am no longer going to fix the fold issue, it is too much hasle to get this fixed.
Comment 7 Roger Sayle 2006-10-29 17:51:17 UTC
Subject: Bug 15458

Author: sayle
Date: Sun Oct 29 17:51:07 2006
New Revision: 118152

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=118152
Log:

	PR tree-optimization/15458
	* fold-const.c (fold_binary): Optimize ~X ^ C as X ^ ~C, where C
	is a constant.

	* gcc.dg/fold-xornot-1.c: New test case.


Added:
    trunk/gcc/testsuite/gcc.dg/fold-xornot-1.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/fold-const.c
    trunk/gcc/testsuite/ChangeLog

Comment 8 Andrew Pinski 2006-10-29 20:27:44 UTC
Fixed.