Skip to content

Commit

Permalink
loop-invariant: Don't move cold bb instructions to preheader in RTL
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
xionghul committed Dec 30, 2021
1 parent be475aa commit dc1969d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
17 changes: 14 additions & 3 deletions gcc/loop-invariant.c
Original file line number Diff line number Diff line change
Expand Up @@ -1183,9 +1183,21 @@ find_invariants_insn (rtx_insn *insn, bool always_reached, bool always_executed)
call. */

static void
find_invariants_bb (basic_block bb, bool always_reached, bool always_executed)
find_invariants_bb (class loop *loop, basic_block bb, bool always_reached,
bool always_executed)
{
rtx_insn *insn;
basic_block preheader = loop_preheader_edge (loop)->src;

/* Don't move insn of cold BB out of loop to preheader to reduce calculations
and register live range in hot loop with cold BB. */
if (!always_executed && preheader->count > bb->count)
{
if (dump_file)
fprintf (dump_file, "Don't move invariant from bb: %d out of loop %d\n",
bb->index, loop->num);
return;
}

FOR_BB_INSNS (bb, insn)
{
Expand Down Expand Up @@ -1214,8 +1226,7 @@ find_invariants_body (class loop *loop, basic_block *body,
unsigned i;

for (i = 0; i < loop->num_nodes; i++)
find_invariants_bb (body[i],
bitmap_bit_p (always_reached, i),
find_invariants_bb (loop, body[i], bitmap_bit_p (always_reached, i),
bitmap_bit_p (always_executed, i));
}

Expand Down
20 changes: 20 additions & 0 deletions gcc/testsuite/gcc.dg/loop-invariant-2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* { dg-do compile } */
/* { dg-options "-O2 -fdump-rtl-loop2_invariant" } */

volatile int x;
void
bar (int, char *, char *);
void
foo (int *a, int n, int k)
{
int i;

for (i = 0; i < n; i++)
{
if (__builtin_expect (x, 0))
bar (k / 5, "one", "two");
a[i] = k;
}
}

/* { dg-final { scan-rtl-dump "Don't move invariant from bb: .*out of loop" "loop2_invariant" } } */

0 comments on commit dc1969d

Please sign in to comment.