Bug 53979 - ((a ^ b) | a) not optimized to (a | b)
Summary: ((a ^ b) | a) not optimized to (a | b)
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.8.0
: P3 enhancement
Target Milestone: 7.0
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2012-07-16 10:41 UTC by Wouter Vermaelen
Modified: 2021-07-28 22:19 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2016-08-14 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Wouter Vermaelen 2012-07-16 10:41:16 UTC
The following 3 functions should ideally generate identical code (and they do
when using clang).

int f1(int a, int b) {
	int c = b;
	return (a ^ b ^ c) | (a ^ b) | a;
}
int f2(int a, int b) {
	return (a ^ b) | a;
}
int f3(int a, int b) {
	return a | b;
}

I tested gcc revision trunk@189510 and it shows 2 missed optimizations:

1) (a ^ b ^ b) not simplified to (a)
Normally gcc performs this optimization, but I *guess* it misses it here
because of the CSE opportunity with (a ^ b).

2) ((a ^ b) | a) not simplified to (a | b)


Of course in this example it's easy to manually rewrite the code. But in my
original code this function was actually a template and for some instantiations
the expression for the variable 'c' simplified to just 'b'. So the first missed
optimization is something I saw in real code. The second missed optimization
only occurs in this (much) simplified variant of the function.
Comment 1 Andrew Pinski 2016-08-14 18:20:06 UTC
Confirmed, f1 and f2 are the same now.  So the only thing left is 2 from comment #0.
Comment 2 prathamesh3492 2016-10-20 07:56:00 UTC
Author: prathamesh3492
Date: Thu Oct 20 07:55:28 2016
New Revision: 241360

URL: https://gcc.gnu.org/viewcvs?rev=241360&root=gcc&view=rev
Log:
2016-10-20  Prathamesh Kulkarni  <prathamesh.kulkarni@linaro.org>

	PR tree-optimization/53979
	* match.pd ((a ^ b) | a -> a | b): New pattern.

testsuite/
	* gcc.dg/pr53979-1.c: New test-case.
	* gcc.dg/pr53979-2.c: Likewise.

Added:
    trunk/gcc/testsuite/gcc.dg/pr53979-1.c
    trunk/gcc/testsuite/gcc.dg/pr53979-2.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/match.pd
    trunk/gcc/testsuite/ChangeLog
Comment 3 Martin Liška 2018-11-19 12:05:48 UTC
Can the bug be marked as resolved?
Comment 4 Gabriel Ravier 2020-06-03 03:18:54 UTC
I've just checked on trunk and it looks fixed
Comment 5 Andrew Pinski 2021-07-28 22:19:54 UTC
Fixed for GCC 7.