9d2c7dadc683dc00f17cc4be381aed4391838f53
[libfirm] / ir / be / beuses.c
1 /**
2  * @file   beuse.c
3  * @date   27.06.2005
4  * @author Sebastian Hack
5  *
6  * Methods to compute when a value will be used again.
7  *
8  * Copyright (C) 2005 Universitaet Karlsruhe
9  * Released under the GPL
10  */
11
12 #include <limits.h>
13 #include <stdlib.h>
14
15 #include "config.h"
16 #include "obst.h"
17 #include "pmap.h"
18 #include "debug.h"
19
20 #include "irgwalk.h"
21 #include "irnode_t.h"
22 #include "ircons_t.h"
23 #include "irgraph_t.h"
24 #include "iredges_t.h"
25 #include "irdom_t.h"
26
27 #include "be_t.h"
28 #include "beutil.h"
29 #include "belive_t.h"
30 #include "benode_t.h"
31 #include "besched_t.h"
32 #include "beirgmod.h"
33 #include "bearch.h"
34 #include "beuses_t.h"
35
36 typedef struct _be_use_t {
37   const ir_node *bl;
38   const ir_node *irn;
39   unsigned next_use;
40 } be_use_t;
41
42 struct _be_uses_t {
43   set *uses;
44   ir_graph *irg;
45   firm_dbg_module_t *dbg;
46   const arch_env_t *arch_env;
47 };
48
49
50 #define MIN(a, b)                ((a) < (b) ? (a) : (b))
51
52 static INLINE unsigned sadd(unsigned a, unsigned b)
53 {
54   return a + b;
55 }
56
57 static INLINE unsigned sdiv(unsigned a, unsigned b)
58 {
59   return a / b;
60 }
61
62 static int cmp_use(const void *a, const void *b, size_t n)
63 {
64   const be_use_t *p = a;
65   const be_use_t *q = b;
66   return !(p->bl == q->bl && p->irn == q->irn);
67 }
68
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)
71 {
72   unsigned hash = HASH_COMBINE(HASH_PTR(bl), HASH_PTR(irn));
73   be_use_t templ;
74
75   templ.bl = bl;
76   templ.irn = irn;
77   templ.next_use = next_use;
78   return set_insert(uses->uses, &templ, sizeof(templ), hash);
79 }
80
81 unsigned be_get_next_use(be_uses_t *uses, const ir_node *from,
82     unsigned from_step, const ir_node *def);
83
84 static unsigned get_next_use_bl(be_uses_t *uses, const ir_node *bl,
85     const ir_node *def)
86 {
87   be_use_t *u;
88
89   u = get_or_set_use(uses, bl, def, 0);
90   if(USES_IS_INIFINITE(u->next_use))
91     return u->next_use;
92
93   u->next_use = USES_INFINITY;
94   u->next_use = be_get_next_use(uses, sched_first(bl), 0, def);
95   return u->next_use;
96 }
97
98 unsigned be_get_next_use(be_uses_t *uses,
99     const ir_node *from, unsigned from_step, const ir_node *def)
100 {
101   unsigned next_use = USES_INFINITY;
102   unsigned step = from_step;
103   unsigned n = 0;
104   const ir_node *irn;
105   const ir_node *bl = get_block(from);
106   const ir_edge_t *succ_edge;
107
108   sched_foreach_from(from, irn) {
109     int i, n;
110
111     for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
112       ir_node *operand = get_irn_n(irn, i);
113
114       if(operand == def) {
115         DBG((uses->dbg, LEVEL_3, "found use of %+F at %+F\n", operand, irn));
116         return step;
117       }
118     }
119
120     step++;
121   }
122
123   next_use = step;
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);
128
129       DBG((uses->dbg, LEVEL_2, "\t\tnext use in succ %+F: %d\n", succ_bl, next));
130       next_use = sadd(next_use, next);
131       n++;
132     }
133   }
134
135   if(n > 1)
136     next_use = sdiv(next_use, n);
137
138   return sadd(next_use, step);
139 }
140
141 be_uses_t *be_begin_uses(
142     ir_graph *irg,
143     const arch_env_t *arch_env,
144     const arch_register_class_t *cls)
145 {
146   be_uses_t *uses = malloc(sizeof(uses[0]));
147
148   edges_assure(irg);
149
150   uses->arch_env = arch_env;
151   uses->uses     = new_set(cmp_use, 512);
152   uses->dbg      = firm_dbg_register("be.uses");
153
154   return uses;
155 }
156
157 void be_end_uses(be_uses_t *uses)
158 {
159   del_set(uses->uses);
160   free(uses);
161 }
162
163 int loc_compare(const void *a, const void *b)
164 {
165   const loc_t *p = a;
166   const loc_t *q = b;
167   return p->time - q->time;
168 }