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