2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Compute register pressure in loops.
23 * @author Christian Wuerdig
42 #include "beloopana.h"
45 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL);
47 #define HASH_LOOP_INFO(info) (HASH_PTR((info)->loop) ^ HASH_PTR((info)->cls))
49 typedef struct _be_loop_info_t {
51 const arch_register_class_t *cls;
52 unsigned max_pressure;
55 struct _be_loopana_t {
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;
65 return ! (i1->loop == i2->loop && i1->cls == i2->cls);
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.
75 static unsigned be_compute_block_pressure(be_loopana_t *loop_ana,
76 ir_node *block, const arch_register_class_t *cls)
78 const be_irg_t *birg = loop_ana->birg;
79 be_lv_t *lv = be_get_birg_liveness(birg);
80 ir_nodeset_t live_nodes;
84 DBG((dbg, LEVEL_1, "Processing Block %+F\n", block));
86 /* determine largest pressure with this block */
87 ir_nodeset_init(&live_nodes);
88 be_liveness_end_of_block(lv, cls, block, &live_nodes);
89 max_live = ir_nodeset_size(&live_nodes);
91 sched_foreach_reverse(block, irn) {
97 be_liveness_transfer(cls, irn, &live_nodes);
98 cnt = ir_nodeset_size(&live_nodes);
99 max_live = MAX(cnt, max_live);
102 DBG((dbg, LEVEL_1, "Finished with Block %+F (%s %u)\n", block, cls->name, max_live));
104 ir_nodeset_destroy(&live_nodes);
109 * Compute the highest register pressure in a loop and it's sub-loops.
110 * @param loop_ana The loop ana object.
111 * @param loop The loop to compute pressure for.
112 * @param cls The register class to compute pressure for.
113 * @return The highest register pressure in the given loop.
115 static unsigned be_compute_loop_pressure(be_loopana_t *loop_ana, ir_loop *loop, const arch_register_class_t *cls) {
118 be_loop_info_t *entry, key;
120 DBG((dbg, LEVEL_1, "\nProcessing Loop %d\n", loop->loop_nr));
121 assert(get_loop_n_elements(loop) > 0);
124 /* determine maximal pressure in all loop elements */
125 for (i = 0, max = get_loop_n_elements(loop); i < max; ++i) {
126 unsigned son_pressure;
127 loop_element elem = get_loop_element(loop, i);
129 switch (*elem.kind) {
131 son_pressure = be_compute_block_pressure(loop_ana, elem.node, cls);
134 son_pressure = be_compute_loop_pressure(loop_ana, elem.son, cls);
137 panic("Unknown element found in loop");
141 pressure = MAX(pressure, son_pressure);
143 DBG((dbg, LEVEL_1, "Done with loop %d, pressure %u for class %s\n", loop->loop_nr, pressure, cls->name));
145 /* update info in set */
148 key.max_pressure = 0;
149 entry = set_insert(loop_ana->data, &key, sizeof(key), HASH_LOOP_INFO(&key));
150 entry->max_pressure = MAX(entry->max_pressure, pressure);
156 * Compute the register pressure for a class of all loops in the birg.
157 * @param birg The backend irg object
158 * @param cls The register class to compute the pressure for
159 * @return The loop analysis object.
161 be_loopana_t *be_new_loop_pressure_cls(be_irg_t *birg,
162 const arch_register_class_t *cls) {
163 ir_graph *irg = be_get_birg_irg(birg);
164 be_loopana_t *loop_ana = XMALLOC(be_loopana_t);
166 loop_ana->data = new_set(cmp_loop_info, 16);
167 loop_ana->birg = birg;
169 DBG((dbg, LEVEL_1, "\n=====================================================\n", cls->name));
170 DBG((dbg, LEVEL_1, " Computing register pressure for class %s:\n", cls->name));
171 DBG((dbg, LEVEL_1, "=====================================================\n", cls->name));
173 /* construct control flow loop tree */
174 if (! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) {
175 construct_cf_backedges(irg);
178 be_compute_loop_pressure(loop_ana, get_irg_loop(irg), cls);
184 * Compute the register pressure for all classes of all loops in the birg.
185 * @param birg The backend irg object
186 * @return The loop analysis object.
188 be_loopana_t *be_new_loop_pressure(be_irg_t *birg,
189 const arch_register_class_t *cls)
191 ir_graph *irg = be_get_birg_irg(birg);
192 be_loopana_t *loop_ana = XMALLOC(be_loopana_t);
193 ir_loop *irg_loop = get_irg_loop(irg);
194 const arch_env_t *arch_env = be_get_birg_arch_env(birg);
197 loop_ana->data = new_set(cmp_loop_info, 16);
198 loop_ana->birg = birg;
200 /* construct control flow loop tree */
201 if (! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) {
202 construct_cf_backedges(irg);
206 be_compute_loop_pressure(loop_ana, irg_loop, cls);
208 for (i = arch_env_get_n_reg_class(arch_env) - 1; i >= 0; --i) {
209 const arch_register_class_t *cls = arch_env_get_reg_class(arch_env, i);
210 DBG((dbg, LEVEL_1, "\n=====================================================\n", cls->name));
211 DBG((dbg, LEVEL_1, " Computing register pressure for class %s:\n", cls->name));
212 DBG((dbg, LEVEL_1, "=====================================================\n", cls->name));
213 be_compute_loop_pressure(loop_ana, irg_loop, cls);
221 * Returns the computed register pressure for the given class and loop.
222 * @return The pressure or INT_MAX if not found
224 unsigned be_get_loop_pressure(be_loopana_t *loop_ana, const arch_register_class_t *cls, ir_loop *loop) {
225 unsigned pressure = INT_MAX;
226 be_loop_info_t *entry, key;
232 entry = set_find(loop_ana->data, &key, sizeof(key), HASH_LOOP_INFO(&key));
235 pressure = entry->max_pressure;
237 assert(0 && "Pressure not computed for given class and loop object.");
243 * Frees the loop analysis object.
245 void be_free_loop_pressure(be_loopana_t *loop_ana) {
246 del_set(loop_ana->data);
250 void be_init_loopana(void)
252 FIRM_DBG_REGISTER(dbg, "firm.be.loopana");
255 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_loopana);