Bug 15757 - Convert "If (a >= b) X else if (b <= a) Y" into "if (a >= b) X".
Summary: Convert "If (a >= b) X else if (b <= a) Y" into "if (a >= b) X".
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.0.0
: P2 enhancement
Target Milestone: 4.0.0
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2004-06-01 08:17 UTC by Kazu Hirata
Modified: 2004-07-10 00:45 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2004-06-01 11:20:34


Attachments
ZZZ (2.37 KB, text/plain)
2004-06-16 05:08 UTC, Jeffrey A. Law
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Kazu Hirata 2004-06-01 08:17:22 UTC
void bar1 (void);
void bar2 (void);

void
foo (unsigned int a, unsigned int b)
{
  if (a >= b)
    bar1 ();
  else if (b <= a)
    bar2 ();
}

The last tree-ssa form looks like so:

foo (a, b)
{
<bb 0>:
  if (a_1 >= b_2) goto <L0>; else goto <L1>;

<L0>:;
  bar1 () [tail call];
  goto <bb 4> (<L3>);

<L1>:;
  if (b_2 <= a_1) goto <L2>; else goto <L3>;

<L2>:;
  bar2 () [tail call];

<L3>:;
  return;

}

Note that the second "if" should go away completely as the two "if"s
are identical except the order of the variables appearing in the condition.

This actually occurs in alias.i.t50.tailc like so:

  if (T.1029_168 >= T.1030_170) goto <L6>; else goto <L2>;

<L2>:;
  if (T.1030_170 <= T.1029_168) goto <L3>; else goto <L4>;

If I have the identical conditions twice, including the order of the variables,
then the second "if" goes away as expected.
Comment 1 Andrew Pinski 2004-06-01 11:20:33 UTC
Confirmed.

Hmm, looks like jump threading needs a little improvement.
Comment 2 Jeffrey A. Law 2004-06-05 20:22:20 UTC
Subject: Re:  Convert "If (a >= b) X else if
	(b <= a) Y" into "if (a >= b) X".

On Tue, 2004-06-01 at 05:20, pinskia at gcc dot gnu dot org wrote:
> ------- Additional Comments From pinskia at gcc dot gnu dot org  2004-06-01 11:20 -------
> Confirmed.
> 
> Hmm, looks like jump threading needs a little improvement.
No.  More correctly, we do not canonicalize equivalent expressions.

What we're looking at is canonicalizing commutative operators and
conditionals so that given two SSA_NAMEs as operands, they will
be automatically ordered such that the lowest SSA_NAME_VERSION 
appears first.  Given an SSA_NAME and a constant, the SSA_NAME
should come first.

A prototype of this (of course) fixes this problem.  I'm still
evaluating that prototype.

jeff

Comment 3 Jeffrey A. Law 2004-06-14 20:43:34 UTC
*** Bug 15758 has been marked as a duplicate of this bug. ***
Comment 4 Jeffrey A. Law 2004-06-16 05:08:15 UTC
Subject: Re:  Convert "If (a >= b) X else if
	(b <= a) Y" into "if (a >= b) X".

On Tue, 2004-06-01 at 05:20, pinskia at gcc dot gnu dot org wrote:
> ------- Additional Comments From pinskia at gcc dot gnu dot org  2004-06-01 11:20 -------
> Confirmed.
> 
> Hmm, looks like jump threading needs a little improvement.
As mentioned previously, this problem is actually a result of GCC
failing to canonicalize conditional expressions.  Without
canonicalization we have no way to determine that

x > y

y < x

Are equivalent conditions.  Not good.

After talking with Andrew MacLeod at the GCC summit I decided to
tackle this by canonicalizing operands within tree-ssa-operands.c.

Basically given a commutative or relational operator with two
SSA_NAMEs as arguments, we canonicalize the expression so that
the SSA_NAME with the lowest version number appears first
(twiddling the relational operator as necessary).

With a canonical form the dominator optimizer is able to detect
the redundancy and optimize appropriately.

The only weakness is that we do not canonicalize immediately after
going into SSA form.  Thus we have to wait until dom2 to actually
discover the useless condition.  Worse things have happened in this
world.

The canonicalization requires minor twiddling to the expected
output in two of the tree-ssa tests.  I've also added a test
derived from the code in this bugzilla report to the tree-ssa
testsuite.

Bootstrapped and regression tested on i686-pc-linux-gnu.


Comment 5 Jeffrey A. Law 2004-06-16 05:08:21 UTC
Created attachment 6537 [details]
ZZZ
Comment 6 Jeffrey A. Law 2004-06-16 05:08:40 UTC
Should be fixed with today's checkin to canonicalize expressions in
get_expr_operands.