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 firm_dbg_module_t *dbg;
49 const arch_env_t *arch_env;
53 #define MIN(a, b) ((a) < (b) ? (a) : (b))
55 static INLINE unsigned sadd(unsigned a, unsigned b)
60 static INLINE unsigned sdiv(unsigned a, unsigned b)
65 static int cmp_use(const void *a, const void *b, size_t n)
67 const be_use_t *p = a;
68 const be_use_t *q = b;
69 return !(p->bl == q->bl && p->irn == q->irn);
72 static INLINE be_use_t *get_or_set_use(be_uses_t *uses,
73 const ir_node *bl, const ir_node *irn, unsigned next_use)
75 unsigned hash = HASH_COMBINE(HASH_PTR(bl), HASH_PTR(irn));
80 templ.next_use = next_use;
82 return set_insert(uses->uses, &templ, sizeof(templ), hash);
85 unsigned be_get_next_use(be_uses_t *uses, const ir_node *from,
86 unsigned from_step, const ir_node *def, int skip_from_uses);
88 static unsigned get_next_use_bl(be_uses_t *uses, const ir_node *bl,
93 u = get_or_set_use(uses, bl, def, 0);
96 u->next_use = USES_INFINITY;
97 u->next_use = be_get_next_use(uses, sched_first(bl), 0, def, 0);
102 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)
104 unsigned next_use = USES_INFINITY;
105 unsigned step = from_step;
107 ir_node *bl = get_nodes_block(from);
109 const ir_edge_t *succ_edge;
111 set_irn_visited(bl, visited_nr);
113 sched_foreach_from(from, irn) {
116 if(!skip_from_uses) {
117 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
118 ir_node *operand = get_irn_n(irn, i);
121 DBG((uses->dbg, LEVEL_3, "found use of %+F at %+F\n", operand, irn));
131 next_use = USES_INFINITY;
132 foreach_block_succ(bl, succ_edge) {
133 const ir_node *succ_bl = succ_edge->src;
134 if(get_irn_visited(succ_bl) < visited_nr && (is_live_in(succ_bl, def) || (get_irn_arity(succ_bl) > 1 && is_live_end(bl, def)))) {
135 unsigned next = get_next_use_bl(uses, succ_bl, def);
137 DBG((uses->dbg, LEVEL_2, "\t\tnext use in succ %+F: %d\n", succ_bl, next));
138 next_use = MIN(next_use, next);
143 return next_use + step;
146 unsigned be_get_next_use(be_uses_t *uses, const ir_node *from, unsigned from_step, const ir_node *def, int skip_from_uses)
148 unsigned long visited_nr = get_irg_visited(uses->irg) + 1;
150 set_irg_visited(uses->irg, visited_nr);
151 return get_next_use(uses, from, from_step, def, skip_from_uses, visited_nr);
155 be_uses_t *be_begin_uses(ir_graph *irg, const arch_env_t *arch_env, const arch_register_class_t *cls)
157 be_uses_t *uses = xmalloc(sizeof(uses[0]));
161 uses->arch_env = arch_env;
162 uses->uses = new_set(cmp_use, 512);
163 uses->dbg = firm_dbg_register("be.uses");
165 firm_dbg_set_mask(uses->dbg, DBG_LEVEL);
170 void be_end_uses(be_uses_t *uses)
176 int loc_compare(const void *a, const void *b)
180 return p->time - q->time;