Skip to content

Commit 1820137

Browse files
committed
shrink-wrapping: Fix up prologue block discovery [PR103860]
The following testcase is miscompiled, because a prologue which contains subq $8, %rsp instruction is emitted at the start of a basic block which contains conditional jump that depends on flags register set in an earlier basic block, the prologue instruction then clobbers those flags. Normally this case is checked by can_get_prologue predicate, but this is done only at the start of the loop. If we update pro later in the loop (because some bb shouldn't be duplicated) and then don't push anything further into vec and the vec is already empty (this can happen when the new pro is already in bb_with bitmask and either has no successors (that is the case in the testcase where that bb ends with a trap) or all the successors are already in bb_with, then the loop doesn't iterate further and can_get_prologue will not be checked. The following simple patch makes sure we call can_get_prologue even after the last former iteration when vec is already empty and only break from the loop afterwards (and only if the updating of pro done because of !can_get_prologue didn't push anything into vec again). 2021-12-30 Jakub Jelinek <[email protected]> PR rtl-optimization/103860 * shrink-wrap.c (try_shrink_wrapping): Make sure can_get_prologue is called on pro even if nothing further is pushed into vec. * gcc.dg/pr103860.c: New test.
1 parent dc1969d commit 1820137

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

gcc/shrink-wrap.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ try_shrink_wrapping (edge *entry_edge, rtx_insn *prologue_seq)
781781
unsigned max_grow_size = get_uncond_jump_length ();
782782
max_grow_size *= param_max_grow_copy_bb_insns;
783783

784-
while (!vec.is_empty () && pro != entry)
784+
while (pro != entry)
785785
{
786786
while (pro != entry && !can_get_prologue (pro, prologue_clobbered))
787787
{
@@ -791,6 +791,9 @@ try_shrink_wrapping (edge *entry_edge, rtx_insn *prologue_seq)
791791
vec.quick_push (pro);
792792
}
793793

794+
if (vec.is_empty ())
795+
break;
796+
794797
basic_block bb = vec.pop ();
795798
if (!can_dup_for_shrink_wrapping (bb, pro, max_grow_size))
796799
while (!dominated_by_p (CDI_DOMINATORS, bb, pro))

gcc/testsuite/gcc.dg/pr103860.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* PR rtl-optimization/103860 */
2+
/* { dg-do run } */
3+
/* { dg-options "-O3" } */
4+
/* { dg-additional-options "-fPIC" { target fpic } } */
5+
6+
static int d, *e;
7+
int f;
8+
9+
__attribute__((noinline)) signed char
10+
foo (signed char b, signed char c)
11+
{
12+
return b + c;
13+
}
14+
15+
int
16+
main ()
17+
{
18+
signed char l;
19+
for (l = -1; l; l = foo (l, 1))
20+
{
21+
while (d < 0)
22+
;
23+
if (d > 0)
24+
{
25+
f = 0;
26+
*e = 0;
27+
}
28+
}
29+
d = 0;
30+
return 0;
31+
}

0 commit comments

Comments
 (0)