2019-08-30 Jakub Jelinek Backported from mainline 2019-01-17 Jakub Jelinek PR rtl-optimization/88870 * dce.c (deletable_insn_p): Never delete const/pure calls that can throw if we can't alter the cfg or delete dead exceptions. (mark_insn): Don't call find_call_stack_args for such calls. * gcc.dg/pr88870.c: New test. --- gcc/dce.c (revision 268620) +++ gcc/dce.c (revision 268621) @@ -108,7 +108,10 @@ deletable_insn_p (rtx_insn *insn, bool f /* We can delete dead const or pure calls as long as they do not infinite loop. */ && (RTL_CONST_OR_PURE_CALL_P (insn) - && !RTL_LOOPING_CONST_OR_PURE_CALL_P (insn))) + && !RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)) + /* Don't delete calls that may throw if we cannot do so. */ + && ((cfun->can_delete_dead_exceptions && can_alter_cfg) + || insn_nothrow_p (insn))) return find_call_stack_args (as_a (insn), false, fast, arg_stores); @@ -200,7 +203,9 @@ mark_insn (rtx_insn *insn, bool fast) && !df_in_progress && !SIBLING_CALL_P (insn) && (RTL_CONST_OR_PURE_CALL_P (insn) - && !RTL_LOOPING_CONST_OR_PURE_CALL_P (insn))) + && !RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)) + && ((cfun->can_delete_dead_exceptions && can_alter_cfg) + || insn_nothrow_p (insn))) find_call_stack_args (as_a (insn), true, fast, NULL); } } --- gcc/testsuite/gcc.dg/pr88870.c (nonexistent) +++ gcc/testsuite/gcc.dg/pr88870.c (revision 268621) @@ -0,0 +1,23 @@ +/* PR rtl-optimization/88870 */ +/* { dg-do compile } */ +/* { dg-options "-O1 -fexceptions -fnon-call-exceptions -ftrapv -fno-tree-dominator-opts" } */ + +int a, b; + +void +foo (int *x) +{ + int c = 0; + { + int d; + x = &c; + for (;;) + { + x = &d; + b = 0; + d = c + 1; + b = c = 1; + ++a; + } + } +}