Skip to content

Commit dc1969d

Browse files
committed
loop-invariant: Don't move cold bb instructions to preheader in RTL
gcc/ChangeLog: 2021-12-30 Xionghu Luo <[email protected]> * loop-invariant.c (find_invariants_bb): Check profile count before motion. (find_invariants_body): Add argument. gcc/testsuite/ChangeLog: 2021-12-30 Xionghu Luo <[email protected]> * gcc.dg/loop-invariant-2.c: New.
1 parent be475aa commit dc1969d

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

gcc/loop-invariant.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,9 +1183,21 @@ find_invariants_insn (rtx_insn *insn, bool always_reached, bool always_executed)
11831183
call. */
11841184

11851185
static void
1186-
find_invariants_bb (basic_block bb, bool always_reached, bool always_executed)
1186+
find_invariants_bb (class loop *loop, basic_block bb, bool always_reached,
1187+
bool always_executed)
11871188
{
11881189
rtx_insn *insn;
1190+
basic_block preheader = loop_preheader_edge (loop)->src;
1191+
1192+
/* Don't move insn of cold BB out of loop to preheader to reduce calculations
1193+
and register live range in hot loop with cold BB. */
1194+
if (!always_executed && preheader->count > bb->count)
1195+
{
1196+
if (dump_file)
1197+
fprintf (dump_file, "Don't move invariant from bb: %d out of loop %d\n",
1198+
bb->index, loop->num);
1199+
return;
1200+
}
11891201

11901202
FOR_BB_INSNS (bb, insn)
11911203
{
@@ -1214,8 +1226,7 @@ find_invariants_body (class loop *loop, basic_block *body,
12141226
unsigned i;
12151227

12161228
for (i = 0; i < loop->num_nodes; i++)
1217-
find_invariants_bb (body[i],
1218-
bitmap_bit_p (always_reached, i),
1229+
find_invariants_bb (loop, body[i], bitmap_bit_p (always_reached, i),
12191230
bitmap_bit_p (always_executed, i));
12201231
}
12211232

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* { dg-do compile } */
2+
/* { dg-options "-O2 -fdump-rtl-loop2_invariant" } */
3+
4+
volatile int x;
5+
void
6+
bar (int, char *, char *);
7+
void
8+
foo (int *a, int n, int k)
9+
{
10+
int i;
11+
12+
for (i = 0; i < n; i++)
13+
{
14+
if (__builtin_expect (x, 0))
15+
bar (k / 5, "one", "two");
16+
a[i] = k;
17+
}
18+
}
19+
20+
/* { dg-final { scan-rtl-dump "Don't move invariant from bb: .*out of loop" "loop2_invariant" } } */

0 commit comments

Comments
 (0)