re PR rtl-optimization/18628 (miscompilation of switch statement in loop)

gcc/ChangeLog:
PR middle-end/18628
* cse.c (fold_rtx_mem): Don't fold a load from a jumptable into a
register.
gcc/testsuite/ChangeLog:
* gcc.dg/pr18628.c: New.

From-SVN: r96445
This commit is contained in:
Alexandre Oliva 2005-03-14 20:06:23 +00:00 committed by Alexandre Oliva
parent aff98faf41
commit 3a3b81e73f
4 changed files with 65 additions and 2 deletions

View File

@ -1,3 +1,9 @@
2005-03-14 Alexandre Oliva <aoliva@redhat.com>
PR middle-end/18628
* cse.c (fold_rtx_mem): Don't fold a load from a jumptable into a
register.
2005-03-14 Alexandre Oliva <aoliva@redhat.com>
PR c++/20280

View File

@ -3515,8 +3515,30 @@ fold_rtx_mem (rtx x, rtx insn)
if (offset >= 0
&& (offset / GET_MODE_SIZE (GET_MODE (table))
< XVECLEN (table, 0)))
return XVECEXP (table, 0,
offset / GET_MODE_SIZE (GET_MODE (table)));
{
rtx label = XVECEXP
(table, 0, offset / GET_MODE_SIZE (GET_MODE (table)));
rtx set;
/* If we have an insn that loads the label from the
jumptable into a reg, we don't want to set the reg
to the label, because this may cause a reference to
the label to remain after the label is removed in
some very obscure cases (PR middle-end/18628). */
if (!insn)
return label;
set = single_set (insn);
if (! set || SET_SRC (set) != x)
return x;
/* If it's a jump, it's safe to reference the label. */
if (SET_DEST (set) == pc_rtx)
return label;
return x;
}
}
if (table_insn && JUMP_P (table_insn)
&& GET_CODE (PATTERN (table_insn)) == ADDR_DIFF_VEC)

View File

@ -1,3 +1,7 @@
2005-03-14 Alexandre Oliva <aoliva@redhat.com>
* gcc.dg/pr18628.c: New.
2005-03-14 Alexandre Oliva <aoliva@redhat.com>
PR c++/20280

View File

@ -0,0 +1,31 @@
/* { dg-do link } */
/* { dg-options "-O2" } */
/* PR middle-end/18628 exposed a problem in which cse folded a load
from a jump table into the label that was the target of the branch.
Unfortunately, the indirect jump was moved to a different basic
block, and the LABEL_REF copied to the register wasn't enough to
keep the cfg from optimizing the otherwise-unused label away. So
we ended up with a dangling reference to the label. */
int i;
int main()
{
for (;;)
{
switch (i)
{
case 0:
case 1:
return 1;
case 2:
case 3:
return 0;
case 5:
--i;
}
}
}