2 * Copyright (C) 1995-2007 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, ir_node *block, const arch_register_class_t *cls) {
76 const be_irg_t *birg = loop_ana->birg;
77 const arch_env_t *aenv = be_get_birg_arch_env(birg);
78 be_lv_t *lv = be_get_birg_liveness(birg);
79 pset *live_nodes = pset_new_ptr_default();
83 DBG((dbg, LEVEL_1, "Processing Block %+F\n", block));
85 /* determine largest pressure with this block */
86 live_nodes = be_liveness_end_of_block(lv, aenv, cls, block, live_nodes);
87 max_live = pset_count(live_nodes);
89 sched_foreach_reverse(block, irn) {
95 live_nodes = be_liveness_transfer(aenv, cls, irn, live_nodes);
96 cnt = pset_count(live_nodes);
97 max_live = MAX(cnt, max_live);
100 DBG((dbg, LEVEL_1, "Finished with Block %+F (%s %u)\n", block, cls->name, max_live));
102 del_pset(live_nodes);
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.
113 static unsigned be_compute_loop_pressure(be_loopana_t *loop_ana, ir_loop *loop, const arch_register_class_t *cls) {
116 be_loop_info_t *entry, key;
118 DBG((dbg, LEVEL_1, "\nProcessing Loop %d\n", loop->loop_nr));
119 assert(get_loop_n_elements(loop) > 0);
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);
127 switch (*elem.kind) {
129 son_pressure = be_compute_block_pressure(loop_ana, elem.node, cls);
132 son_pressure = be_compute_loop_pressure(loop_ana, elem.son, cls);
135 panic("Unknown element found in loop");
139 pressure = MAX(pressure, son_pressure);
141 DBG((dbg, LEVEL_1, "Done with loop %d, pressure %u for class %s\n", loop->loop_nr, pressure, cls->name));
143 /* update info in set */
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);
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.
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(sizeof(*loop_ana));
164 loop_ana->data = new_set(cmp_loop_info, 16);
165 loop_ana->birg = birg;
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));
171 be_compute_loop_pressure(loop_ana, get_irg_loop(irg), cls);
177 * Compute the register pressure for all classes of all loops in the birg.
178 * @param birg The backend irg object
179 * @return The loop analysis object.
181 be_loopana_t *be_new_loop_pressure(be_irg_t *birg) {
182 ir_graph *irg = be_get_birg_irg(birg);
183 be_loopana_t *loop_ana = xmalloc(sizeof(*loop_ana));
184 ir_loop *irg_loop = get_irg_loop(irg);
185 const arch_env_t *arch_env = be_get_birg_arch_env(birg);
186 const arch_isa_t *isa = arch_env->isa;
189 loop_ana->data = new_set(cmp_loop_info, 16);
190 loop_ana->birg = birg;
192 for (i = arch_isa_get_n_reg_class(isa) - 1; i >= 0; --i) {
193 const arch_register_class_t *cls = arch_isa_get_reg_class(isa, i);
194 DBG((dbg, LEVEL_1, "\n=====================================================\n", cls->name));
195 DBG((dbg, LEVEL_1, " Computing register pressure for class %s:\n", cls->name));
196 DBG((dbg, LEVEL_1, "=====================================================\n", cls->name));
197 be_compute_loop_pressure(loop_ana, irg_loop, cls);
204 * Returns the computed register pressure for the given class and loop.
205 * @return The pressure or INT_MAX if not found
207 unsigned be_get_loop_pressure(be_loopana_t *loop_ana, const arch_register_class_t *cls, ir_loop *loop) {
208 unsigned pressure = INT_MAX;
209 be_loop_info_t *entry, key;
215 entry = set_find(loop_ana->data, &key, sizeof(key), HASH_LOOP_INFO(&key));
218 pressure = entry->max_pressure;
220 assert(0 && "Pressure not computed for given class and loop object.");
226 * Frees the loop analysis object.
228 void be_free_loop_pressure(be_loopana_t *loop_ana) {
229 del_set(loop_ana->data);
233 void be_init_loopana(void)
235 FIRM_DBG_REGISTER(dbg, "firm.be.loopana");
238 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_loopana);