+ /* Collect all outs, including keeps.
+ * (TODO firm function for number of out edges?) */
+ foreach_out_edge_kind(block, edge, EDGE_KIND_NORMAL) {
+ ++nodes_n;
+ }
+
+ /* We do not want to collect more nodes from condition chains, than the limit allows us to.
+ * Also, leave at least one block as body. */
+ if (head_inversion_node_count + nodes_n > inversion_head_node_limit
+ || head_inversion_block_count + 1 == loop_info.blocks) {
+ set_Block_mark(block, 0);
+
+ return 0;
+ }
+
+ /* First: check our successors, and add all succs that are outside of the loop to the list */
+ foreach_block_succ(block, edge) {
+ ir_node *src = get_edge_src_irn( edge );
+ int pos = get_edge_src_pos( edge );
+
+ if (!is_in_loop(src)) {
+ out_edge entry;
+
+ mark = 1;
+ entry.node = src;
+ entry.pred_irn_n = pos;
+ ARR_APP1(out_edge, cond_chain_entries, entry);
+ mark_irn_visited(src);
+ }
+ }
+
+ if (mark == 0) {
+ /* this block is not part of the chain,
+ * because the chain would become too long or we have no successor outside of the loop */
+
+ set_Block_mark(block, 0);
+ return 0;
+ } else {
+ set_Block_mark(block, 1);
+ ++head_inversion_block_count;
+ DB((dbg, LEVEL_5, "block %ld is part of condition chain\n", get_irn_node_nr(block)));
+ head_inversion_node_count += nodes_n;
+ }
+
+ /* Second: walk all successors, and add them to the list if they are not part of the chain */
+ foreach_block_succ(block, edge) {
+ unsigned inchain;
+ ir_node *src = get_edge_src_irn( edge );
+ int pos = get_edge_src_pos( edge );
+
+ /* already done cases */
+ if (!is_in_loop( src ) || (get_irn_visited(src) >= get_irg_visited(current_ir_graph))) {
+ continue;
+ }
+
+ mark_irn_visited(src);
+ DB((dbg, LEVEL_5, "condition chain walk %ld\n", get_irn_node_nr(src)));
+ inchain = find_condition_chains(src);
+
+ /* if successor is not part of chain we need to collect its outs */
+ if (!inchain) {
+ out_edge entry;
+ entry.node = src;
+ entry.pred_irn_n = pos;
+ ARR_APP1(out_edge, cond_chain_entries, entry);
+ }
+ }
+ return mark;
+}
+
+/**
+ * Rewire the loop head and inverted head for loop inversion.
+ */
+static void inversion_fix_heads(void)
+{
+ ir_node **loopheadnins, **invheadnins;
+ ir_node *loophead = loop_cf_head;
+ ir_node *invhead = get_copy(loophead);
+
+ int headarity = get_irn_arity(loophead);
+ ir_node *phi;
+ int i;