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