+ unsigned step = from_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;
+
+#if 1
+ 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;
+
+#else
+ if(skip_from_uses) {
+ from = sched_next(from);
+ ++step;
+ }
+
+ sched_foreach_from(from, node) {
+ int i, arity;
+
+ if(is_Phi(node)) {
+ step++;
+ continue;
+ }
+
+ arity = get_irn_arity(node);
+ for (i = 0; i < arity; ++i) {
+ const ir_node *operand = get_irn_n(node, i);
+
+ if (operand == def) {
+ be_next_use_t result;
+
+ DBG((env->dbg, LEVEL_3, "found use of %+F at %+F\n", operand, node));
+
+ /**
+ * Spills/Reloads are a special case, they're not really a
+ * usage of a value, continue searching
+ */
+ if (be_is_Spill(node) || be_is_Reload(node)) {
+ return be_get_next_use(env, node, step, node, 1);
+ }
+
+ result.time = step;
+ result.outermost_loop = get_loop_depth(get_irn_loop(block));
+ result.before = node;
+ return result;
+ }
+ }
+
+ step++;
+ }
+#endif
+
+ 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;
+ }
+
+#ifdef SCAN_INTERBLOCK_USES
+ {
+ unsigned next_use = USES_INFINITY;
+ int outermost_loop;
+ be_next_use_t result;
+ ir_loop *loop = get_irn_loop(block);
+ int 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;
+ foreach_block_succ(block, edge) {
+ const be_use_t *use;
+ const ir_node *succ_block = get_edge_src_irn(edge);
+ ir_loop *succ_loop;
+ unsigned use_dist;
+
+ if(succ_block == startblock)
+ continue;
+
+ DBG((env->dbg, LEVEL_5, "Checking succ of block %+F: %+F (for use of %+F)\n", block, succ_block, def));
+ if(!be_is_live_in(env->lv, succ_block, def)) {
+ //next_use = USES_INFINITY;
+ DBG((env->dbg, LEVEL_5, " not live in\n"));
+ continue;
+ }
+
+ use = get_or_set_use_block(env, succ_block, def);
+ DBG((env->dbg, LEVEL_5, "Found %u (loopdepth %d) (we're in block %+F)\n", use->next_use,
+ use->outermost_loop, block));
+ if(USES_IS_INFINITE(use->next_use)) {
+ if(use->outermost_loop < 0) {
+ found_visited = 1;
+ }
+ continue;
+ }
+
+ found_use = 1;
+ use_dist = use->next_use;
+
+ succ_loop = get_irn_loop(succ_block);
+ if(get_loop_depth(succ_loop) < loopdepth) {
+ unsigned factor = (loopdepth - get_loop_depth(succ_loop)) * 5000;
+ DBG((env->dbg, LEVEL_5, "Increase usestep because of loop out edge %d -> %d (%u)\n", factor));
+ // TODO we should use the number of nodes in the loop or so...
+ use_dist += factor;
+ }
+
+ if(use_dist < next_use) {
+ next_use = use_dist;
+ outermost_loop = use->outermost_loop;
+ result.before = use->node;
+ }
+ }
+
+ if(loopdepth < outermost_loop)
+ outermost_loop = loopdepth;
+
+ result.time = next_use + step;
+ result.outermost_loop = outermost_loop;
+
+ if(!found_use && found_visited) {
+ // the current result is correct for the current search, but isn't
+ // generally correct, so mark it
+ result.outermost_loop = -1;
+ }
+ DBG((env->dbg, LEVEL_5, "Result: %d (outerloop: %d)\n", result.time, result.outermost_loop));
+ return result;
+ }
+#else
+ return USES_INFINITY;
+#endif