2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Compute register pressure in loops.
9 * @author Christian Wuerdig
26 #include "beloopana.h"
29 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
31 #define HASH_LOOP_INFO(info) (hash_ptr((info)->loop) ^ hash_ptr((info)->cls))
33 typedef struct be_loop_info_t {
35 const arch_register_class_t *cls;
36 unsigned max_pressure;
44 static int cmp_loop_info(const void *a, const void *b, size_t size)
46 const be_loop_info_t *i1 = (const be_loop_info_t*)a;
47 const be_loop_info_t *i2 = (const be_loop_info_t*)b;
50 return ! (i1->loop == i2->loop && i1->cls == i2->cls);
54 * Compute the highest register pressure in a block.
55 * @param irg The graph.
56 * @param block The block to compute pressure for.
57 * @param cls The register class to compute pressure for.
58 * @return The highest register pressure in the given block.
60 static unsigned be_compute_block_pressure(const ir_graph *irg,
62 const arch_register_class_t *cls)
64 be_lv_t *lv = be_get_irg_liveness(irg);
65 ir_nodeset_t live_nodes;
68 DBG((dbg, LEVEL_1, "Processing Block %+F\n", block));
70 /* determine largest pressure with this block */
71 ir_nodeset_init(&live_nodes);
72 be_liveness_end_of_block(lv, cls, block, &live_nodes);
73 max_live = ir_nodeset_size(&live_nodes);
75 sched_foreach_reverse(block, irn) {
81 be_liveness_transfer(cls, irn, &live_nodes);
82 cnt = ir_nodeset_size(&live_nodes);
83 max_live = MAX(cnt, max_live);
86 DBG((dbg, LEVEL_1, "Finished with Block %+F (%s %zu)\n", block, cls->name, max_live));
88 ir_nodeset_destroy(&live_nodes);
93 * Compute the highest register pressure in a loop and its sub-loops.
94 * @param loop_ana The loop ana object.
95 * @param loop The loop to compute pressure for.
96 * @param cls The register class to compute pressure for.
97 * @return The highest register pressure in the given loop.
99 static unsigned be_compute_loop_pressure(be_loopana_t *loop_ana, ir_loop *loop,
100 const arch_register_class_t *cls)
104 be_loop_info_t *entry, key;
106 DBG((dbg, LEVEL_1, "\nProcessing Loop %ld\n", loop->loop_nr));
107 assert(get_loop_n_elements(loop) > 0);
110 /* determine maximal pressure in all loop elements */
111 for (i = 0, max = get_loop_n_elements(loop); i < max; ++i) {
112 unsigned son_pressure;
113 loop_element elem = get_loop_element(loop, i);
115 if (*elem.kind == k_ir_node)
116 son_pressure = be_compute_block_pressure(loop_ana->irg, elem.node, cls);
118 assert(*elem.kind == k_ir_loop);
119 son_pressure = be_compute_loop_pressure(loop_ana, elem.son, cls);
122 pressure = MAX(pressure, son_pressure);
124 DBG((dbg, LEVEL_1, "Done with loop %ld, pressure %u for class %s\n", loop->loop_nr, pressure, cls->name));
126 /* update info in set */
129 key.max_pressure = 0;
130 entry = set_insert(be_loop_info_t, loop_ana->data, &key, sizeof(key), HASH_LOOP_INFO(&key));
131 entry->max_pressure = MAX(entry->max_pressure, pressure);
137 * Compute the register pressure for a class of all loops in a graph
138 * @param irg The graph
139 * @param cls The register class to compute the pressure for
140 * @return The loop analysis object.
142 be_loopana_t *be_new_loop_pressure_cls(ir_graph *irg,
143 const arch_register_class_t *cls)
145 be_loopana_t *loop_ana = XMALLOC(be_loopana_t);
147 loop_ana->data = new_set(cmp_loop_info, 16);
150 DBG((dbg, LEVEL_1, "\n=====================================================\n", cls->name));
151 DBG((dbg, LEVEL_1, " Computing register pressure for class %s:\n", cls->name));
152 DBG((dbg, LEVEL_1, "=====================================================\n", cls->name));
154 assure_loopinfo(irg);
156 be_compute_loop_pressure(loop_ana, get_irg_loop(irg), cls);
162 * Compute the register pressure for all classes of all loops in the irg.
163 * @param irg The graph
164 * @return The loop analysis object.
166 be_loopana_t *be_new_loop_pressure(ir_graph *irg,
167 const arch_register_class_t *cls)
169 be_loopana_t *loop_ana = XMALLOC(be_loopana_t);
170 ir_loop *irg_loop = get_irg_loop(irg);
171 const arch_env_t *arch_env = be_get_irg_arch_env(irg);
174 loop_ana->data = new_set(cmp_loop_info, 16);
177 assure_loopinfo(irg);
180 be_compute_loop_pressure(loop_ana, irg_loop, cls);
182 for (i = arch_env->n_register_classes - 1; i >= 0; --i) {
183 const arch_register_class_t *cls = &arch_env->register_classes[i];
184 DBG((dbg, LEVEL_1, "\n=====================================================\n", cls->name));
185 DBG((dbg, LEVEL_1, " Computing register pressure for class %s:\n", cls->name));
186 DBG((dbg, LEVEL_1, "=====================================================\n", cls->name));
187 be_compute_loop_pressure(loop_ana, irg_loop, cls);
195 * Returns the computed register pressure for the given class and loop.
196 * @return The pressure or INT_MAX if not found
198 unsigned be_get_loop_pressure(be_loopana_t *loop_ana, const arch_register_class_t *cls, ir_loop *loop)
200 unsigned pressure = INT_MAX;
201 be_loop_info_t *entry, key;
207 entry = set_find(be_loop_info_t, loop_ana->data, &key, sizeof(key), HASH_LOOP_INFO(&key));
210 pressure = entry->max_pressure;
212 panic("Pressure not computed for given class and loop object.");
218 * Frees the loop analysis object.
220 void be_free_loop_pressure(be_loopana_t *loop_ana)
222 del_set(loop_ana->data);
226 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_loopana)
227 void be_init_loopana(void)
229 FIRM_DBG_REGISTER(dbg, "firm.be.loopana");