Bootstrapped and tested on x86_64-unknown-linux-gnu, applied. Richard.
2017-05-19 Richard Biener <rguent...@suse.de> PR middle-end/80764 * cfgexpand.c (expand_gimple_cond): Fix loop fixup. * gcc.dg/torture/pr80764.c: New testcase. Index: gcc/cfgexpand.c =================================================================== --- gcc/cfgexpand.c (revision 248094) +++ gcc/cfgexpand.c (working copy) @@ -2503,7 +2503,10 @@ expand_gimple_cond (basic_block bb, gcon false_edge->flags |= EDGE_FALLTHRU; new_bb->count = false_edge->count; new_bb->frequency = EDGE_FREQUENCY (false_edge); - add_bb_to_loop (new_bb, bb->loop_father); + add_bb_to_loop (new_bb, dest->loop_father); + if (bb->loop_father->latch == bb + && bb->loop_father->header == dest) + bb->loop_father->latch = new_bb; new_edge = make_edge (new_bb, dest, 0); new_edge->probability = REG_BR_PROB_BASE; new_edge->count = new_bb->count; Index: gcc/testsuite/gcc.dg/torture/pr80764.c =================================================================== --- gcc/testsuite/gcc.dg/torture/pr80764.c (nonexistent) +++ gcc/testsuite/gcc.dg/torture/pr80764.c (working copy) @@ -0,0 +1,14 @@ +/* { dg-do compile } */ + +int b, d, f, h; +char e; +int fn1(); +int main() { return fn1(); } +int fn1(int p1) { + for (; d;) + for (; e < 3;) { + for (; h;) + b = fn1(0); + return f; + } +}