===================================================================
@@ -1674,8 +1678,12 @@ gimple_can_merge_blocks_p (basic_block a
return false;
}
- /* Protect the loop latches. */
- if (current_loops && b->loop_father->latch == b)
+ /* Protect simple loop latches. We only want to avoid merging
+ the latch with the loop header in this case. */
+ if (current_loops
+ && b->loop_father->latch == b
+ && loops_state_satisfies_p (LOOPS_HAVE_SIMPLE_LATCHES)
+ && b->loop_father->header == a)
return false;
/* It must be possible to eliminate all phi nodes in B. If ssa form
===================================================================
@@ -766,6 +766,11 @@ merge_blocks (basic_block a, basic_block
add_bb_to_loop (a, b->loop_father);
a->loop_father->header = a;
}
+ /* If we merge a loop latch into its predecessor, update the loop
+ structure. */
+ if (b->loop_father->latch
+ && b->loop_father->latch == b)
+ b->loop_father->latch = a;
remove_bb_from_loops (b);
}
===================================================================
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-ch-details" } */
+
+int mymax2(int *it, int *end)
+{
+ int max = *it;
+ while (++it != end)
+ if (*it > max)
+ max = *it;
+ return max;
+}
+
+/* { dg-final { scan-tree-dump "Duplicating header" "ch" } } */
+/* { dg-final { cleanup-tree-dump "ch" } } */