On Thu, Nov 29, 2012 at 09:34:31AM +0100, Richard Biener wrote: > Definitely not - that means to not preserve loops until after cprop. The goal > is to preserve loops everywhere!
Yikes, sorry, it wasn't clear to me what PROP_loops really does. Anyway, I think I have a better fix now. The problem is just that when removing BB 4 (which was a header), we have to zap ->header and ->latch. We already have code for this: if (current_loops != NULL && e->src->loop_father->latch == e->src) { /* ??? Now we are creating (or may create) a loop with multiple entries. Simply mark it for removal. Alternatively we could not do this threading. */ e->src->loop_father->header = NULL; e->src->loop_father->latch = NULL; } but the thing is that when there are multiple latch edges, then ->latch is NULL. So we need to keep track of how many latch edges the header has. Regtested/bootstrapped on x86_64, ok for trunk? Can I get rid of may_be_loop_header (and just use n_latch_edges > 0 instead at that one place) in a followup? 2012-11-29 Marek Polacek <pola...@redhat.com> PR middle-end/54838 * cprop.c (bypass_block): Set header and latch to NULL when BB has more than one latch edge. (n_latches): New variable. * gcc.dg/pr54838.c: New test. --- gcc/cprop.c.mp 2012-11-29 15:49:53.120524295 +0100 +++ gcc/cprop.c 2012-11-29 15:50:01.421547832 +0100 @@ -1499,6 +1499,7 @@ bypass_block (basic_block bb, rtx setcc, int may_be_loop_header; unsigned removed_p; unsigned i; + unsigned n_latch_edges; edge_iterator ei; insn = (setcc != NULL) ? setcc : jump; @@ -1510,13 +1511,12 @@ bypass_block (basic_block bb, rtx setcc, if (note) find_used_regs (&XEXP (note, 0), NULL); - may_be_loop_header = false; + n_latch_edges = 0; FOR_EACH_EDGE (e, ei, bb->preds) if (e->flags & EDGE_DFS_BACK) - { - may_be_loop_header = true; - break; - } + n_latch_edges++; + + may_be_loop_header = n_latch_edges > 0; change = 0; for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); ) @@ -1605,7 +1605,8 @@ bypass_block (basic_block bb, rtx setcc, && dest != EXIT_BLOCK_PTR) { if (current_loops != NULL - && e->src->loop_father->latch == e->src) + && (e->src->loop_father->latch == e->src + || n_latch_edges > 1)) { /* ??? Now we are creating (or may create) a loop with multiple entries. Simply mark it for --- gcc/testsuite/gcc.dg/pr54838.c.mp 2012-11-26 14:48:43.783980854 +0100 +++ gcc/testsuite/gcc.dg/pr54838.c 2012-11-26 14:49:51.051158719 +0100 @@ -0,0 +1,24 @@ +/* PR middle-end/54838 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fno-forward-propagate -ftracer" } */ + +void bar (void); + +void +foo (void *b, int *c) +{ +again: + switch (*c) + { + case 1: + if (!b) + { + bar (); + return; + } + goto again; + case 3: + if (!b) + goto again; + } +} Marek