Bug 35691 - Missed (a == 0) && (b == 0) into (a|(typeof(a)(b)) == 0 when the types don't match
Summary: Missed (a == 0) && (b == 0) into (a|(typeof(a)(b)) == 0 when the types don't ...
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.4.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2008-03-25 04:31 UTC by Andrew Pinski
Modified: 2017-09-26 13:58 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2010-03-02 18:40:14


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andrew Pinski 2008-03-25 04:31:50 UTC
While looking into PR 35429, I noticed this small missed optimization.
If we have:
typedef unsigned int1;

int foo(int z0, int1 z1)
{
  return z0 != 0 || z1 != 0;
}

int foo1(int z0, int1 z1)
{
  return z0 == 0 && z1 == 0;
}
--- CUT ---
Both of those should optimize as int is the same size as unsigned and we are comparing against 0.  The same thing should happen with int and long on a ILP32 target.
Comment 1 prathamesh3492 2016-11-07 17:32:49 UTC
Author: prathamesh3492
Date: Mon Nov  7 17:32:17 2016
New Revision: 241915

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

	PR middle-end/35691
	* match.pd: Add following two patterns:
	(x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
	(x != 0 | y != 0) -> (x | typeof(x)(y)) != 0.

testsuite/
	* gcc.dg/pr35691-1.c: New test-case.
	* gcc.dg/pr35691-4.c: Likewise.

Added:
    trunk/gcc/testsuite/gcc.dg/pr35691-1.c
    trunk/gcc/testsuite/gcc.dg/pr35691-2.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/match.pd
    trunk/gcc/testsuite/ChangeLog
Comment 2 prathamesh3492 2016-11-09 09:52:17 UTC
Fixed on targets when LOGICAL_OP_NON_SHORT_CIRCUIT is true.
When LOGICAL_OP_NON_SHORT_CIRCUIT is false, the conversion of truth_andif_expr to bit_and_expr doesn't happen and the pattern added in r241915 doesn't get matched.
Comment 3 Jakub Jelinek 2017-09-22 18:56:54 UTC
Author: jakub
Date: Fri Sep 22 18:56:23 2017
New Revision: 253107

URL: https://gcc.gnu.org/viewcvs?rev=253107&root=gcc&view=rev
Log:
	PR middle-end/35691
	* match.pd: Simplify x == -1 & y == -1 into (x & y) == -1
	and x != -1 | y != -1 into (x & y) != -1.

	* gcc.dg/pr35691-1.c: Use -fdump-tree-forwprop1-details
	instead of -fdump-tree-forwprop-details in dg-options.
	* gcc.dg/pr35691-2.c: Likewise.
	* gcc.dg/pr35691-3.c: New test.
	* gcc.dg/pr35691-4.c: New test.

Added:
    trunk/gcc/testsuite/gcc.dg/pr35691-3.c
    trunk/gcc/testsuite/gcc.dg/pr35691-4.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/match.pd
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gcc.dg/pr35691-1.c
    trunk/gcc/testsuite/gcc.dg/pr35691-2.c
Comment 4 Andrew Pinski 2017-09-22 21:18:24 UTC
(In reply to prathamesh3492 from comment #2)
> Fixed on targets when LOGICAL_OP_NON_SHORT_CIRCUIT is true.
> When LOGICAL_OP_NON_SHORT_CIRCUIT is false, the conversion of
> truth_andif_expr to bit_and_expr doesn't happen and the pattern added in
> r241915 doesn't get matched.

Hmm, I thought ifcombine should do the "right thing" here ...
Comment 5 Jakub Jelinek 2017-09-22 23:32:07 UTC
No, the infrastructure is in tree-ssa-reassoc.c, and I plan to look at it next week.
Comment 6 Jakub Jelinek 2017-09-26 13:58:42 UTC
Author: jakub
Date: Tue Sep 26 13:58:11 2017
New Revision: 253201

URL: https://gcc.gnu.org/viewcvs?rev=253201&root=gcc&view=rev
Log:
	PR middle-end/35691
	* tree-ssa-reassoc.c (update_range_test): Dump r->exp each time
	if it is different SSA_NAME.
	(optimize_range_tests_cmp_bitwise): New function.
	(optimize_range_tests): Call it.

	* gcc.dg/pr35691-5.c: New test.
	* gcc.dg/pr35691-6.c: New test.

Added:
    trunk/gcc/testsuite/gcc.dg/pr35691-5.c
    trunk/gcc/testsuite/gcc.dg/pr35691-6.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/tree-ssa-reassoc.c