+ * Computes the start-workset for a block with multiple predecessors. We assume
+ * that at least 1 of the predeccesors is a back-edge which means we're at the
+ * beginning of a loop. We try to reload as much values as possible now so they
+ * don't get reloaded inside the loop.
+ */
+static void compute_live_ins(const ir_node *block)
+{
+ ir_loop *loop = get_irn_loop(block);
+ ir_node *first;
+ ir_node *node;
+ loc_t loc;
+ loc_t *starters;
+ loc_t *delayed;
+ int i, len, ws_count;
+ int free_slots, free_pressure_slots;
+ unsigned pressure;
+ //int arity;
+ //int n_pred_worksets;
+ //workset_t **pred_worksets;
+
+ /* Collect all values living at start of block */
+ starters = NEW_ARR_F(loc_t, 0);
+ delayed = NEW_ARR_F(loc_t, 0);
+
+ DB((dbg, DBG_START, "Living at start of %+F:\n", block));
+ first = sched_first(block);
+
+ /* check all Phis first */
+ sched_foreach(block, node) {
+ if (! is_Phi(node))
+ break;
+
+ loc = to_take_or_not_to_take(first, node, loop);
+
+ if (! USES_IS_INFINITE(loc.time)) {
+ if (USES_IS_PENDING(loc.time))
+ ARR_APP1(loc_t, delayed, loc);
+ else
+ ARR_APP1(loc_t, starters, loc);
+ }
+ }
+
+ /* check all Live-Ins */
+ be_lv_foreach(lv, block, be_lv_state_in, i) {
+ ir_node *node = be_lv_get_irn(lv, block, i);
+
+ loc = to_take_or_not_to_take(first, node, loop);
+
+ if (! USES_IS_INFINITE(loc.time)) {
+ if (USES_IS_PENDING(loc.time))
+ ARR_APP1(loc_t, delayed, loc);
+ else
+ ARR_APP1(loc_t, starters, loc);
+ }
+ }
+
+ pressure = be_get_loop_pressure(loop_ana, cls, loop);
+ assert(ARR_LEN(delayed) <= (signed)pressure);
+ free_slots = n_regs - ARR_LEN(starters);
+ free_pressure_slots = n_regs - (pressure - ARR_LEN(delayed));
+ free_slots = MIN(free_slots, free_pressure_slots);
+
+ /* so far we only put nodes into the starters list that are used inside
+ * the loop. If register pressure in the loop is low then we can take some
+ * values and let them live through the loop */
+ if(free_slots > 0) {
+ qsort(delayed, ARR_LEN(delayed), sizeof(delayed[0]), loc_compare);
+
+ for (i = 0; i < ARR_LEN(delayed) && i < free_slots; ++i) {
+ DB((dbg, DBG_START, " delayed %+F taken\n", delayed[i].node));
+ ARR_APP1(loc_t, starters, delayed[i]);
+ delayed[i].node = NULL;
+ }
+ }
+
+ /* spill phis (the actual phis not just their values) that are in this block
+ * but not in the start workset */
+ for (i = ARR_LEN(delayed) - 1; i >= 0; --i) {
+ ir_node *node = delayed[i].node;
+ if(node == NULL || !is_Phi(node) || get_nodes_block(node) != block)
+ continue;
+
+ DB((dbg, DBG_START, " spilling delayed phi %+F\n", node));
+ be_spill_phi(senv, node);
+ }
+ DEL_ARR_F(delayed);
+
+ /* Sort start values by first use */
+ qsort(starters, ARR_LEN(starters), sizeof(starters[0]), loc_compare);
+
+ /* Copy the best ones from starters to start workset */
+ ws_count = MIN(ARR_LEN(starters), n_regs);
+ workset_clear(ws);
+ workset_bulk_fill(ws, ws_count, starters);
+
+ /* spill phis (the actual phis not just their values) that are in this block
+ * but not in the start workset */
+ len = ARR_LEN(starters);
+ for (i = ws_count; i < len; ++i) {
+ ir_node *node = starters[i].node;
+ if (! is_Phi(node) || get_nodes_block(node) != block)
+ continue;
+
+ DB((dbg, DBG_START, " spilling phi %+F\n", node));
+ be_spill_phi(senv, node);
+ }
+
+ DEL_ARR_F(starters);
+
+#if 0
+ /* determine reloaded status of the values: If there's 1 pred block (which
+ * is no backedge) where the value is reloaded then we must set it to
+ * reloaded here. We place spills in all pred where the value was not yet
+ * reloaded to be sure we have a spill on each path */
+ n_pred_worksets = 0;
+ arity = get_irn_arity(block);
+ pred_worksets = alloca(sizeof(pred_worksets[0]) * arity);
+ for(i = 0; i < arity; ++i) {
+ ir_node *pred_block = get_Block_cfgpred_block(block, i);
+ block_info_t *pred_info = get_block_info(pred_block);
+ if(pred_info == NULL)
+ continue;
+
+ pred_worksets[n_pred_worksets] = pred_info->end_workset;
+ ++n_pred_worksets;
+ }
+
+ for(i = 0; i < ws_count; ++i) {
+ loc_t *loc = &ws->vals[i];
+ ir_node *value = loc->node;
+ int reloaded;
+ int n;
+
+ /* phis from this block aren't reloaded */
+ if(get_nodes_block(value) == block) {
+ assert(is_Phi(value));
+ loc->reloaded = value_not_reloaded;
+ continue;
+ }
+
+ /* was the value reloaded on any of the other inputs */
+ reloaded = 0;
+ arity = get_Block_n_cfgpreds(block);
+ for(n = 0; n < n_pred_worksets; ++n) {
+ workset_t *pred_workset = pred_worksets[n];
+ int p_len = workset_get_length(pred_workset);
+ int p;
+
+ for(p = 0; p < p_len; ++p) {
+ loc_t *l = &pred_workset->vals[p];
+ if(l->node == value) {
+ if(l->reloaded) {
+ reloaded = 1;
+ }
+ break;
+ }
+ }
+ if(p >= p_len) {
+ reloaded = 1;
+ break;
+ }
+ }
+ }
+#endif
+}
+
+/**
+ * For the given block @p block, decide for each values