Skip to content
This repository was archived by the owner on Jan 31, 2024. It is now read-only.

Commit 46bfe1b

Browse files
committed
Fix incorrect loop exit edge probability [PR103270]
r12-4526 cancelled jump thread path rotates loop. It exposes a issue in profile-estimate when predict_extra_loop_exits, outer loop's exit edge is marked as inner loop's extra loop exit and set with incorrect prediction, then a hot inner loop will become cold loop finally through optimizations, this patch add loop check when searching extra exit edges to avoid unexpected predict_edge from predict_paths_for_bb. Regression tested on P8LE. gcc/ChangeLog: 2021-12-21 Xionghu Luo <[email protected]> PR middle-end/103270 * predict.c (predict_extra_loop_exits): Add loop parameter. (predict_loops): Call with loop argument. gcc/testsuite/ChangeLog: 2021-12-21 Xionghu Luo <[email protected]> PR middle-end/103270 * gcc.dg/pr103270.c: New test.
1 parent 460d53f commit 46bfe1b

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

gcc/predict.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1859,7 +1859,7 @@ predict_iv_comparison (class loop *loop, basic_block bb,
18591859
exits to predict them using PRED_LOOP_EXTRA_EXIT. */
18601860

18611861
static void
1862-
predict_extra_loop_exits (edge exit_edge)
1862+
predict_extra_loop_exits (class loop *loop, edge exit_edge)
18631863
{
18641864
unsigned i;
18651865
bool check_value_one;
@@ -1912,12 +1912,14 @@ predict_extra_loop_exits (edge exit_edge)
19121912
continue;
19131913
if (EDGE_COUNT (e->src->succs) != 1)
19141914
{
1915-
predict_paths_leading_to_edge (e, PRED_LOOP_EXTRA_EXIT, NOT_TAKEN);
1915+
predict_paths_leading_to_edge (e, PRED_LOOP_EXTRA_EXIT, NOT_TAKEN,
1916+
loop);
19161917
continue;
19171918
}
19181919

19191920
FOR_EACH_EDGE (e1, ei, e->src->preds)
1920-
predict_paths_leading_to_edge (e1, PRED_LOOP_EXTRA_EXIT, NOT_TAKEN);
1921+
predict_paths_leading_to_edge (e1, PRED_LOOP_EXTRA_EXIT, NOT_TAKEN,
1922+
loop);
19211923
}
19221924
}
19231925

@@ -2008,7 +2010,7 @@ predict_loops (void)
20082010
ex->src->index, ex->dest->index);
20092011
continue;
20102012
}
2011-
predict_extra_loop_exits (ex);
2013+
predict_extra_loop_exits (loop, ex);
20122014

20132015
if (number_of_iterations_exit (loop, ex, &niter_desc, false, false))
20142016
niter = niter_desc.niter;

gcc/testsuite/gcc.dg/pr103270.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* { dg-do compile } */
2+
/* { dg-options "-O2 -fdump-tree-profile_estimate" } */
3+
4+
void test(int a, int* i)
5+
{
6+
for (; a < 5; ++a)
7+
{
8+
int b = 0;
9+
int c = 0;
10+
for (; b != -11; b--)
11+
for (int d = 0; d ==0; d++)
12+
{
13+
*i += c & a;
14+
c = b;
15+
}
16+
}
17+
}
18+
19+
/* { dg-final { scan-tree-dump-not "extra loop exit heuristics of edge\[^:\]*:" "profile_estimate"} } */

0 commit comments

Comments
 (0)