From 3a3b81e73f2f4b05740199db410f8b37df46be4f Mon Sep 17 00:00:00 2001 From: Alexandre Oliva Date: Mon, 14 Mar 2005 20:06:23 +0000 Subject: [PATCH] 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 --- gcc/ChangeLog | 6 ++++++ gcc/cse.c | 26 ++++++++++++++++++++++++-- gcc/testsuite/ChangeLog | 4 ++++ gcc/testsuite/gcc.dg/pr18628.c | 31 +++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/pr18628.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 910eb398c9f..1e0a0f48f74 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2005-03-14 Alexandre Oliva + + 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 PR c++/20280 diff --git a/gcc/cse.c b/gcc/cse.c index d7f3027e2bb..fd5e21ac2ee 100644 --- a/gcc/cse.c +++ b/gcc/cse.c @@ -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) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 4e18492dbab..3053e823e8f 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2005-03-14 Alexandre Oliva + + * gcc.dg/pr18628.c: New. + 2005-03-14 Alexandre Oliva PR c++/20280 diff --git a/gcc/testsuite/gcc.dg/pr18628.c b/gcc/testsuite/gcc.dg/pr18628.c new file mode 100644 index 00000000000..d365075b729 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr18628.c @@ -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; + } + } +} -- 2.30.2