+/* This function is called from loop_version. It splits the entry edge
+ of the loop we want to version, adds the versioning condition, and
+ adjust the edges to the two versions of the loop appropriately.
+ e is an incoming edge. Returns the basic block containing the
+ condition.
+
+ --- edge e ---- > [second_head]
+
+ Split it and insert new conditional expression and adjust edges.
+
+ --- edge e ---> [cond expr] ---> [first_head]
+ |
+ +---------> [second_head]
+
+ THEN_PROB is the probability of then branch of the condition. */
+
+static basic_block
+lv_adjust_loop_entry_edge (basic_block first_head, basic_block second_head,
+ edge e, void *cond_expr, unsigned then_prob)
+{
+ basic_block new_head = NULL;
+ edge e1;
+
+ gcc_assert (e->dest == second_head);
+
+ /* Split edge 'e'. This will create a new basic block, where we can
+ insert conditional expr. */
+ new_head = split_edge (e);
+
+ lv_add_condition_to_bb (first_head, second_head, new_head,
+ cond_expr);
+
+ /* Don't set EDGE_TRUE_VALUE in RTL mode, as it's invalid there. */
+ e = single_succ_edge (new_head);
+ e1 = make_edge (new_head, first_head,
+ current_ir_type () == IR_GIMPLE ? EDGE_TRUE_VALUE : 0);
+ e1->probability = then_prob;
+ e->probability = REG_BR_PROB_BASE - then_prob;
+ e1->count = RDIV (e->count * e1->probability, REG_BR_PROB_BASE);
+ e->count = RDIV (e->count * e->probability, REG_BR_PROB_BASE);
+
+ set_immediate_dominator (CDI_DOMINATORS, first_head, new_head);
+ set_immediate_dominator (CDI_DOMINATORS, second_head, new_head);
+
+ /* Adjust loop header phi nodes. */
+ lv_adjust_loop_header_phi (first_head, second_head, new_head, e1);
+
+ return new_head;
+}
+
+/* Main entry point for Loop Versioning transformation.
+
+ This transformation given a condition and a loop, creates
+ -if (condition) { loop_copy1 } else { loop_copy2 },
+ where loop_copy1 is the loop transformed in one way, and loop_copy2
+ is the loop transformed in another way (or unchanged). 'condition'
+ may be a run time test for things that were not resolved by static
+ analysis (overlapping ranges (anti-aliasing), alignment, etc.).
+
+ THEN_PROB is the probability of the then edge of the if. THEN_SCALE
+ is the ratio by that the frequencies in the original loop should
+ be scaled. ELSE_SCALE is the ratio by that the frequencies in the
+ new loop should be scaled.
+
+ If PLACE_AFTER is true, we place the new loop after LOOP in the
+ instruction stream, otherwise it is placed before LOOP. */
+
+struct loop *
+loop_version (struct loop *loop,
+ void *cond_expr, basic_block *condition_bb,
+ unsigned then_prob, unsigned then_scale, unsigned else_scale,
+ bool place_after)