-static unsigned get_next_use(be_uses_t *uses, const ir_node *from, unsigned from_step, const ir_node *def, int skip_from_uses, unsigned long visited_nr)
-{
- unsigned next_use = USES_INFINITY;
- unsigned step = from_step;
- unsigned n = 0;
- ir_node *bl = get_nodes_block(from);
- const ir_node *irn;
- const ir_edge_t *succ_edge;
-
- set_irn_visited(bl, visited_nr);
-
- sched_foreach_from(from, irn) {
- int i, n;
-
- if(!skip_from_uses) {
- for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
- ir_node *operand = get_irn_n(irn, i);
-
- if(operand == def) {
- DBG((uses->dbg, LEVEL_3, "found use of %+F at %+F\n", operand, irn));
- return step;
- }
+ 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;
+ 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;