4 * @author Sebastian Hack
6 * Methods to compute when a value will be used again.
8 * Copyright (C) 2005 Universitaet Karlsruhe
9 * Released under the GPL
23 #include "irgraph_t.h"
24 #include "iredges_t.h"
31 #include "besched_t.h"
36 #define DBG_LEVEL SET_LEVEL_0
38 typedef struct _be_use_t {
48 const arch_env_t *arch_env;
49 DEBUG_ONLY(firm_dbg_module_t *dbg;)
52 static int cmp_use(const void *a, const void *b, size_t n)
54 const be_use_t *p = a;
55 const be_use_t *q = b;
56 return !(p->bl == q->bl && p->irn == q->irn);
59 static INLINE be_use_t *get_or_set_use(be_uses_t *uses,
60 const ir_node *bl, const ir_node *def, unsigned next_use)
62 unsigned hash = HASH_COMBINE(HASH_PTR(bl), HASH_PTR(def));
68 templ.next_use = be_get_next_use(uses, sched_first(bl), 0, def, 0);
69 result = set_insert(uses->uses, &templ, sizeof(templ), hash);
74 unsigned be_get_next_use(be_uses_t *uses, const ir_node *from,
75 unsigned from_step, const ir_node *def, int skip_from_uses);
77 static unsigned get_next_use_bl(be_uses_t *uses, const ir_node *bl,
80 be_use_t *u = get_or_set_use(uses, bl, def, 0);
85 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)
87 unsigned next_use = USES_INFINITY;
88 unsigned step = from_step;
90 ir_node *bl = get_nodes_block(from);
92 const ir_edge_t *succ_edge;
94 set_irn_visited(bl, visited_nr);
96 sched_foreach_from(from, irn) {
99 if (! skip_from_uses) {
100 for (i = 0, n = get_irn_arity(irn); i < n; ++i) {
101 ir_node *operand = get_irn_n(irn, i);
103 if (operand == def) {
104 DBG((uses->dbg, LEVEL_3, "found use of %+F at %+F\n", operand, irn));
114 /* FIXME: quick and dirty hack to prevent ignore nodes (like stack pointer) from being spilled */
115 return be_is_live_end(uses->lv, bl, def) ? step : USES_INFINITY;
117 next_use = USES_INFINITY;
118 foreach_block_succ(bl, succ_edge) {
119 const ir_node *succ_bl = succ_edge->src;
120 if(get_irn_visited(succ_bl) < visited_nr && (be_is_live_in(uses->lv, succ_bl, def) || (get_irn_arity(succ_bl) > 1 && be_is_live_end(uses->lv, bl, def)))) {
121 unsigned next = get_next_use_bl(uses, succ_bl, def);
123 DBG((uses->dbg, LEVEL_2, "\t\tnext use in succ %+F: %d\n", succ_bl, next));
124 next_use = MIN(next_use, next);
129 return next_use + step;
132 unsigned be_get_next_use(be_uses_t *uses, const ir_node *from, unsigned from_step, const ir_node *def, int skip_from_uses)
134 unsigned long visited_nr = get_irg_visited(uses->irg) + 1;
136 set_irg_visited(uses->irg, visited_nr);
137 return get_next_use(uses, from, from_step, def, skip_from_uses, visited_nr);
141 be_uses_t *be_begin_uses(ir_graph *irg, const be_lv_t *lv, const arch_env_t *arch_env, const arch_register_class_t *cls)
143 be_uses_t *uses = xmalloc(sizeof(uses[0]));
147 uses->arch_env = arch_env;
148 uses->uses = new_set(cmp_use, 512);
151 FIRM_DBG_REGISTER(uses->dbg, "firm.be.uses");
156 void be_end_uses(be_uses_t *uses)
162 int loc_compare(const void *a, const void *b)
166 return p->time - q->time;