+/**
+ * Set the scheduled index (the "step") of this node in its
+ * block.
+ *
+ * @param node the node
+ * @param step the scheduled index of the node
+ */
+static inline void set_step(ir_node *node, unsigned step)
+{
+ set_irn_link(node, INT_TO_PTR(step));
+}
+
+/**
+ * Find the next use of a value defined by def, starting at node from.
+ *
+ * @param env the uses environment
+ * @param from the node at which we should start the search
+ * @param def the definition of the value
+ * @param skip_from_uses if non-zero, ignore from uses
+ */
+static be_next_use_t get_next_use(be_uses_t *env, ir_node *from,
+ const ir_node *def, int skip_from_uses)
+{
+ unsigned step;
+ ir_node *block = get_nodes_block(from);
+ ir_node *next_use;
+ ir_node *node;
+ unsigned timestep;
+ unsigned next_use_step;
+ const ir_edge_t *edge;
+
+ assert(skip_from_uses == 0 || skip_from_uses == 1);
+ if (skip_from_uses) {
+ from = sched_next(from);
+ }
+
+ next_use = NULL;
+ next_use_step = INT_MAX;
+ timestep = get_step(from);
+ foreach_out_edge(def, edge) {
+ ir_node *node = get_edge_src_irn(edge);
+ unsigned node_step;
+
+ if (is_Anchor(node))
+ continue;
+ if (get_nodes_block(node) != block)
+ continue;
+ if (is_Phi(node))
+ continue;
+
+ node_step = get_step(node);
+ if (node_step < timestep)
+ continue;
+ if (node_step < next_use_step) {
+ next_use = node;
+ next_use_step = node_step;
+ }
+ }
+
+ if (next_use != NULL) {
+ be_next_use_t result;
+ result.time = next_use_step - timestep + skip_from_uses;
+ result.outermost_loop = get_loop_depth(get_irn_loop(block));
+ result.before = next_use;
+ return result;
+ }
+
+ node = sched_last(block);
+ step = get_step(node) + 1 + timestep + skip_from_uses;
+
+ if (be_is_phi_argument(block, def)) {
+ // TODO we really should continue searching the uses of the phi,
+ // as a phi isn't a real use that implies a reload (because we could
+ // easily spill the whole phi)
+
+ be_next_use_t result;
+ result.time = step;
+ result.outermost_loop = get_loop_depth(get_irn_loop(block));
+ result.before = block;
+ return result;
+ }
+
+ {
+ unsigned next_use = USES_INFINITY;
+ unsigned outermost_loop;
+ be_next_use_t result;
+ ir_loop *loop = get_irn_loop(block);
+ unsigned loopdepth = get_loop_depth(loop);
+ int found_visited = 0;
+ int found_use = 0;
+ ir_graph *irg = get_irn_irg(block);
+ ir_node *startblock = get_irg_start_block(irg);
+
+ result.before = NULL;
+ outermost_loop = loopdepth;