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