tarvals are unique no need to use tarval_cmp for equality checks
[libfirm] / ir / be / beloopana.c
1 /*
2  * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Compute register pressure in loops.
23  * @author      Christian Wuerdig
24  * @date        19.02.2007
25  * @version     $Id$
26  */
27 #include "config.h"
28
29 #include "set.h"
30 #include "pset.h"
31 #include "irnode.h"
32 #include "irtools.h"
33 #include "irloop_t.h"
34 #include "error.h"
35 #include "debug.h"
36
37 #include "bearch.h"
38 #include "belive.h"
39 #include "besched.h"
40 #include "beloopana.h"
41 #include "bemodule.h"
42
43 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
44
45 #define HASH_LOOP_INFO(info) (HASH_PTR((info)->loop) ^ HASH_PTR((info)->cls))
46
47 typedef struct be_loop_info_t {
48         ir_loop                     *loop;
49         const arch_register_class_t *cls;
50         unsigned                    max_pressure;
51 } be_loop_info_t;
52
53 struct be_loopana_t {
54         set      *data;
55         ir_graph *irg;
56 };
57
58 static int cmp_loop_info(const void *a, const void *b, size_t size)
59 {
60         const be_loop_info_t *i1 = (const be_loop_info_t*)a;
61         const be_loop_info_t *i2 = (const be_loop_info_t*)b;
62         (void) size;
63
64         return ! (i1->loop == i2->loop && i1->cls == i2->cls);
65 }
66
67 /**
68  * Compute the highest register pressure in a block.
69  * @param irg       The graph.
70  * @param block     The block to compute pressure for.
71  * @param cls       The register class to compute pressure for.
72  * @return The highest register pressure in the given block.
73  */
74 static unsigned be_compute_block_pressure(const ir_graph *irg,
75                                           ir_node *block,
76                                           const arch_register_class_t *cls)
77 {
78         be_lv_t      *lv = be_get_irg_liveness(irg);
79         ir_nodeset_t  live_nodes;
80         ir_node      *irn;
81         size_t        max_live;
82
83         DBG((dbg, LEVEL_1, "Processing Block %+F\n", block));
84
85         /* determine largest pressure with this block */
86         ir_nodeset_init(&live_nodes);
87         be_liveness_end_of_block(lv, cls, block, &live_nodes);
88         max_live   = ir_nodeset_size(&live_nodes);
89
90         sched_foreach_reverse(block, irn) {
91                 size_t cnt;
92
93                 if (is_Phi(irn))
94                         break;
95
96                 be_liveness_transfer(cls, irn, &live_nodes);
97                 cnt      = ir_nodeset_size(&live_nodes);
98                 max_live = MAX(cnt, max_live);
99         }
100
101         DBG((dbg, LEVEL_1, "Finished with Block %+F (%s %zu)\n", block, cls->name, max_live));
102
103         ir_nodeset_destroy(&live_nodes);
104         return max_live;
105 }
106
107 /**
108  * Compute the highest register pressure in a loop and its sub-loops.
109  * @param loop_ana  The loop ana object.
110  * @param loop      The loop to compute pressure for.
111  * @param cls       The register class to compute pressure for.
112  * @return The highest register pressure in the given loop.
113  */
114 static unsigned be_compute_loop_pressure(be_loopana_t *loop_ana, ir_loop *loop,
115                                          const arch_register_class_t *cls)
116 {
117         size_t         i, max;
118         unsigned       pressure;
119         be_loop_info_t *entry, key;
120
121         DBG((dbg, LEVEL_1, "\nProcessing Loop %ld\n", loop->loop_nr));
122         assert(get_loop_n_elements(loop) > 0);
123         pressure = 0;
124
125         /* determine maximal pressure in all loop elements */
126         for (i = 0, max = get_loop_n_elements(loop); i < max; ++i) {
127                 unsigned     son_pressure;
128                 loop_element elem = get_loop_element(loop, i);
129
130                 if (*elem.kind == k_ir_node)
131                         son_pressure = be_compute_block_pressure(loop_ana->irg, elem.node, cls);
132                 else {
133                         assert(*elem.kind == k_ir_loop);
134                         son_pressure = be_compute_loop_pressure(loop_ana, elem.son, cls);
135                 }
136
137                 pressure = MAX(pressure, son_pressure);
138         }
139         DBG((dbg, LEVEL_1, "Done with loop %ld, pressure %u for class %s\n", loop->loop_nr, pressure, cls->name));
140
141         /* update info in set */
142         key.loop            = loop;
143         key.cls             = cls;
144         key.max_pressure    = 0;
145         entry               = (be_loop_info_t*)set_insert(loop_ana->data, &key, sizeof(key), HASH_LOOP_INFO(&key));
146         entry->max_pressure = MAX(entry->max_pressure, pressure);
147
148         return pressure;
149 }
150
151 /**
152  * Compute the register pressure for a class of all loops in a graph
153  * @param irg   The graph
154  * @param cls   The register class to compute the pressure for
155  * @return The loop analysis object.
156  */
157 be_loopana_t *be_new_loop_pressure_cls(ir_graph *irg,
158                                        const arch_register_class_t *cls)
159 {
160         be_loopana_t *loop_ana = XMALLOC(be_loopana_t);
161
162         loop_ana->data = new_set(cmp_loop_info, 16);
163         loop_ana->irg  = irg;
164
165         DBG((dbg, LEVEL_1, "\n=====================================================\n", cls->name));
166         DBG((dbg, LEVEL_1, " Computing register pressure for class %s:\n", cls->name));
167         DBG((dbg, LEVEL_1, "=====================================================\n", cls->name));
168
169         assure_loopinfo(irg);
170
171         be_compute_loop_pressure(loop_ana, get_irg_loop(irg), cls);
172
173         return loop_ana;
174 }
175
176 /**
177  * Compute the register pressure for all classes of all loops in the irg.
178  * @param irg  The graph
179  * @return The loop analysis object.
180  */
181 be_loopana_t *be_new_loop_pressure(ir_graph *irg,
182                                    const arch_register_class_t *cls)
183 {
184         be_loopana_t     *loop_ana = XMALLOC(be_loopana_t);
185         ir_loop          *irg_loop = get_irg_loop(irg);
186         const arch_env_t *arch_env = be_get_irg_arch_env(irg);
187         int               i;
188
189         loop_ana->data = new_set(cmp_loop_info, 16);
190         loop_ana->irg  = irg;
191
192         assure_loopinfo(irg);
193
194         if (cls != NULL) {
195                 be_compute_loop_pressure(loop_ana, irg_loop, cls);
196         } else {
197                 for (i = arch_env->n_register_classes - 1; i >= 0; --i) {
198                         const arch_register_class_t *cls = &arch_env->register_classes[i];
199                         DBG((dbg, LEVEL_1, "\n=====================================================\n", cls->name));
200                         DBG((dbg, LEVEL_1, " Computing register pressure for class %s:\n", cls->name));
201                         DBG((dbg, LEVEL_1, "=====================================================\n", cls->name));
202                         be_compute_loop_pressure(loop_ana, irg_loop, cls);
203                 }
204         }
205
206         return loop_ana;
207 }
208
209 /**
210  * Returns the computed register pressure for the given class and loop.
211  * @return The pressure or INT_MAX if not found
212  */
213 unsigned be_get_loop_pressure(be_loopana_t *loop_ana, const arch_register_class_t *cls, ir_loop *loop)
214 {
215         unsigned pressure = INT_MAX;
216         be_loop_info_t *entry, key;
217
218         assert(cls && loop);
219
220         key.loop = loop;
221         key.cls  = cls;
222         entry    = (be_loop_info_t*)set_find(loop_ana->data, &key, sizeof(key), HASH_LOOP_INFO(&key));
223
224         if (entry)
225                 pressure = entry->max_pressure;
226         else
227                 panic("Pressure not computed for given class and loop object.");
228
229         return pressure;
230 }
231
232 /**
233  * Frees the loop analysis object.
234  */
235 void be_free_loop_pressure(be_loopana_t *loop_ana)
236 {
237         del_set(loop_ana->data);
238         xfree(loop_ana);
239 }
240
241 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_loopana)
242 void be_init_loopana(void)
243 {
244         FIRM_DBG_REGISTER(dbg, "firm.be.loopana");
245 }