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 typedef struct _be_use_t {
45 firm_dbg_module_t *dbg;
46 const arch_env_t *arch_env;
50 #define MIN(a, b) ((a) < (b) ? (a) : (b))
52 static INLINE unsigned sadd(unsigned a, unsigned b)
57 static INLINE unsigned sdiv(unsigned a, unsigned b)
62 static int cmp_use(const void *a, const void *b, size_t n)
64 const be_use_t *p = a;
65 const be_use_t *q = b;
66 return !(p->bl == q->bl && p->irn == q->irn);
69 static INLINE be_use_t *get_or_set_use(be_uses_t *uses,
70 const ir_node *bl, const ir_node *irn, unsigned next_use)
72 unsigned hash = HASH_COMBINE(HASH_PTR(bl), HASH_PTR(irn));
77 templ.next_use = next_use;
78 return set_insert(uses->uses, &templ, sizeof(templ), hash);
81 unsigned be_get_next_use(be_uses_t *uses, const ir_node *from,
82 unsigned from_step, const ir_node *def);
84 static unsigned get_next_use_bl(be_uses_t *uses, const ir_node *bl,
89 u = get_or_set_use(uses, bl, def, 0);
90 if(USES_IS_INIFINITE(u->next_use))
93 u->next_use = USES_INFINITY;
94 u->next_use = be_get_next_use(uses, sched_first(bl), 0, def);
98 unsigned be_get_next_use(be_uses_t *uses,
99 const ir_node *from, unsigned from_step, const ir_node *def)
101 unsigned next_use = USES_INFINITY;
102 unsigned step = from_step;
105 const ir_node *bl = get_block(from);
106 const ir_edge_t *succ_edge;
108 sched_foreach_from(from, irn) {
111 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
112 ir_node *operand = get_irn_n(irn, i);
115 DBG((uses->dbg, LEVEL_3, "found use of %+F at %+F\n", operand, irn));
124 foreach_block_succ(bl, succ_edge) {
125 const ir_node *succ_bl = succ_edge->src;
126 if(is_live_in(succ_bl, def)) {
127 unsigned next = get_next_use_bl(uses, succ_bl, def);
129 DBG((uses->dbg, LEVEL_2, "\t\tnext use in succ %+F: %d\n", succ_bl, next));
130 next_use = sadd(next_use, next);
136 next_use = sdiv(next_use, n);
138 return sadd(next_use, step);
141 be_uses_t *be_begin_uses(
143 const arch_env_t *arch_env,
144 const arch_register_class_t *cls)
146 be_uses_t *uses = malloc(sizeof(uses[0]));
150 uses->arch_env = arch_env;
151 uses->uses = new_set(cmp_use, 512);
152 uses->dbg = firm_dbg_register("be.uses");
157 void be_end_uses(be_uses_t *uses)
163 int loc_compare(const void *a, const void *b)
167 return p->time - q->time;