2 * Copyright (C) 1995-2011 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
39 #include "beloopana.h"
42 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
44 #define HASH_LOOP_INFO(info) (hash_ptr((info)->loop) ^ hash_ptr((info)->cls))
46 typedef struct be_loop_info_t {
48 const arch_register_class_t *cls;
49 unsigned max_pressure;
57 static int cmp_loop_info(const void *a, const void *b, size_t size)
59 const be_loop_info_t *i1 = (const be_loop_info_t*)a;
60 const be_loop_info_t *i2 = (const be_loop_info_t*)b;
63 return ! (i1->loop == i2->loop && i1->cls == i2->cls);
67 * Compute the highest register pressure in a block.
68 * @param irg The graph.
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.
73 static unsigned be_compute_block_pressure(const ir_graph *irg,
75 const arch_register_class_t *cls)
77 be_lv_t *lv = be_get_irg_liveness(irg);
78 ir_nodeset_t live_nodes;
82 DBG((dbg, LEVEL_1, "Processing Block %+F\n", block));
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);
89 sched_foreach_reverse(block, irn) {
95 be_liveness_transfer(cls, irn, &live_nodes);
96 cnt = ir_nodeset_size(&live_nodes);
97 max_live = MAX(cnt, max_live);
100 DBG((dbg, LEVEL_1, "Finished with Block %+F (%s %zu)\n", block, cls->name, max_live));
102 ir_nodeset_destroy(&live_nodes);
107 * Compute the highest register pressure in a loop and its 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.
113 static unsigned be_compute_loop_pressure(be_loopana_t *loop_ana, ir_loop *loop,
114 const arch_register_class_t *cls)
118 be_loop_info_t *entry, key;
120 DBG((dbg, LEVEL_1, "\nProcessing Loop %ld\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 if (*elem.kind == k_ir_node)
130 son_pressure = be_compute_block_pressure(loop_ana->irg, elem.node, cls);
132 assert(*elem.kind == k_ir_loop);
133 son_pressure = be_compute_loop_pressure(loop_ana, elem.son, cls);
136 pressure = MAX(pressure, son_pressure);
138 DBG((dbg, LEVEL_1, "Done with loop %ld, pressure %u for class %s\n", loop->loop_nr, pressure, cls->name));
140 /* update info in set */
143 key.max_pressure = 0;
144 entry = set_insert(be_loop_info_t, loop_ana->data, &key, sizeof(key), HASH_LOOP_INFO(&key));
145 entry->max_pressure = MAX(entry->max_pressure, pressure);
151 * Compute the register pressure for a class of all loops in a graph
152 * @param irg The graph
153 * @param cls The register class to compute the pressure for
154 * @return The loop analysis object.
156 be_loopana_t *be_new_loop_pressure_cls(ir_graph *irg,
157 const arch_register_class_t *cls)
159 be_loopana_t *loop_ana = XMALLOC(be_loopana_t);
161 loop_ana->data = new_set(cmp_loop_info, 16);
164 DBG((dbg, LEVEL_1, "\n=====================================================\n", cls->name));
165 DBG((dbg, LEVEL_1, " Computing register pressure for class %s:\n", cls->name));
166 DBG((dbg, LEVEL_1, "=====================================================\n", cls->name));
168 assure_loopinfo(irg);
170 be_compute_loop_pressure(loop_ana, get_irg_loop(irg), cls);
176 * Compute the register pressure for all classes of all loops in the irg.
177 * @param irg The graph
178 * @return The loop analysis object.
180 be_loopana_t *be_new_loop_pressure(ir_graph *irg,
181 const arch_register_class_t *cls)
183 be_loopana_t *loop_ana = XMALLOC(be_loopana_t);
184 ir_loop *irg_loop = get_irg_loop(irg);
185 const arch_env_t *arch_env = be_get_irg_arch_env(irg);
188 loop_ana->data = new_set(cmp_loop_info, 16);
191 assure_loopinfo(irg);
194 be_compute_loop_pressure(loop_ana, irg_loop, cls);
196 for (i = arch_env->n_register_classes - 1; i >= 0; --i) {
197 const arch_register_class_t *cls = &arch_env->register_classes[i];
198 DBG((dbg, LEVEL_1, "\n=====================================================\n", cls->name));
199 DBG((dbg, LEVEL_1, " Computing register pressure for class %s:\n", cls->name));
200 DBG((dbg, LEVEL_1, "=====================================================\n", cls->name));
201 be_compute_loop_pressure(loop_ana, irg_loop, cls);
209 * Returns the computed register pressure for the given class and loop.
210 * @return The pressure or INT_MAX if not found
212 unsigned be_get_loop_pressure(be_loopana_t *loop_ana, const arch_register_class_t *cls, ir_loop *loop)
214 unsigned pressure = INT_MAX;
215 be_loop_info_t *entry, key;
221 entry = set_find(be_loop_info_t, loop_ana->data, &key, sizeof(key), HASH_LOOP_INFO(&key));
224 pressure = entry->max_pressure;
226 panic("Pressure not computed for given class and loop object.");
232 * Frees the loop analysis object.
234 void be_free_loop_pressure(be_loopana_t *loop_ana)
236 del_set(loop_ana->data);
240 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_loopana)
241 void be_init_loopana(void)
243 FIRM_DBG_REGISTER(dbg, "firm.be.loopana");