[PATCH] Simplify (cond (compare x y) 0) into (cond x y)
Roger Sayle
roger@eyesopen.com
Fri May 24 17:11:00 GMT 2002
On Fri, 24 May 2002, Richard Henderson wrote:
> On Sat, May 18, 2002 at 06:07:38PM -0600, Roger Sayle wrote:
> > * simplify-rtx.c (simplify_gen_relational): Simplify the RTX
> > (cond (compare x y) 0) into the equivalent (cond x y).
>
> It certainly looks correct, but I tested this with an additional
> warning annotation, and this never triggered on an i686 build.
>
> What case is this supposed to catch?
Its required by my "jump bypass optimization" and to fix GCSE's cprop.
The problem is that on architectures with condition codes, such as the
x86, when we start with RTL such as
A: (set (reg:SI 1) (const_int 2 [0x2]))
B: (set (cc0) (compare (reg:SI 1) (const_int 3 [0x3])))
C: (set (pc) (if_then_else (ne (cc0) (const_int 0 [0x0]))
(label_ref 2) (pc)))
The current behaviour is to substitute A in B, and then try and
substitute the result into C (see the code in gcse:cprop_cc0_jump).
Unfortunately, the result of simplify_replace_rtx of "(reg:SI 1)"
with "(const_int 2 [0x02])" in instruction B is the invalid RTL,
"(set (cc0) (compare (const_int 2 [0x02]) (const_int 3 [0x03])))".
This is invalid because the mode of the comparison has now been
lost, as well as the actual comparison operator, and so the constant
is never propagated into the jump instruction.
My solution, both for HAVE_cc0 targets and targets with MODE_CC
registers, is to identify condition code setters followed by
conditional jumps, and the substitute B into C first, and then
A into the resulting instruction.
So simplify_replace_rtx substitutes "(compare (reg:SI 1) (const_int
3 [0x03]))" for "(cc0)" in C, which together with the posted patch
should eventually lead to
(set (pc) (if_then_else (ne (reg:SI 1) (const_int 3 [0x03]))))
Now when "(reg:SI 1)" is substituted for "(const_int 2 [0x02])"
with simplify_replace_rtx, the above jump_insn reduces to just
"(set (pc) (pc))". Which is recognized as unconditional.
With the exception of the posted patch, all of the above changes
are local to gcse.c's cprop implementation. Hence, I thought it
best to confirm that the required simplify_replace_rtx changes
were valid, and didn't cause problems with other passes or on
other targets. If you'd prefer to review both patches together,
my "cprop and jump bypass" patch is ready to be submitted to
gcc-patches. Andreas has even done some SPEC testing for me.
I hope this explains the motivation.
Roger
--
More information about the Gcc-patches
mailing list