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.
Confirmed. Hmm, looks like jump threading needs a little improvement.
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
*** Bug 15758 has been marked as a duplicate of this bug. ***
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.
Created attachment 6537 [details] ZZZ
Should be fixed with today's checkin to canonicalize expressions in get_expr_operands.