2017-12-18 Jakub Jelinek PR debug/83419 * tree-iterator.c: Include options.h. (alloc_stmt_list): Clear TREE_SIDE_EFFECTS for a new node. (tsi_link_after): Set TREE_SIDE_EFFECTS when adding STATEMENT_LIST with TREE_SIDE_EFFECTS or non-DEBUG_BEGIN_STMT. (tsi_link_before): Likewise. Formatting fix. (tsi_delink): Recompute TREE_SIDE_EFFECTS on removal. * gimplify.c (gimplify_statement_list): Clear TREE_SIDE_EFFECTS. * gcc.dg/pr83419.c: New test. --- gcc/tree-iterator.c.jj 2017-12-12 09:48:26.000000000 +0100 +++ gcc/tree-iterator.c 2017-12-18 13:44:02.330719440 +0100 @@ -23,6 +23,7 @@ along with GCC; see the file COPYING3. #include "coretypes.h" #include "tree.h" #include "tree-iterator.h" +#include "options.h" /* This is a cache of STATEMENT_LIST nodes. We create and destroy them @@ -41,7 +42,10 @@ alloc_stmt_list (void) TREE_SET_CODE (list, STATEMENT_LIST); } else - list = make_node (STATEMENT_LIST); + { + list = make_node (STATEMENT_LIST); + TREE_SIDE_EFFECTS (list) = 0; + } TREE_TYPE (list) = void_type_node; return list; } @@ -127,6 +131,8 @@ tsi_link_before (tree_stmt_iterator *i, gcc_assert (head == tail); return; } + if (TREE_SIDE_EFFECTS (t)) + TREE_SIDE_EFFECTS (i->container) = 1; } else { @@ -135,11 +141,10 @@ tsi_link_before (tree_stmt_iterator *i, head->next = NULL; head->stmt = t; tail = head; + if (TREE_CODE (t) != DEBUG_BEGIN_STMT) + TREE_SIDE_EFFECTS (i->container) = 1; } - if (TREE_CODE (t) != DEBUG_BEGIN_STMT) - TREE_SIDE_EFFECTS (i->container) = 1; - cur = i->ptr; /* Link it into the list. */ @@ -157,9 +162,9 @@ tsi_link_before (tree_stmt_iterator *i, { head->prev = STATEMENT_LIST_TAIL (i->container); if (head->prev) - head->prev->next = head; + head->prev->next = head; else - STATEMENT_LIST_HEAD (i->container) = head; + STATEMENT_LIST_HEAD (i->container) = head; STATEMENT_LIST_TAIL (i->container) = tail; } @@ -204,6 +209,9 @@ tsi_link_after (tree_stmt_iterator *i, t gcc_assert (head == tail); return; } + + if (TREE_SIDE_EFFECTS (t)) + TREE_SIDE_EFFECTS (i->container) = 1; } else { @@ -212,10 +220,10 @@ tsi_link_after (tree_stmt_iterator *i, t head->next = NULL; head->stmt = t; tail = head; - } - if (TREE_CODE (t) != DEBUG_BEGIN_STMT) - TREE_SIDE_EFFECTS (i->container) = 1; + if (TREE_CODE (t) != DEBUG_BEGIN_STMT) + TREE_SIDE_EFFECTS (i->container) = 1; + } cur = i->ptr; @@ -275,10 +283,30 @@ tsi_delink (tree_stmt_iterator *i) else STATEMENT_LIST_TAIL (i->container) = prev; - if (!next && !prev) - TREE_SIDE_EFFECTS (i->container) = 0; - i->ptr = next; + + if (!MAY_HAVE_DEBUG_MARKER_STMTS) + TREE_SIDE_EFFECTS (i->container) = 0; + else if (TREE_SIDE_EFFECTS (i->container)) + { + while (next || prev) + { + if (next) + { + if (TREE_CODE (next->stmt) != DEBUG_BEGIN_STMT) + break; + next = next->next; + } + if (prev) + { + if (TREE_CODE (prev->stmt) != DEBUG_BEGIN_STMT) + break; + prev = prev->prev; + } + } + if (next == NULL && prev == NULL) + TREE_SIDE_EFFECTS (i->container) = 0; + } } /* Return the first expression in a sequence of COMPOUND_EXPRs, or in --- gcc/gimplify.c.jj 2017-12-14 21:11:40.000000000 +0100 +++ gcc/gimplify.c 2017-12-18 10:17:07.922556324 +0100 @@ -1763,6 +1763,9 @@ gimplify_statement_list (tree *expr_p, g tree_stmt_iterator i = tsi_start (*expr_p); + /* No need to recompute TREE_SIDE_EFFECTS on removal, we are going to + delink everything. */ + TREE_SIDE_EFFECTS (*expr_p) = 0; while (!tsi_end_p (i)) { gimplify_stmt (tsi_stmt_ptr (i), pre_p); --- gcc/testsuite/gcc.dg/pr83419.c.jj 2017-12-18 10:08:24.214911480 +0100 +++ gcc/testsuite/gcc.dg/pr83419.c 2017-12-18 10:08:24.214911480 +0100 @@ -0,0 +1,16 @@ +/* PR debug/83419 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fcompare-debug" } */ + +int a, b; +void foo (int, ...); + +void +bar (void) +{ + if (a || 1 == b) + foo (1); + else + 0; + foo (1, 0); +}