unified mein file comments
[libfirm] / ir / be / beloopana.c
1 /*
2  * Copyright (C) 1995-2007 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 sz) {
61         const be_loop_info_t *i1 = a;
62         const be_loop_info_t *i2 = b;
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 loop_ana  The loop ana object.
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(be_loopana_t *loop_ana, ir_node *block, const arch_register_class_t *cls) {
75         const be_irg_t   *birg       = loop_ana->birg;
76         const arch_env_t *aenv       = be_get_birg_arch_env(birg);
77         be_lv_t          *lv         = be_get_birg_liveness(birg);
78         pset             *live_nodes = pset_new_ptr_default();
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         live_nodes = be_liveness_end_of_block(lv, aenv, cls, block, live_nodes);
86         max_live   = pset_count(live_nodes);
87
88         sched_foreach_reverse(block, irn) {
89                 int cnt;
90
91                 if (is_Phi(irn))
92                         break;
93
94                 live_nodes = be_liveness_transfer(aenv, cls, irn, live_nodes);
95                 cnt        = pset_count(live_nodes);
96                 max_live   = MAX(cnt, max_live);
97         }
98
99         DBG((dbg, LEVEL_1, "Finished with Block %+F (%s %u)\n", block, cls->name, max_live));
100
101         del_pset(live_nodes);
102         return max_live;
103 }
104
105 /**
106  * Compute the highest register pressure in a loop and it's sub-loops.
107  * @param loop_ana  The loop ana object.
108  * @param loop      The loop to compute pressure for.
109  * @param cls       The register class to compute pressure for.
110  * @return The highest register pressure in the given loop.
111  */
112 static unsigned be_compute_loop_pressure(be_loopana_t *loop_ana, ir_loop *loop, const arch_register_class_t *cls) {
113         int            i, max;
114         unsigned       pressure;
115         be_loop_info_t *entry, key;
116
117         DBG((dbg, LEVEL_1, "\nProcessing Loop %d\n", loop->loop_nr));
118         assert(get_loop_n_elements(loop) > 0);
119         pressure = 0;
120
121         /* determine maximal pressure in all loop elements */
122         for (i = 0, max = get_loop_n_elements(loop); i < max; ++i) {
123                 unsigned     son_pressure;
124                 loop_element elem = get_loop_element(loop, i);
125
126                 switch (*elem.kind) {
127                         case k_ir_node:
128                                 son_pressure = be_compute_block_pressure(loop_ana, elem.node, cls);
129                                 break;
130                         case k_ir_loop:
131                                 son_pressure = be_compute_loop_pressure(loop_ana, elem.son, cls);
132                                 break;
133                         default:
134                                 panic("Unknown element found in loop");
135                                 break;
136                 }
137
138                 pressure = MAX(pressure, son_pressure);
139         }
140         DBG((dbg, LEVEL_1, "Done with loop %d, pressure %u for class %s\n", loop->loop_nr, pressure, cls->name));
141
142         /* update info in set */
143         key.loop            = loop;
144         key.cls             = cls;
145         key.max_pressure    = 0;
146         entry               = set_insert(loop_ana->data, &key, sizeof(key), HASH_LOOP_INFO(&key));
147         entry->max_pressure = MAX(entry->max_pressure, pressure);
148
149         return pressure;
150 }
151
152 /**
153  * Compute the register pressure for a class of all loops in the birg.
154  * @param birg  The backend irg object
155  * @param cls   The register class to compute the pressure for
156  * @return The loop analysis object.
157  */
158 be_loopana_t *be_new_loop_pressure_cls(be_irg_t *birg,
159                                        const arch_register_class_t *cls) {
160         ir_graph     *irg      = be_get_birg_irg(birg);
161         be_loopana_t *loop_ana = xmalloc(sizeof(*loop_ana));
162
163         loop_ana->data = new_set(cmp_loop_info, 16);
164         loop_ana->birg = birg;
165
166         DBG((dbg, LEVEL_1, "\n=====================================================\n", cls->name));
167         DBG((dbg, LEVEL_1, " Computing register pressure for class %s:\n", cls->name));
168         DBG((dbg, LEVEL_1, "=====================================================\n", cls->name));
169
170         be_compute_loop_pressure(loop_ana, get_irg_loop(irg), cls);
171
172         return loop_ana;
173 }
174
175 /**
176  * Compute the register pressure for all classes of all loops in the birg.
177  * @param birg  The backend irg object
178  * @return The loop analysis object.
179  */
180 be_loopana_t *be_new_loop_pressure(be_irg_t *birg) {
181         ir_graph         *irg      = be_get_birg_irg(birg);
182         be_loopana_t     *loop_ana = xmalloc(sizeof(*loop_ana));
183         ir_loop          *irg_loop = get_irg_loop(irg);
184         const arch_env_t *arch_env = be_get_birg_arch_env(birg);
185         const arch_isa_t *isa      = arch_env->isa;
186         int               i;
187
188         loop_ana->data = new_set(cmp_loop_info, 16);
189         loop_ana->birg = birg;
190
191         for (i = arch_isa_get_n_reg_class(isa) - 1; i >= 0; --i) {
192                 const arch_register_class_t *cls = arch_isa_get_reg_class(isa, i);
193                 DBG((dbg, LEVEL_1, "\n=====================================================\n", cls->name));
194                 DBG((dbg, LEVEL_1, " Computing register pressure for class %s:\n", cls->name));
195                 DBG((dbg, LEVEL_1, "=====================================================\n", cls->name));
196                 be_compute_loop_pressure(loop_ana, irg_loop, cls);
197         }
198
199         return loop_ana;
200 }
201
202 /**
203  * Returns the computed register pressure for the given class and loop.
204  * @return The pressure or INT_MAX if not found
205  */
206 unsigned be_get_loop_pressure(be_loopana_t *loop_ana, const arch_register_class_t *cls, ir_loop *loop) {
207         unsigned pressure = INT_MAX;
208         be_loop_info_t *entry, key;
209
210         assert(cls && loop);
211
212         key.loop = loop;
213         key.cls  = cls;
214         entry    = set_find(loop_ana->data, &key, sizeof(key), HASH_LOOP_INFO(&key));
215
216         if (entry)
217                 pressure = entry->max_pressure;
218         else
219                 assert(0 && "Pressure not computed for given class and loop object.");
220
221         return pressure;
222 }
223
224 /**
225  * Frees the loop analysis object.
226  */
227 void be_free_loop_pressure(be_loopana_t *loop_ana) {
228         del_set(loop_ana->data);
229         xfree(loop_ana);
230 }
231
232 void be_init_loopana(void)
233 {
234         FIRM_DBG_REGISTER(dbg, "firm.be.loopana");
235 }
236
237 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_loopana);