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
40 #include "beloopana.h"
43 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL);
45 #define HASH_LOOP_INFO(info) (HASH_PTR((info)->loop) ^ HASH_PTR((info)->cls))
47 typedef struct _be_loop_info_t {
49 const arch_register_class_t *cls;
50 unsigned max_pressure;
53 struct _be_loopana_t {
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;
63 return ! (i1->loop == i2->loop && i1->cls == i2->cls);
67 * Compute the highest register pressure in a block.
68 * @param birg 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 be_irg_t *birg,
74 ir_node *block, const arch_register_class_t *cls)
76 be_lv_t *lv = be_get_birg_liveness(birg);
77 ir_nodeset_t live_nodes;
81 DBG((dbg, LEVEL_1, "Processing Block %+F\n", block));
83 /* determine largest pressure with this block */
84 ir_nodeset_init(&live_nodes);
85 be_liveness_end_of_block(lv, cls, block, &live_nodes);
86 max_live = ir_nodeset_size(&live_nodes);
88 sched_foreach_reverse(block, irn) {
94 be_liveness_transfer(cls, irn, &live_nodes);
95 cnt = ir_nodeset_size(&live_nodes);
96 max_live = MAX(cnt, max_live);
99 DBG((dbg, LEVEL_1, "Finished with Block %+F (%s %u)\n", block, cls->name, max_live));
101 ir_nodeset_destroy(&live_nodes);
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.
112 static unsigned be_compute_loop_pressure(be_loopana_t *loop_ana, ir_loop *loop, const arch_register_class_t *cls) {
115 be_loop_info_t *entry, key;
117 DBG((dbg, LEVEL_1, "\nProcessing Loop %d\n", loop->loop_nr));
118 assert(get_loop_n_elements(loop) > 0);
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);
126 if (*elem.kind == k_ir_node)
127 son_pressure = be_compute_block_pressure(loop_ana->birg, elem.node, cls);
129 assert(*elem.kind == k_ir_loop);
130 son_pressure = be_compute_loop_pressure(loop_ana, elem.son, cls);
133 pressure = MAX(pressure, son_pressure);
135 DBG((dbg, LEVEL_1, "Done with loop %d, pressure %u for class %s\n", loop->loop_nr, pressure, cls->name));
137 /* update info in set */
140 key.max_pressure = 0;
141 entry = set_insert(loop_ana->data, &key, sizeof(key), HASH_LOOP_INFO(&key));
142 entry->max_pressure = MAX(entry->max_pressure, pressure);
148 * Compute the register pressure for a class of all loops in the birg.
149 * @param birg The backend irg object
150 * @param cls The register class to compute the pressure for
151 * @return The loop analysis object.
153 be_loopana_t *be_new_loop_pressure_cls(be_irg_t *birg,
154 const arch_register_class_t *cls) {
155 ir_graph *irg = be_get_birg_irg(birg);
156 be_loopana_t *loop_ana = XMALLOC(be_loopana_t);
158 loop_ana->data = new_set(cmp_loop_info, 16);
159 loop_ana->birg = birg;
161 DBG((dbg, LEVEL_1, "\n=====================================================\n", cls->name));
162 DBG((dbg, LEVEL_1, " Computing register pressure for class %s:\n", cls->name));
163 DBG((dbg, LEVEL_1, "=====================================================\n", cls->name));
165 /* construct control flow loop tree */
166 if (! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) {
167 construct_cf_backedges(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 birg.
177 * @param birg The backend irg object
178 * @return The loop analysis object.
180 be_loopana_t *be_new_loop_pressure(be_irg_t *birg,
181 const arch_register_class_t *cls)
183 ir_graph *irg = be_get_birg_irg(birg);
184 be_loopana_t *loop_ana = XMALLOC(be_loopana_t);
185 ir_loop *irg_loop = get_irg_loop(irg);
186 const arch_env_t *arch_env = be_get_birg_arch_env(birg);
189 loop_ana->data = new_set(cmp_loop_info, 16);
190 loop_ana->birg = birg;
192 /* construct control flow loop tree */
193 if (! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) {
194 construct_cf_backedges(irg);
198 be_compute_loop_pressure(loop_ana, irg_loop, cls);
200 for (i = arch_env_get_n_reg_class(arch_env) - 1; i >= 0; --i) {
201 const arch_register_class_t *cls = arch_env_get_reg_class(arch_env, i);
202 DBG((dbg, LEVEL_1, "\n=====================================================\n", cls->name));
203 DBG((dbg, LEVEL_1, " Computing register pressure for class %s:\n", cls->name));
204 DBG((dbg, LEVEL_1, "=====================================================\n", cls->name));
205 be_compute_loop_pressure(loop_ana, irg_loop, cls);
213 * Returns the computed register pressure for the given class and loop.
214 * @return The pressure or INT_MAX if not found
216 unsigned be_get_loop_pressure(be_loopana_t *loop_ana, const arch_register_class_t *cls, ir_loop *loop) {
217 unsigned pressure = INT_MAX;
218 be_loop_info_t *entry, key;
224 entry = set_find(loop_ana->data, &key, sizeof(key), HASH_LOOP_INFO(&key));
227 pressure = entry->max_pressure;
229 panic("Pressure not computed for given class and loop object.");
235 * Frees the loop analysis object.
237 void be_free_loop_pressure(be_loopana_t *loop_ana) {
238 del_set(loop_ana->data);
242 void be_init_loopana(void)
244 FIRM_DBG_REGISTER(dbg, "firm.be.loopana");
247 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_loopana);