+ workset_set_length(ws, len - spills_needed);
+ }
+
+ /* 3. Insert the new values into the workset */
+ for (i = 0; i < demand; ++i) {
+ ir_node *val = to_insert[i];
+
+ workset_insert(ws, val, 1);
+ }
+}
+
+/** Decides whether a specific node should be in the start workset or not
+ *
+ * @param env belady environment
+ * @param first
+ * @param node the node to test
+ * @param loop the loop of the node
+ */
+static loc_t to_take_or_not_to_take(ir_node* first, ir_node *node,
+ ir_loop *loop)
+{
+ be_next_use_t next_use;
+ loc_t loc;
+
+ loc.time = USES_INFINITY;
+ loc.node = node;
+ //loc.reloaded = rand() % 2; /* provoke a bug... */
+ loc.reloaded = 0;
+
+ if (!arch_irn_consider_in_reg_alloc(arch_env, cls, node)) {
+ loc.time = USES_INFINITY;
+ return loc;
+ }
+
+ /* We have to keep nonspillable nodes in the workingset */
+ if(arch_irn_get_flags(arch_env, node) & arch_irn_flags_dont_spill) {
+ loc.time = 0;
+ DB((dbg, DBG_START, " %+F taken (dontspill node)\n", node, loc.time));
+ return loc;
+ }
+
+ next_use = be_get_next_use(uses, first, 0, node, 0);
+ if(USES_IS_INFINITE(next_use.time)) {
+ // the nodes marked as live in shouldn't be dead, so it must be a phi
+ assert(is_Phi(node));
+ loc.time = USES_INFINITY;
+ DB((dbg, DBG_START, " %+F not taken (dead)\n", node));
+ if(is_Phi(node)) {
+ be_spill_phi(senv, node);
+ }
+ return loc;
+ }
+
+ loc.time = next_use.time;
+
+ if(next_use.outermost_loop >= get_loop_depth(loop)) {
+ DB((dbg, DBG_START, " %+F taken (%u, loop %d)\n", node, loc.time, next_use.outermost_loop));
+ } else {
+ loc.time = USES_PENDING;
+ DB((dbg, DBG_START, " %+F delayed (outerloopdepth %d < loopdetph %d)\n", node, next_use.outermost_loop, get_loop_depth(loop)));
+ }
+ return loc;
+}
+
+/**
+ * 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;
+ }