Starting with r16-4438-ga93f80feeef744, the edges started to be sorted
in ascending order.  Thus the most likely branches were deprioritized
instead of optimized to fallthroughs.

Fix by restoring the sorting order prior to r16-4438-ga93f80feeef744.

There are no regressions for C and C++ on x86_64-pc-linux-gnu.

I struggle to write a test case.  Suggestions how to check the BB order
are welcome.

Ok for trunk?

        PR rtl-optimization/122675

gcc/ChangeLog:

        * bb-reorder.cc (edge_order): Fix BB edge ordering to be
        descending.

Signed-off-by: Dimitar Dimitrov <[email protected]>
---
 gcc/bb-reorder.cc | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/gcc/bb-reorder.cc b/gcc/bb-reorder.cc
index e4efdee0b16..43fa019984a 100644
--- a/gcc/bb-reorder.cc
+++ b/gcc/bb-reorder.cc
@@ -2392,7 +2392,12 @@ edge_order (const void *ve1, const void *ve2)
   gcov_type gc1 = c1.initialized_p () ? c1.to_gcov_type () : 0;
   gcov_type gc2 = c2.initialized_p () ? c2.to_gcov_type () : 0;
   gcov_type m = MAX (gc1, gc2);
-  return (m == gc1) - (m == gc2);
+  /* While gcc_stablesort sorts in ascending order, the edges should
+     be sorted in descending order of their execution frequency.
+     So return a reversed comparison.  Expressed with a spaceship operator:
+       return gc2 <=> gc1;
+   */
+  return (m == gc2) - (m == gc1);
 }
 
 /* Reorder basic blocks using the "simple" algorithm.  This tries to
-- 
2.51.1

Reply via email to