https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120747
--- Comment #23 from Richard Biener <rguenth at gcc dot gnu.org> ---
(In reply to Filip Kastl from comment #22)
> Hmm, but the patch makes a few testsuite tests fail. So that would have to
> be sorted out.
>
> +FAIL: gcc.dg/optimize-bswapdi-3.c scan-tree-dump-times bswap "64 bit bswap
> implementation found at" 3
> +FAIL: gcc.dg/optimize-bswapsi-2.c scan-tree-dump-times bswap "32 bit bswap
> implementation found at" 4
> +FAIL: gcc.dg/optimize-bswapsi-7.c scan-tree-dump-times optimized "=
> __builtin_bswap32 \\\\(" 4
> +FAIL: gcc.dg/tree-ssa/pr38533.c scan-tree-dump-times reassoc1
> "__asm__[^;\\n]*;\\n *__asm__" 1
> +FAIL: gcc.dg/tree-ssa/reassoc-20.c scan-tree-dump-times optimized " \\\\+ "
> 2
> +FAIL: gcc.dg/tree-ssa/reassoc-4.c scan-tree-dump-times optimized "\\\\+" 2
The stabilization "hacks" are not so much to stabilize the sort but to order
"equal" operands in the order they are defined in the instruction stream which
will "nicely" shuffle stmts upon code generating the associated chain across
the original positions rather than piling in some place. This boils down
to how we linearize the expression tree initially, which does _not_ order
operands in this way.
That said, the order is pretty random for same rank and apart from sorting
same operands together the actual order shouldn't matter much. But what is
"bad luck" in this case could be "good luck" in another. The op->id order
is similarly random due to visitation order in linearize_tree being
dependent on operand order and operands are eventually sorted after
SSA_NAME_VERSION as well.
Instead of altering the initial order what would be a good thing would be
to, within same-operand-rank operands, try to re-use existing computations.
That might conflict with linearization and applying reassoc-width, of course.
But original leafs might be unchanged and could be preserved. Preserving
as much as possible would be relevant when reassoc is used on
signed integer (TYPE_OVERFLOW_UNDEFINED) chains, where unchanged leafs
could be left signed but all others would need to be promoted unsigned
for correctness reasons.