55cc651198d03d7b6729e9c5a9595669a0fd71cc
[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 #define DBG_LEVEL SET_LEVEL_0
37
38 typedef struct _be_use_t {
39         const ir_node *bl;
40         const ir_node *irn;
41         unsigned next_use;
42         int is_set;
43 } be_use_t;
44
45 struct _be_uses_t {
46   set *uses;
47   ir_graph *irg;
48   firm_dbg_module_t *dbg;
49   const arch_env_t *arch_env;
50 };
51
52
53 #define MIN(a, b)                ((a) < (b) ? (a) : (b))
54
55 static INLINE unsigned sadd(unsigned a, unsigned b)
56 {
57   return a + b;
58 }
59
60 static INLINE unsigned sdiv(unsigned a, unsigned b)
61 {
62   return a / b;
63 }
64
65 static int cmp_use(const void *a, const void *b, size_t n)
66 {
67   const be_use_t *p = a;
68   const be_use_t *q = b;
69   return !(p->bl == q->bl && p->irn == q->irn);
70 }
71
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)
74 {
75   unsigned hash = HASH_COMBINE(HASH_PTR(bl), HASH_PTR(irn));
76   be_use_t templ;
77
78   templ.bl = bl;
79   templ.irn = irn;
80   templ.next_use = next_use;
81   templ.is_set = 0;
82   return set_insert(uses->uses, &templ, sizeof(templ), hash);
83 }
84
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);
87
88 static unsigned get_next_use_bl(be_uses_t *uses, const ir_node *bl,
89     const ir_node *def)
90 {
91   be_use_t *u;
92
93   u = get_or_set_use(uses, bl, def, 0);
94   if(!u->is_set) {
95         u->is_set = 1;
96         u->next_use = USES_INFINITY;
97         u->next_use = be_get_next_use(uses, sched_first(bl), 0, def, 0);
98   }
99   return u->next_use;
100 }
101
102 unsigned be_get_next_use(be_uses_t *uses,
103     const ir_node *from, unsigned from_step, const ir_node *def,
104     int skip_from_uses)
105 {
106   unsigned next_use = USES_INFINITY;
107   unsigned step = from_step;
108   unsigned n = 0;
109   const ir_node *irn;
110   const ir_node *bl = get_block(from);
111   const ir_edge_t *succ_edge;
112
113   sched_foreach_from(from, irn) {
114     int i, n;
115
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);
119
120               if(operand == def) {
121                 DBG((uses->dbg, LEVEL_3, "found use of %+F at %+F\n", operand, irn));
122                 return step;
123               }
124             }
125         }
126
127         skip_from_uses = 0;
128     step++;
129   }
130
131         next_use = USES_INFINITY;
132         foreach_block_succ(bl, succ_edge) {
133                 const ir_node *succ_bl = succ_edge->src;
134                 if(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);
136
137                         DBG((uses->dbg, LEVEL_2, "\t\tnext use in succ %+F: %d\n", succ_bl, next));
138                         next_use = MIN(next_use, next);
139                         n++;
140             }
141         }
142
143         return next_use + step;
144 }
145
146 be_uses_t *be_begin_uses(
147     ir_graph *irg,
148     const arch_env_t *arch_env,
149     const arch_register_class_t *cls)
150 {
151   be_uses_t *uses = xmalloc(sizeof(uses[0]));
152
153   edges_assure(irg);
154
155   uses->arch_env = arch_env;
156   uses->uses     = new_set(cmp_use, 512);
157   uses->dbg      = firm_dbg_register("be.uses");
158   firm_dbg_set_mask(uses->dbg, DBG_LEVEL);
159
160   return uses;
161 }
162
163 void be_end_uses(be_uses_t *uses)
164 {
165   del_set(uses->uses);
166   free(uses);
167 }
168
169 int loc_compare(const void *a, const void *b)
170 {
171   const loc_t *p = a;
172   const loc_t *q = b;
173   return p->time - q->time;
174 }