+ /* If this is a phi of the loophead shared by more than 1 loop,
+ * we need to check if all defs are not in the loop. */
+ if (is_Phi(node)) {
+ ir_node *block;
+ block = get_nodes_block(node);
+
+ /* To prevent unexpected situations. */
+ if (block != loop_head) {
+ return 0;
+ }
+
+ for (i = 0; i < get_irn_arity(node); ++i) {
+ /* Check if all bes are just loopbacks. */
+ if (is_own_backedge(block, i) && get_irn_n(node, i) != node)
+ return 0;
+ }
+ DB((dbg, LEVEL_4, "invar %N\n", node));
+ return 1;
+ }
+ DB((dbg, LEVEL_4, "Not invar %N\n", node));
+
+ return 0;
+}
+
+/* Returns 1 if one pred of node is invariant and the other is not.
+ * invar_pred and other are set analogously. */
+static unsigned get_invariant_pred(ir_node *node, ir_node **invar_pred, ir_node **other)
+{
+ ir_node *pred0 = get_irn_n(node, 0);
+ ir_node *pred1 = get_irn_n(node, 1);
+
+ *invar_pred = NULL;
+ *other = NULL;
+
+ if (is_loop_invariant_def(pred0)) {
+ DB((dbg, LEVEL_4, "pred0 invar %N\n", pred0));
+ *invar_pred = pred0;
+ *other = pred1;
+ }
+
+ if (is_loop_invariant_def(pred1)) {
+ DB((dbg, LEVEL_4, "pred1 invar %N\n", pred1));
+
+ if (*invar_pred != NULL) {
+ /* RETURN. We do not want both preds to be invariant. */
+ return 0;
+ }
+
+ *other = pred0;
+ *invar_pred = pred1;
+ return 1;
+ } else {
+ DB((dbg, LEVEL_4, "pred1 not invar %N\n", pred1));
+
+ if (*invar_pred != NULL)
+ return 1;
+ else
+ return 0;
+ }
+}
+
+/* Starts from a phi that may belong to an iv.
+ * If an add forms a loop with iteration_phi,
+ * and add uses a constant, 1 is returned
+ * and 'start' as well as 'add' are sane. */
+static unsigned get_start_and_add(ir_node *iteration_phi, unrolling_kind_flag role)
+{
+ int i;
+ ir_node *found_add = loop_info.add;
+ int arity = get_irn_arity(iteration_phi);
+
+ DB((dbg, LEVEL_4, "Find start and add from %N\n", iteration_phi));
+
+ for (i = 0; i < arity; ++i) {
+
+ /* Find start_val which needs to be pred of the iteration_phi.
+ * If start_val already known, sanity check. */
+ if (!is_backedge(get_nodes_block(loop_info.iteration_phi), i)) {
+ ir_node *found_start_val = get_irn_n(loop_info.iteration_phi, i);
+
+ DB((dbg, LEVEL_4, "found_start_val %N\n", found_start_val));
+
+ /* We already found a start_val it has to be always the same. */
+ if (loop_info.start_val && found_start_val != loop_info.start_val)
+ return 0;
+
+ if ((role == constant) && !(is_SymConst(found_start_val) || is_Const(found_start_val)))
+ return 0;
+ else if((role == constant) && !(is_loop_invariant_def(found_start_val)))
+ return 0;
+
+ loop_info.start_val = found_start_val;
+ }
+
+ /* The phi has to be in the loop head.
+ * Follow all own backedges. Every value supplied from these preds of the phi
+ * needs to origin from the same add. */
+ if (is_own_backedge(get_nodes_block(loop_info.iteration_phi), i)) {
+ ir_node *new_found = get_irn_n(loop_info.iteration_phi,i);
+
+ DB((dbg, LEVEL_4, "is add? %N\n", new_found));
+
+ if (! (is_Add(new_found) || is_Sub(new_found)) || (found_add && found_add != new_found))
+ return 0;
+ else
+ found_add = new_found;
+ }
+ }
+
+ loop_info.add = found_add;
+
+ return 1;
+}
+
+
+/* Returns 1 if one pred of node is a const value and the other is not.
+ * const_pred and other are set analogously. */
+static unsigned get_const_pred(ir_node *node, ir_node **const_pred, ir_node **other)
+{
+ ir_node *pred0 = get_irn_n(node, 0);
+ ir_node *pred1 = get_irn_n(node, 1);
+
+ DB((dbg, LEVEL_4, "Checking for constant pred of %N\n", node));
+
+ *const_pred = NULL;
+ *other = NULL;
+
+ /*DB((dbg, LEVEL_4, "is %N const\n", pred0));*/
+ if (is_Const(pred0) || is_SymConst(pred0)) {
+ *const_pred = pred0;
+ *other = pred1;
+ }
+
+ /*DB((dbg, LEVEL_4, "is %N const\n", pred1));*/
+ if (is_Const(pred1) || is_SymConst(pred1)) {
+ if (*const_pred != NULL) {
+ /* RETURN. We do not want both preds to be constant. */
+ return 0;
+ }
+
+ *other = pred0;
+ *const_pred = pred1;
+ }
+
+ if (*const_pred == NULL)
+ return 0;
+ else
+ return 1;
+}
+
+/* Returns the mathematically inverted pn_Cmp. */
+static pn_Cmp get_math_inverted_case(pn_Cmp proj)
+{
+ switch(proj) {
+ case pn_Cmp_Eq:
+ return pn_Cmp_Lg;
+ case pn_Cmp_Lg:
+ return pn_Cmp_Eq;
+ case pn_Cmp_Lt:
+ return pn_Cmp_Ge;
+ case pn_Cmp_Le:
+ return pn_Cmp_Gt;
+ case pn_Cmp_Gt:
+ return pn_Cmp_Le;
+ case pn_Cmp_Ge:
+ return pn_Cmp_Lt;
+ default:
+ panic("Unhandled pn_Cmp.");
+ }
+}
+
+/* Returns 1 if loop exits within 2 steps of the iv.
+ * Norm_proj means we do not exit the loop.*/
+static unsigned simulate_next(ir_tarval **count_tar,
+ ir_tarval *stepped, ir_tarval *step_tar, ir_tarval *end_tar,
+ pn_Cmp norm_proj)
+{
+ ir_tarval *next;
+
+ DB((dbg, LEVEL_4, "Loop taken if (stepped)%ld %s (end)%ld ",
+ get_tarval_long(stepped),
+ get_pnc_string((norm_proj)),
+ get_tarval_long(end_tar)));
+ DB((dbg, LEVEL_4, "comparing latest value %d\n", loop_info.latest_value));
+
+ /* If current iv does not stay in the loop,
+ * this run satisfied the exit condition. */
+ if (! (tarval_cmp(stepped, end_tar) & norm_proj))
+ return 1;
+
+ DB((dbg, LEVEL_4, "Result: (stepped)%ld IS %s (end)%ld\n",
+ get_tarval_long(stepped),
+ get_pnc_string(tarval_cmp(stepped, end_tar)),
+ get_tarval_long(end_tar)));
+
+ /* next step */
+ if (is_Add(loop_info.add))
+ next = tarval_add(stepped, step_tar);
+ else
+ /* sub */
+ next = tarval_sub(stepped, step_tar, get_irn_mode(loop_info.end_val));
+
+ DB((dbg, LEVEL_4, "Loop taken if %ld %s %ld ",
+ get_tarval_long(next),
+ get_pnc_string(norm_proj),
+ get_tarval_long(end_tar)));
+ DB((dbg, LEVEL_4, "comparing latest value %d\n", loop_info.latest_value));
+
+ /* Increase steps. */
+ *count_tar = tarval_add(*count_tar, get_tarval_one(get_tarval_mode(*count_tar)));
+
+ /* Next has to fail the loop condition, or we will never exit. */
+ if (! (tarval_cmp(next, end_tar) & norm_proj))
+ return 1;
+ else
+ return 0;
+}
+
+/* Check if loop meets requirements for a 'simple loop':
+ * - Exactly one cf out
+ * - Allowed calls
+ * - Max nodes after unrolling
+ * - tail-controlled
+ * - exactly one be
+ * - cmp
+ * Returns Projection of cmp node or NULL; */
+static ir_node *is_simple_loop(void)
+{
+ int arity, i;
+ ir_node *loop_block, *exit_block, *projx, *cond, *projres, *loop_condition;
+
+ /* Maximum of one condition, and no endless loops. */
+ if (loop_info.cf_outs != 1)
+ return NULL;
+
+ DB((dbg, LEVEL_4, "1 loop exit\n"));
+
+#if LOOP_IGNORE_NODE_LIMITS
+ /* Ignore loop size. Probably not wise in other than testcases. */
+ loop_info.max_unroll = 40;
+#else
+ /* Calculate maximum unroll_nr keeping node count below limit. */
+ loop_info.max_unroll = (int)((double)opt_params.max_unrolled_loop_size / (double)loop_info.nodes);
+ if (loop_info.max_unroll < 2) {
+ count_stats(stats.too_large);
+ return NULL;
+ }
+#endif
+
+
+ DB((dbg, LEVEL_4, "maximum unroll factor %u, to not exceed node limit \n",
+ opt_params.max_unrolled_loop_size));
+
+ arity = get_irn_arity(loop_head);
+ /* RETURN if we have more than 1 be. */
+ /* Get my backedges without alien bes. */
+ loop_block = NULL;
+ for (i = 0; i < arity; ++i) {
+ ir_node *pred = get_irn_n(loop_head, i);
+ if (is_own_backedge(loop_head, i)) {
+ if (loop_block)
+ /* Our simple loops may have only one backedge. */
+ return NULL;
+ else {
+ loop_block = get_nodes_block(pred);
+ loop_info.be_src_pos = i;
+ }
+ }
+ }
+
+ DB((dbg, LEVEL_4, "loop has 1 own backedge.\n"));
+
+ exit_block = get_nodes_block(loop_info.cf_out.pred);
+ /* The loop has to be tail-controlled.
+ * This can be changed/improved,
+ * but we would need a duff iv. */
+ if (exit_block != loop_block)
+ return NULL;
+
+ DB((dbg, LEVEL_4, "tail-controlled loop.\n"));
+
+ /* find value on which loop exit depends */
+ projx = loop_info.cf_out.pred;
+ cond = get_irn_n(projx, 0);
+ projres = get_irn_n(cond, 0);
+ loop_condition = get_irn_n(projres, 0);
+
+ if (!is_Cmp(loop_condition))
+ return NULL;
+
+ DB((dbg, LEVEL_5, "projection is %s\n", get_pnc_string(get_Proj_proj(projx))));
+
+ switch(get_Proj_proj(projx)) {
+ case pn_Cond_false:
+ loop_info.exit_cond = 0;
+ break;
+ case pn_Cond_true:
+ loop_info.exit_cond = 1;
+ break;
+ default:
+ panic("Cond Proj_proj other than true/false");
+ }
+
+ DB((dbg, LEVEL_4, "Valid Cmp.\n"));
+
+ return projres;
+}
+
+/* Returns 1 if all nodes are mode_Iu or mode_Is. */
+static unsigned are_mode_I(ir_node *n1, ir_node* n2, ir_node *n3)
+{
+ ir_mode *m1 = get_irn_mode(n1);
+ ir_mode *m2 = get_irn_mode(n2);
+ ir_mode *m3 = get_irn_mode(n3);
+
+ if ((m1 == mode_Iu && m2 == mode_Iu && m3 == mode_Iu) ||
+ (m1 == mode_Is && m2 == mode_Is && m3 == mode_Is))
+ return 1;
+ else
+ return 0;
+}
+
+/* Checks if cur_loop is a simple tail-controlled counting loop
+ * with start and end value loop invariant, step constant. */
+static unsigned get_unroll_decision_invariant(void)
+{
+
+ ir_node *projres, *loop_condition, *iteration_path;
+ unsigned success, is_latest_val;
+ ir_tarval *step_tar;
+ ir_mode *mode;
+
+
+ /* RETURN if loop is not 'simple' */
+ projres = is_simple_loop();
+ if (projres == NULL)
+ return 0;
+
+ /* Use a minimal size for the invariant unrolled loop,
+ * as duffs device produces overhead */
+ if (loop_info.nodes < opt_params.invar_unrolling_min_size)
+ return 0;
+
+ loop_condition = get_irn_n(projres, 0);
+
+ success = get_invariant_pred(loop_condition, &loop_info.end_val, &iteration_path);
+ DB((dbg, LEVEL_4, "pred invar %d\n", success));
+
+ if (! success)
+ return 0;
+
+ DB((dbg, LEVEL_4, "Invariant End_val %N, other %N\n", loop_info.end_val, iteration_path));
+
+ /* We may find the add or the phi first.
+ * Until now we only have end_val. */
+ if (is_Add(iteration_path) || is_Sub(iteration_path)) {
+
+ /* We test against the latest value of the iv. */
+ is_latest_val = 1;
+
+ loop_info.add = iteration_path;
+ DB((dbg, LEVEL_4, "Case 1: Got add %N (maybe not sane)\n", loop_info.add));
+
+ /* Preds of the add should be step and the iteration_phi */
+ success = get_const_pred(loop_info.add, &loop_info.step, &loop_info.iteration_phi);
+ if (! success)
+ return 0;
+
+ DB((dbg, LEVEL_4, "Got step %N\n", loop_info.step));
+
+ if (! is_Phi(loop_info.iteration_phi))
+ return 0;
+
+ DB((dbg, LEVEL_4, "Got phi %N\n", loop_info.iteration_phi));
+
+ /* Find start_val.
+ * Does necessary sanity check of add, if it is already set. */
+ success = get_start_and_add(loop_info.iteration_phi, invariant);
+ if (! success)
+ return 0;
+
+ DB((dbg, LEVEL_4, "Got start A %N\n", loop_info.start_val));
+
+ } else if (is_Phi(iteration_path)) {
+ ir_node *new_iteration_phi;
+
+ /* We compare with the value the iv had entering this run. */
+ is_latest_val = 0;
+
+ loop_info.iteration_phi = iteration_path;
+ DB((dbg, LEVEL_4, "Case 2: Got phi %N\n", loop_info.iteration_phi));
+
+ /* Find start_val and add-node.
+ * Does necessary sanity check of add, if it is already set. */
+ success = get_start_and_add(loop_info.iteration_phi, invariant);
+ if (! success)
+ return 0;
+
+ DB((dbg, LEVEL_4, "Got start B %N\n", loop_info.start_val));
+ DB((dbg, LEVEL_4, "Got add or sub %N\n", loop_info.add));
+
+ success = get_const_pred(loop_info.add, &loop_info.step, &new_iteration_phi);
+ if (! success)
+ return 0;
+
+ DB((dbg, LEVEL_4, "Got step (B) %N\n", loop_info.step));
+
+ if (loop_info.iteration_phi != new_iteration_phi)
+ return 0;
+
+ } else {
+ return 0;
+ }
+
+ mode = get_irn_mode(loop_info.end_val);
+
+ DB((dbg, LEVEL_4, "start %N, end %N, step %N\n",
+ loop_info.start_val, loop_info.end_val, loop_info.step));
+
+ if (mode != mode_Is && mode != mode_Iu)
+ return 0;
+
+ /* TODO necessary? */
+ if (!are_mode_I(loop_info.start_val, loop_info.step, loop_info.end_val))
+ return 0;
+
+ DB((dbg, LEVEL_4, "mode integer\n"));
+
+ step_tar = get_Const_tarval(loop_info.step);
+
+ if (tarval_is_null(step_tar)) {
+ /* TODO Might be worth a warning. */
+ return 0;
+ }
+
+ DB((dbg, LEVEL_4, "step is not 0\n"));
+
+ create_duffs_block();
+
+ return loop_info.max_unroll;
+}
+
+/* Returns unroll factor,
+ * given maximum unroll factor and number of loop passes. */
+static unsigned get_preferred_factor_constant(ir_tarval *count_tar)
+{
+ ir_tarval *tar_6, *tar_5, *tar_4, *tar_3, *tar_2;
+ unsigned prefer;
+ ir_mode *mode = get_irn_mode(loop_info.end_val);
+
+ tar_6 = new_tarval_from_long(6, mode);
+ tar_5 = new_tarval_from_long(5, mode);
+ tar_4 = new_tarval_from_long(4, mode);
+ tar_3 = new_tarval_from_long(3, mode);
+ tar_2 = new_tarval_from_long(2, mode);
+
+ /* loop passes % {6, 5, 4, 3, 2} == 0 */
+ if (tarval_is_null(tarval_mod(count_tar, tar_6)))
+ prefer = 6;
+ else if (tarval_is_null(tarval_mod(count_tar, tar_5)))
+ prefer = 5;
+ else if (tarval_is_null(tarval_mod(count_tar, tar_4)))
+ prefer = 4;
+ else if (tarval_is_null(tarval_mod(count_tar, tar_3)))
+ prefer = 3;
+ else if (tarval_is_null(tarval_mod(count_tar, tar_2)))
+ prefer = 2;
+ else {
+ /* gcd(max_unroll, count_tar) */
+ int a = loop_info.max_unroll;
+ int b = (int)get_tarval_long(count_tar);
+ int c;
+
+ DB((dbg, LEVEL_4, "gcd of max_unroll %d and count_tar %d: ", a, b));
+
+ do {
+ c = a % b;
+ a = b; b = c;
+ } while( c != 0);
+
+ DB((dbg, LEVEL_4, "%d\n", a));
+ return a;
+ }
+
+ DB((dbg, LEVEL_4, "preferred unroll factor %d\n", prefer));