Remove the unused parameter const arch_env_t *arch_env from be_liveness_transfer().
[libfirm] / ir / be / beloopana.c
1 /*
2  * Copyright (C) 1995-2008 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 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include "set.h"
32 #include "pset.h"
33 #include "irnode.h"
34 #include "irtools.h"
35 #include "irloop_t.h"
36 #include "error.h"
37 #include "debug.h"
38
39 #include "bearch_t.h"
40 #include "belive.h"
41 #include "besched.h"
42 #include "beloopana.h"
43 #include "bemodule.h"
44
45 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL);
46
47 #define HASH_LOOP_INFO(info) (HASH_PTR((info)->loop) ^ HASH_PTR((info)->cls))
48
49 typedef struct _be_loop_info_t {
50         ir_loop                     *loop;
51         const arch_register_class_t *cls;
52         unsigned                    max_pressure;
53 } be_loop_info_t;
54
55 struct _be_loopana_t {
56         set      *data;
57         be_irg_t *birg;
58 };
59
60 static int cmp_loop_info(const void *a, const void *b, size_t size) {
61         const be_loop_info_t *i1 = a;
62         const be_loop_info_t *i2 = b;
63         (void) size;
64
65         return ! (i1->loop == i2->loop && i1->cls == i2->cls);
66 }
67
68 /**
69  * Compute the highest register pressure in a block.
70  * @param loop_ana  The loop ana object.
71  * @param block     The block to compute pressure for.
72  * @param cls       The register class to compute pressure for.
73  * @return The highest register pressure in the given block.
74  */
75 static unsigned be_compute_block_pressure(be_loopana_t *loop_ana,
76                 ir_node *block, const arch_register_class_t *cls)
77 {
78         const be_irg_t   *birg       = loop_ana->birg;
79         const arch_env_t *aenv       = be_get_birg_arch_env(birg);
80         be_lv_t          *lv         = be_get_birg_liveness(birg);
81         ir_nodeset_t      live_nodes;
82         ir_node          *irn;
83         int              max_live;
84
85         DBG((dbg, LEVEL_1, "Processing Block %+F\n", block));
86
87         /* determine largest pressure with this block */
88         ir_nodeset_init(&live_nodes);
89         be_liveness_end_of_block(lv, aenv, cls, block, &live_nodes);
90         max_live   = ir_nodeset_size(&live_nodes);
91
92         sched_foreach_reverse(block, irn) {
93                 int cnt;
94
95                 if (is_Phi(irn))
96                         break;
97
98                 be_liveness_transfer(cls, irn, &live_nodes);
99                 cnt        = ir_nodeset_size(&live_nodes);
100                 max_live   = MAX(cnt, max_live);
101         }
102
103         DBG((dbg, LEVEL_1, "Finished with Block %+F (%s %u)\n", block, cls->name, max_live));
104
105         ir_nodeset_destroy(&live_nodes);
106         return max_live;
107 }
108
109 /**
110  * Compute the highest register pressure in a loop and it's sub-loops.
111  * @param loop_ana  The loop ana object.
112  * @param loop      The loop to compute pressure for.
113  * @param cls       The register class to compute pressure for.
114  * @return The highest register pressure in the given loop.
115  */
116 static unsigned be_compute_loop_pressure(be_loopana_t *loop_ana, ir_loop *loop, const arch_register_class_t *cls) {
117         int            i, max;
118         unsigned       pressure;
119         be_loop_info_t *entry, key;
120
121         DBG((dbg, LEVEL_1, "\nProcessing Loop %d\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                 switch (*elem.kind) {
131                         case k_ir_node:
132                                 son_pressure = be_compute_block_pressure(loop_ana, elem.node, cls);
133                                 break;
134                         case k_ir_loop:
135                                 son_pressure = be_compute_loop_pressure(loop_ana, elem.son, cls);
136                                 break;
137                         default:
138                                 panic("Unknown element found in loop");
139                                 break;
140                 }
141
142                 pressure = MAX(pressure, son_pressure);
143         }
144         DBG((dbg, LEVEL_1, "Done with loop %d, pressure %u for class %s\n", loop->loop_nr, pressure, cls->name));
145
146         /* update info in set */
147         key.loop            = loop;
148         key.cls             = cls;
149         key.max_pressure    = 0;
150         entry               = set_insert(loop_ana->data, &key, sizeof(key), HASH_LOOP_INFO(&key));
151         entry->max_pressure = MAX(entry->max_pressure, pressure);
152
153         return pressure;
154 }
155
156 /**
157  * Compute the register pressure for a class of all loops in the birg.
158  * @param birg  The backend irg object
159  * @param cls   The register class to compute the pressure for
160  * @return The loop analysis object.
161  */
162 be_loopana_t *be_new_loop_pressure_cls(be_irg_t *birg,
163                                        const arch_register_class_t *cls) {
164         ir_graph     *irg      = be_get_birg_irg(birg);
165         be_loopana_t *loop_ana = XMALLOC(be_loopana_t);
166
167         loop_ana->data = new_set(cmp_loop_info, 16);
168         loop_ana->birg = birg;
169
170         DBG((dbg, LEVEL_1, "\n=====================================================\n", cls->name));
171         DBG((dbg, LEVEL_1, " Computing register pressure for class %s:\n", cls->name));
172         DBG((dbg, LEVEL_1, "=====================================================\n", cls->name));
173
174         /* construct control flow loop tree */
175         if (! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) {
176                 construct_cf_backedges(irg);
177         }
178
179         be_compute_loop_pressure(loop_ana, get_irg_loop(irg), cls);
180
181         return loop_ana;
182 }
183
184 /**
185  * Compute the register pressure for all classes of all loops in the birg.
186  * @param birg  The backend irg object
187  * @return The loop analysis object.
188  */
189 be_loopana_t *be_new_loop_pressure(be_irg_t *birg,
190                                    const arch_register_class_t *cls)
191 {
192         ir_graph         *irg      = be_get_birg_irg(birg);
193         be_loopana_t     *loop_ana = XMALLOC(be_loopana_t);
194         ir_loop          *irg_loop = get_irg_loop(irg);
195         const arch_env_t *arch_env = be_get_birg_arch_env(birg);
196         int               i;
197
198         loop_ana->data = new_set(cmp_loop_info, 16);
199         loop_ana->birg = birg;
200
201         /* construct control flow loop tree */
202         if (! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) {
203                 construct_cf_backedges(irg);
204         }
205
206         if (cls != NULL) {
207                 be_compute_loop_pressure(loop_ana, irg_loop, cls);
208         } else {
209                 for (i = arch_env_get_n_reg_class(arch_env) - 1; i >= 0; --i) {
210                         const arch_register_class_t *cls = arch_env_get_reg_class(arch_env, i);
211                         DBG((dbg, LEVEL_1, "\n=====================================================\n", cls->name));
212                         DBG((dbg, LEVEL_1, " Computing register pressure for class %s:\n", cls->name));
213                         DBG((dbg, LEVEL_1, "=====================================================\n", cls->name));
214                         be_compute_loop_pressure(loop_ana, irg_loop, cls);
215                 }
216         }
217
218         return loop_ana;
219 }
220
221 /**
222  * Returns the computed register pressure for the given class and loop.
223  * @return The pressure or INT_MAX if not found
224  */
225 unsigned be_get_loop_pressure(be_loopana_t *loop_ana, const arch_register_class_t *cls, ir_loop *loop) {
226         unsigned pressure = INT_MAX;
227         be_loop_info_t *entry, key;
228
229         assert(cls && loop);
230
231         key.loop = loop;
232         key.cls  = cls;
233         entry    = set_find(loop_ana->data, &key, sizeof(key), HASH_LOOP_INFO(&key));
234
235         if (entry)
236                 pressure = entry->max_pressure;
237         else
238                 assert(0 && "Pressure not computed for given class and loop object.");
239
240         return pressure;
241 }
242
243 /**
244  * Frees the loop analysis object.
245  */
246 void be_free_loop_pressure(be_loopana_t *loop_ana) {
247         del_set(loop_ana->data);
248         xfree(loop_ana);
249 }
250
251 void be_init_loopana(void)
252 {
253         FIRM_DBG_REGISTER(dbg, "firm.be.loopana");
254 }
255
256 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_loopana);